Project

Profile

Help

saxon:line-number saxon9 api

Added by Anonymous almost 16 years ago

Legacy ID: #6177321 Legacy Poster: pvallone (pvallone)

Using the saxon9 API (java, embedded), How do I set the option -l when not running from the command line? Thanks, Phil


Replies (11)

Please register to reply

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6177693 Legacy Poster: Michael Kay (mhkay)

You can set it globally using either of: Configuration.setLineNumbering() TransformerFactory.setFeature(FeatureKeys.LINE_NUMBERING) or for finer granularity: AugmentedSource as = AugmentedSource.makeAugmentedSource(source); as.setLineNumbering(true); Configuration.buildDocument(as);

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6178030 Legacy Poster: pvallone (pvallone)

Thank you Michael. With system property set: System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); The following: TransformerFactory factory = TransformerFactory.newInstance(); factory.setFeature(FeatureKeys.LINE_NUMBERING, true); returns: [Fatal Error]Transformation Failed. See below for error. javax.xml.transform.TransformerConfigurationException: Unsupported TransformerFactory feature: http://saxon.sf.net/feature/linenumbering Also, I tried: net.sf.saxon.Configuration cong = new net.sf.saxon.Configuration(); cong.setLineNumbering(true); returns: Exception in thread "main" java.lang.AbstractMethodError: net.sf.saxon.dom.DOMEnvelope.getPJConverter(Ljava/lang/Class;)Lnet/sf/saxon/expr/PJConverter; at net.sf.saxon.expr.PJConverter.allocate(PJConverter.java:129) at net.sf.saxon.functions.ExtensionFunctionCall.typeCheck(ExtensionFunctionCall.java:116) at net.sf.saxon.expr.ExpressionVisitor.typeCheck(ExpressionVisitor.java:181) at net.sf.saxon.style.StyleElement.typeCheck(StyleElement.java:1013) at net.sf.saxon.style.XSLValueOf.validate(XSLValueOf.java:85) at net.sf.saxon.style.StyleElement.validateSubtree(StyleElement.java:1234) at net.sf.saxon.style.StyleElement.validateChildren(StyleElement.java:1263) at net.sf.saxon.style.StyleElement.validateSubtree(StyleElement.java:1238) at net.sf.saxon.style.StyleElement.validateChildren(StyleElement.java:1263) at net.sf.saxon.style.StyleElement.validateSubtree(StyleElement.java:1238) at net.sf.saxon.style.StyleElement.validateChildren(StyleElement.java:1263) at net.sf.saxon.style.StyleElement.validateSubtree(StyleElement.java:1238) at net.sf.saxon.style.StyleElement.validateChildren(StyleElement.java:1263) at net.sf.saxon.style.StyleElement.validateSubtree(StyleElement.java:1238) at net.sf.saxon.style.StyleElement.validateChildren(StyleElement.java:1263) at net.sf.saxon.style.StyleElement.validateSubtree(StyleElement.java:1238) at net.sf.saxon.style.XSLStylesheet.preprocess(XSLStylesheet.java:705) at net.sf.saxon.PreparedStylesheet.setStylesheetDocument(PreparedStylesheet.java:331) at net.sf.saxon.PreparedStylesheet.prepare(PreparedStylesheet.java:163) at net.sf.saxon.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:139) at net.sf.saxon.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:91) at acm.transform.Transform.convertXML2PDF(Transform.java:142) at acm.events.Events.doEvents(Events.java:69) at acm.main.ACMMELCreator.parseArgs(ACMMELCreator.java:140) at acm.main.ACMMELCreator.main(ACMMELCreator.java:43) What am I missing? Thanks Phil

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6178218 Legacy Poster: Michael Kay (mhkay)

>TransformerFactory factory = TransformerFactory.newInstance(); >factory.setFeature(FeatureKeys.LINE_NUMBERING, true); I suspect your TransformerFactory isn't Saxon. Try TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl(); if you want to be sure of getting the Saxon implementation. >Exception in thread "main" java.lang.AbstractMethodError: That has nothing to do with your attempt to set line numbering. It's probably because the saxon9-dom.jar isn't on the classpath (or worse, because the wrong version of it is on the classpath.)

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6178347 Legacy Poster: pvallone (pvallone)

Hmmm TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl(); factory.setFeature(FeatureKeys.LINE_NUMBERING, true); Still returns: javax.xml.transform.TransformerConfigurationException: Unsupported TransformerFactory feature: http://saxon.sf.net/feature/linenumbering I must be using the "net.sf.saxon.TransformerFactoryImpl()" because my xslt is using 2.0 features. As for <It's probably because the saxon9-dom.jar isn't on the classpath> all my saxon jar files are in the class path and have been imported (using netbeans IDE)

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6178458 Legacy Poster: Michael Kay (mhkay)

>TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl(); >factory.setFeature(FeatureKeys.LINE_NUMBERING, true); Sorry, my mistake. It should be factory.setAttribute(FeatureKeys.LINE_NUMBERING, Boolean.TRUE); <It's probably because the saxon9-dom.jar isn't on the classpath> >all my saxon jar files are in the class path and have been imported (using netbeans IDE) Well, AbstractMethodError basically means that you're executing code that contains a reference to a method that isn't on your classpath. So it's some kind of configuration problem - in my experience, often caused by loading a JAR file at run time that isn't the one that the code was compiled against.

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6178614 Legacy Poster: pvallone (pvallone)

Thanks. It must be a configuration issue. Thanks again.

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6257703 Legacy Poster: pvallone (pvallone)

Hi, I wanted to follow up on my question. I am still unable to return a line number. So I wanted to also try it using xquery: Configuration config = new Configuration(); config.setLineNumbering(true); Processor proc = new Processor(true); XQueryCompiler comp = proc.newXQueryCompiler(); DocumentBuilder builder = proc.newDocumentBuilder(); builder.setLineNumbering(true); builder.setWhitespaceStrippingPolicy(WhitespaceStrippingPolicy.ALL); XdmNode source = builder.build(new File(myFile)); XQueryExecutable exp = comp.compile(xqr); XQueryEvaluator eval = exp.load(); eval.setContextItem(source); Serializer out = new Serializer(); out.setOutputProperty(Serializer.Property.METHOD, "xml"); out.setOutputProperty(Serializer.Property.INDENT, "yes"); out.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes"); out.setOutputStream(System.out); StringWriter sw = new StringWriter(); out.setOutputWriter(sw); eval.run(out); Here is my xquery: declare namespace f = "http://somewhere.com/"; declare namespace saxon = "http://saxon.sf.net/"; declare function f:name-and-position($n as element()) as xs:string { concat(name($n), '[', 1+count($n/preceding-sibling::[node-name(.) = node-name($n)]), ']') }; for $h in //xref/@href let $xpath := $h/string-join(ancestor-or-self:: /f:name-and-position(.), '/') let $base := $h/base-uri() let $line := $h/saxon:line-number(.) return if (//@id[.=$h]) then <ok/> else <not-found id="{$h}" file="{$base}" line="{$line}">/{$xpath} </not-found> Line number returns "-1", which indicates something is not turned on. What am I missing?

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6257913 Legacy Poster: Michael Kay (mhkay)

You are applying saxon:line-number() to an attribute node. Saxon maintains line numbers only for element nodes: try saxon:line-number(..). Michael Kay http://www.saxonica.com/

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6258245 Legacy Poster: pvallone (pvallone)

Thank you Michael. Unfortunately I get the same result. The original xquery expression works from the command line (returns a line number) Can you be so kind an post a working example? Thank you, Phil

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6260945 Legacy Poster: Michael Kay (mhkay)

The following code, which is closely based on your code, works fine for me: public void testXQueryLineNumber() { try { Configuration config = new Configuration(); config.setLineNumbering(true); Processor proc = new Processor(true); XQueryCompiler comp = proc.newXQueryCompiler(); DocumentBuilder builder = proc.newDocumentBuilder(); builder.setLineNumbering(true); builder.setWhitespaceStrippingPolicy(WhitespaceStrippingPolicy.ALL); XdmNode source = builder.build(new File("c:/MyJava/testdata/books.xml")); XQueryExecutable exp = comp.compile("saxon:line-number((//WEIGHT)[1])"); XQueryEvaluator eval = exp.load(); eval.setContextItem(source); Serializer out = new Serializer(); out.setOutputProperty(Serializer.Property.METHOD, "xml"); out.setOutputProperty(Serializer.Property.INDENT, "yes"); out.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes"); out.setOutputStream(System.out); StringWriter sw = new StringWriter(); out.setOutputWriter(sw); eval.run(out); System.out.println(sw.toString()); } catch (SaxonApiException e) { e.printStackTrace(); fail(); }

RE: saxon:line-number saxon9 api - Added by Anonymous almost 16 years ago

Legacy ID: #6269269 Legacy Poster: pvallone (pvallone)

Thank you Michael, Everything works. As you suggested my configuration was wrong. From what I understand, Configuration config = new Configuration(); was not using net.sf.saxon.Configuration. So once I fixed the reference, this line: Processor proc = new Processor(true); was throwing the error (schema aware): java.lang.ClassNotFoundException: com.saxonica.validate.SchemaAwareConfiguration So now I know that I was using saxon's Configuration Class. All is well now. Thanks Again! Phil

    (1-11/11)

    Please register to reply