This is looking much better. The line number of a document node is returned as zero because the document node has no real existence in the source XML. Try now getting the line numbers of some of the elements.
 
Michael Kay
http://www.saxonica.com/


From: saxon-help-admin@lists.sourceforge.net [mailto:saxon-help-admin@lists.sourceforge.net] On Behalf Of RamKumarDinesh Thillai
Sent: 06 October 2005 12:24
To: saxon-help@lists.sourceforge.net
Subject: Re: RE: RE: RE: [saxon] help in getting line number..


yeah i used 8.5 and i tried ur code but it returns line number as 0.

how it is getting the node line number if i set the source as the xml file?

Configuration config = new Configuration();            config.setLineNumbering(true);
XPathEvaluator xpath = new XPathEvaluator(config);         
NodeInfo doc = xpath.setSource(new StreamSource((new File xmlFileToLoad))));

System.out.println("The Line Number "+doc.getLineNumber());

what should this return?


On Thu, 06 Oct 2005 Michael Kay wrote :
>
>
>
>      thnx for ur relpy with sample eg.
>
>I do wish you could write in proper sentences - you've no idea how difficult
>I find it to decipher this stuff. I would be much more inclined to take time
>to help you if you gave the impression that you were taking care over
>constructing your questions.
>
>        but how could i use the parameter as Configuration instance in
>XPathEvaluator it is expecting a Evaluator.
>      it is showing error in that line .. XPathEvaluator..
>      one more ques.
>      can i set this configuration to a node i Parse.?
>
>I explained in my message that there are differences between 6.5.x and 8.x,
>and that this solution was the one appropriate to 8.x. I can't answer your
>questions unless you tell me which version you are using.
>
>Michael Kay
>Saxonica
>
>
>      Configuration config = new Configuration();
>      >config.setLineNumbering(true);
>      >XPathEvaluator xpath = new
>net.sf.saxon.xpath.XPathEvaluator(config);
>
>
>
>      On Wed, 05 Oct 2005 Michael Kay wrote :
>      >It's not possible to set the line numbering option when using the
>DOM
>      >DocumentBuilder interface to build a Saxon document.
>      >
>      >I've just realized from your code sample that you're using Saxon
>6.5.x
>      >rather than Saxon 8.x. I'm afraid my advice was based on Saxon 8.x.
>I'm
>      >having difficulty remembering much detail about 6.5.x, to be
>honest.
>      >
>      >I would suggest moving forward to 8.5.1, where you can take
>advantage of the
>      >JAXP XPath API. You can then do:
>      >
>      >Configuration config = new Configuration();
>      >config.setLineNumbering(true);
>      >XPathEvaluator xpath = new
>net.sf.saxon.xpath.XPathEvaluator(config);
>      >NodeInfo doc = xpath.setSource(new StreamSource(xmlfile));
>      >
>      >If you stick with 6.5.x, I think the best approach is to build the
>document
>      >using a JAXP identity transformation from a StreamSource to a
>DOMResult
>      >(initializing the DOMResult with a Saxon document node - see the
>      >TraxExamples.java file for details). If you set the right attribute
>on the
>      >TransformerFactory, as specified in my original reply, then line
>numbering
>      >should be enabled.
>      >
>      >Michael Kay
>      >http://www.saxonica.com/
>      >
>      >
>      >  _____
>      >
>      > From: saxon-help-admin@lists.sourceforge.net
>      >[mailto:saxon-help-admin@lists.sourceforge.net] On Behalf Of
>RamKumarDinesh
>      >Thillai
>      >Sent: 05 October 2005 04:14
>      >To: saxon-help@lists.sourceforge.net
>      >Subject: Re: RE: [saxon] help in getting line number..
>      >
>      >
>      >
>      >
>      >hi thnx for ur reply..
>      >  i tried it..
>      >
>      >the code i use is..
>      >  try
>      >      {
>      >        //  Setting a system property required by SAXON
>      >
>
> >System.setProperty("javax.xml.parsers.DocumentBuilderFactory","com.icl.saxo
>n
>      >.om.DocumentBuilderFactoryImpl");
>      >
>      >        //  Create DocumentBuilderFactory
>      >        DocumentBuilderFactory factory =
>      >DocumentBuilderFactory.newInstance();
>      >
>      >        //  Load the specified XML document
>      >        DocumentBuilder builder = factory.newDocumentBuilder();
>      >        InputSource inputSrc = new InputSource
>      >            (new FileInputStream(new File(xmlFileToLoad)));
>      >        Document xmlDoc = builder.parse(inputSrc);
>      >            NodeList dd=xmlDoc.getElementsByTagName("pubname");
>      >            Node dd1=dd.item(0);
>      >        //  Assign root node to SAXON NodeInfo, which is then used
>to
>      >        //  set the current context node for XPath searches
>      >        NodeInfo nodeInfo = (NodeInfo) dd1;
>      >
>      >
>      >        Context context = new Context();
>      >        context.setContextNode(nodeInfo);
>      >
>      >        //  If XML document defines namespaces, this would be the
>time to
>      >declare them
>      >        //  Use StandaloneContext.declareNamespace for this purpose
>      >
>      >        DocumentInfo docInfo = nodeInfo.getDocumentRoot();
>      >        NamePool namePool = docInfo.getNamePool();
>      >        StandaloneContext standaloneContext = new
>      >StandaloneContext(namePool);
>      >
>      >        System.out.println(standaloneContext.getLineNumber());
>      >        //  Parse the input XPath expression
>      >        Expression expr = Expression.make(xpathExpr,
>standaloneContext);
>      >
>      >        try
>      >        {
>      >            //  First, try to get a node-set, if it fails then try
>to
>      >            //  evaluate XPath expression as a string result value
>      >            NodeSetValue resultSet =
>expr.evaluateAsNodeSet(context);
>      >            System.out.println("the dll "+docInfo.getLineNumber()+"
>      >"+nodeInfo.getLineNumber()+"    "+context.getCurrentNodeInfo());
>      >            //  Loop over result node-set
>      >            NodeEnumeration enumResult = resultSet.enumerate();
>      >
>      >            int countNodes = 0;
>      >            while(enumResult.hasMoreElements())
>      >            {
>      >              try
>      >              {
>      >                  NodeInfo curResultNode =
>enumResult.nextElement();
>      >
>      >                  //  System.out.println("The Line Number
>      >"+curResultNode.getLineNumber());
>      >                  String curNodeValue =
>curResultNode.getStringValue();
>      >                  //  System.out.println(curNodeValue+"
>      >"+curResultNode.getLineNumber());
>      >                  if(curNodeValue != null && curNodeValue.length()
> > 0)
>      >                    result += curNodeValue + "\n";
>      >
>      >                  countNodes++;
>      >              }
>      >              catch(Exception exp)
>      >              {
>      >                  exp.printStackTrace();
>      >              }
>      >            }
>      >
>      >            if(countNodes > 0)
>      >            {
>      >              if(countNodes==1)
>      >              {
>      >                  result += "\nOne matching node found!";
>      >              }
>      >              else
>      >              {
>      >                  result += "\n" + String.valueOf(countNodes) +
>      >                    " matching nodes found!";
>      >              }
>      >            }
>      >
>      >        }
>      >        catch(XPathException exp)
>      >        {
>      >            result = expr.evaluateAsString(context);
>      >        }
>      >      }
>      >      catch(SAXException exp)
>      >      {
>      >        System.out.println("'" + xmlFileToLoad + "' is NOT
>well-formed.\n" +
>      >
>      >exp.toString());
>      >      }
>      >      catch(FactoryConfigurationError exp)
>      >      {
>      >        System.err.println(exp.toString());
>      >      }
>      >      catch(ParserConfigurationException exp)
>      >      {
>      >        System.err.println(exp.toString());
>      >      }
>      >      catch(Exception exp)
>      >      {
>      >        System.err.println(exp.toString());
>      >        exp.printStackTrace();
>      >      }
>      >so the line number when using nodeinfo it is returning -1. why is
>it so?
>      >On Tue, 04 Oct 2005 Michael Kay wrote :
>      > >Saxon will only include line numbers in a constructed tree if you
>set the
>      > >appropriate options. It depends a bit how you are building the
>tree. On the
>      > >TransformerFactory you can do
>      > >
>      > >setAttribute(FeatureKeys.LINE_NUMBERING, Boolean.TRUE)
>      > >
>      > >or there are methods to set line numbering on the Configuration
>and Builder
>      > >classes.
>      > >
>      > >Once the tree has been built to include line numbers, you can
>access the
>      > >line number using the getLineNumber() method on NodeInfo.
>      > >
>      > >P.S. Upper case and lower case are significant in Java names, and
>greatly
>      > >help readability in English too.
>      > >
>      > >Michael Kay
>      > >http://www.saxonica.com/
>      > >
>      > >
>      > >  _____
>      > >
>      > > From: saxon-help-admin@lists.sourceforge.net
>      > >[mailto:saxon-help-admin@lists.sourceforge.net] On Behalf Of
>RamKumarDinesh
>      > >Thillai
>      > >Sent: 04 October 2005 04:18
>      > >To: saxon-help@lists.sourceforge.net
>      > >Subject: [saxon] help in getting line number..
>      > >
>      > >
>      > >
>      > >
>      > >hi guru,
>      > >      i am using saxon for reading a node in java.when i parse a
>node or
>      > >node list i need to get the lin number of that particular node..
>      > >how do i get i have seen te class nodeinfo has getlinenumber()
>option but
>      > >still it is not working out could u help me out in getting the
>line number
>      > >of a node i am reading if there is a sample code it will be more
>useful
>      > >
>      > >regards,
>      > >ram.t
>      > >
>      > >
>      > >
>      > >
>      >
> ><http://adworks.rediff.com/cgi-bin/AdWorks/sigclick.cgi/www.rediff.com/sign
>      >a
>      > >ture-home.htm/1507191490@Middle5?PARTNER=3>
>      > >
>      >
>      >
>      >
>      >
>      >
>
> ><http://adworks.rediff.com/cgi-bin/AdWorks/sigclick.cgi/www.rediff.com/sign
>a
>      >ture-home.htm/1507191490@Middle5?PARTNER=3>
>      >
>
>
>
>
>
><http://adworks.rediff.com/cgi-bin/AdWorks/sigclick.cgi/www.rediff.com/signa
>ture-home.htm/1507191490@Middle5?PARTNER=3>
>
>
>
>
>
>-------------------------------------------------------
>This SF.Net email is sponsored by:
>Power Architecture Resource Center: Free content, downloads, discussions,
>and more. http://solutions.newsforge.com/ibmarch.tmpl
>_______________________________________________
>saxon-help mailing list
>saxon-help@lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/saxon-help