help in getting line number..

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

help in getting line number..

RamKumarDinesh Thillai

 
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



Reply | Threaded
Open this post in threaded view
|

RE: help in getting line number..

Michael Kay
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


From: [hidden email] [mailto:[hidden email]] On Behalf Of RamKumarDinesh Thillai
Sent: 04 October 2005 04:18
To: [hidden email]
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



Reply | Threaded
Open this post in threaded view
|

Re: RE: help in getting line number..

RamKumarDinesh Thillai
In reply to this post by RamKumarDinesh Thillai

 
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.saxon.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: [hidden email]
>[mailto:[hidden email]] On Behalf Of RamKumarDinesh
>Thillai
>Sent: 04 October 2005 04:18
>To: [hidden email]
>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/signa
>ture-home.htm/1507191490@Middle5?PARTNER=3>
>



Reply | Threaded
Open this post in threaded view
|

Re: RE: help in getting line number..

RamKumarDinesh Thillai
In reply to this post by RamKumarDinesh Thillai

 
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.saxon.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?

regards

ram.t


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: [hidden email]
>[mailto:[hidden email]] On Behalf Of RamKumarDinesh
>Thillai
>Sent: 04 October 2005 04:18
>To: [hidden email]
>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/signa
>ture-home.htm/1507191490@Middle5?PARTNER=3>
>



Reply | Threaded
Open this post in threaded view
|

RE: RE: help in getting line number..

Michael Kay
In reply to this post by RamKumarDinesh Thillai
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: [hidden email] [mailto:[hidden email]] On Behalf Of RamKumarDinesh Thillai
Sent: 05 October 2005 04:14
To: [hidden email]
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.saxon.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: [hidden email]
>[mailto:[hidden email]] On Behalf Of RamKumarDinesh
>Thillai
>Sent: 04 October 2005 04:18
>To: [hidden email]
>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/signa
>ture-home.htm/1507191490@Middle5?PARTNER=3>
>



Reply | Threaded
Open this post in threaded view
|

Re: RE: RE: help in getting line number..

RamKumarDinesh Thillai
In reply to this post by RamKumarDinesh Thillai


thnx for ur relpy with sample eg.
  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.?


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: [hidden email]
>[mailto:[hidden email]] On Behalf Of RamKumarDinesh
>Thillai
>Sent: 05 October 2005 04:14
>To: [hidden email]
>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.saxon
>.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: [hidden email]
> >[mailto:[hidden email]] On Behalf Of RamKumarDinesh
> >Thillai
> >Sent: 04 October 2005 04:18
> >To: [hidden email]
> >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/signa
>ture-home.htm/1507191490@Middle5?PARTNER=3>
>



Reply | Threaded
Open this post in threaded view
|

RE: RE: RE: help in getting line number..

Michael Kay
 

       
        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: [hidden email]
        >[mailto:[hidden email]] On Behalf Of
RamKumarDinesh
        >Thillai
        >Sent: 05 October 2005 04:14
        >To: [hidden email]
        >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: [hidden email]
        > >[mailto:[hidden email]] On Behalf Of
RamKumarDinesh
        > >Thillai
        > >Sent: 04 October 2005 04:18
        > >To: [hidden email]
        > >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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help
Reply | Threaded
Open this post in threaded view
|

Re: RE: RE: RE: help in getting line number..

RamKumarDinesh Thillai
In reply to this post by RamKumarDinesh Thillai

 
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: [hidden email]
>      >[mailto:[hidden email]] On Behalf Of
>RamKumarDinesh
>      >Thillai
>      >Sent: 05 October 2005 04:14
>      >To: [hidden email]
>      >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: [hidden email]
>      > >[mailto:[hidden email]] On Behalf Of
>RamKumarDinesh
>      > >Thillai
>      > >Sent: 04 October 2005 04:18
>      > >To: [hidden email]
>      > >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
>[hidden email]
>https://lists.sourceforge.net/lists/listinfo/saxon-help



Reply | Threaded
Open this post in threaded view
|

RE: RE: RE: RE: help in getting line number..

Michael Kay
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: [hidden email] [mailto:[hidden email]] On Behalf Of RamKumarDinesh Thillai
Sent: 06 October 2005 12:24
To: [hidden email]
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: [hidden email]
>      >[mailto:[hidden email]] On Behalf Of
>RamKumarDinesh
>      >Thillai
>      >Sent: 05 October 2005 04:14
>      >To: [hidden email]
>      >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: [hidden email]
>      > >[mailto:[hidden email]] On Behalf Of
>RamKumarDinesh
>      > >Thillai
>      > >Sent: 04 October 2005 04:18
>      > >To: [hidden email]
>      > >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
>[hidden email]
>https://lists.sourceforge.net/lists/listinfo/saxon-help



Reply | Threaded
Open this post in threaded view
|

Re: RE: RE: RE: RE: help in getting line number..

RamKumarDinesh Thillai
In reply to this post by RamKumarDinesh Thillai

 
yeah thats what i want it,
  how do i get the line number of a element?could u help more on this?



On Thu, 06 Oct 2005 Michael Kay wrote :
>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: [hidden email]
>[mailto:[hidden email]] On Behalf Of RamKumarDinesh
>Thillai
>Sent: 06 October 2005 12:24
>To: [hidden email]
>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: [hidden email]
> >      >[mailto:[hidden email]] On Behalf Of
> >RamKumarDinesh
> >      >Thillai
> >      >Sent: 05 October 2005 04:14
> >      >To: [hidden email]
> >      >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: [hidden email]
> >      > >[mailto:[hidden email]] On Behalf Of
> >RamKumarDinesh
> >      > >Thillai
> >      > >Sent: 04 October 2005 04:18
> >      > >To: [hidden email]
> >      > >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/sign
>a
> >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
> >[hidden email]
> >https://lists.sourceforge.net/lists/listinfo/saxon-help
>
>
>
>
>
><http://adworks.rediff.com/cgi-bin/AdWorks/sigclick.cgi/www.rediff.com/signa
>ture-home.htm/1507191490@Middle5?PARTNER=3>
>



Reply | Threaded
Open this post in threaded view
|

RE: RE: RE: RE: RE: help in getting line number..

Michael Kay
You evaluate an XPath expression that returns the element and then you call getLineNumber() on the result.
 
Michael Kay
Saxonica


From: [hidden email] [mailto:[hidden email]] On Behalf Of RamKumarDinesh Thillai
Sent: 07 October 2005 04:12
To: [hidden email]
Subject: Re: RE: RE: RE: RE: [saxon] help in getting line number..


yeah thats what i want it,
  how do i get the line number of a element?could u help more on this?



On Thu, 06 Oct 2005 Michael Kay wrote :


>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: [hidden email]
>[mailto:[hidden email]] On Behalf Of RamKumarDinesh
>Thillai
>Sent: 06 October 2005 12:24
>To: [hidden email]
>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: [hidden email]
> >      >[mailto:[hidden email]] On Behalf Of
> >RamKumarDinesh
> >      >Thillai
> >      >Sent: 05 October 2005 04:14
> >      >To: [hidden email]
> >      >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: [hidden email]
> >      > >[mailto:[hidden email]] On Behalf Of
> >RamKumarDinesh
> >      > >Thillai
> >      > >Sent: 04 October 2005 04:18
> >      > >To: [hidden email]
> >      > >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/sign
>a
> >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
> >[hidden email]
> >https://lists.sourceforge.net/lists/listinfo/saxon-help
>
>
>
>
>
><http://adworks.rediff.com/cgi-bin/AdWorks/sigclick.cgi/www.rediff.com/signa
>ture-home.htm/1507191490@Middle5?PARTNER=3>
>



Reply | Threaded
Open this post in threaded view
|

Re: RE: RE: RE: RE: RE: help in getting line number..

RamKumarDinesh Thillai
In reply to this post by RamKumarDinesh Thillai

  yeah tried what u gave..

  like
            Configuration config = new Configuration();
            config.setLineNumbering(true);
     
            XPathEvaluator xpath = new XPathEvaluator(config);
            String expression = "//table/tgroup/@cols";
            Node pubnode=(Node)xpath.evaluate(expression,inputSrc,XPathConstants.NODE);
            NodeInfo doca = xpath.setSource(new StreamSource((new File(xmlFileToLoad))));
            doca.getLineNumber();
            NodeInfo xx=(NodeInfo)pubnode;
           
            System.out.println("The Line Number Is "+ doca.getLineNumber()+"  "+xx.getLineNumber());
   

but it gives me a runtime error in the xpath like..

javax.xml.xpath.XPathExpressionException
net.sf.saxon.trans.DynamicError: net.sf.saxon.trans.DynamicError: java.io.IOException: No such file or directory
        at net.sf.saxon.event.Builder.build(Builder.java:194)
        at net.sf.saxon.xpath.XPathEvaluator.setSource(XPathEvaluator.java:115)
        at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:295)
        at net.sf.saxon.xpath.XPathEvaluator.evaluate(XPathEvaluator.java:363)
        at testforxpath.DOMSaxon_EvalXPath.evalXPath(DOMSaxon_EvalXPath.java:127)
        at testforxpath.DOMSaxon_EvalXPath.main(DOMSaxon_EvalXPath.java:99)
Caused by: net.sf.saxon.trans.DynamicError: java.io.IOException: No such file or directory
        at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:280)
        at net.sf.saxon.event.Sender.send(Sender.java:114)
        at net.sf.saxon.event.Sender.send(Sender.java:45)
        at net.sf.saxon.event.Builder.build(Builder.java:192)
        ... 5 more
Caused by: java.io.IOException: No such file or directory
        at java.io.FileInputStream.read(Native Method)
        at org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
        at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
        at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:269)
        ... 8 more
---------
net.sf.saxon.trans.DynamicError: java.io.IOException: No such file or directory
        at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:280)
        at net.sf.saxon.event.Sender.send(Sender.java:114)
        at net.sf.saxon.event.Sender.send(Sender.java:45)
        at net.sf.saxon.event.Builder.build(Builder.java:192)
        at net.sf.saxon.xpath.XPathEvaluator.setSource(XPathEvaluator.java:115)
        at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:295)
        at net.sf.saxon.xpath.XPathEvaluator.evaluate(XPathEvaluator.java:363)
        at testforxpath.DOMSaxon_EvalXPath.evalXPath(DOMSaxon_EvalXPath.java:127)
        at testforxpath.DOMSaxon_EvalXPath.main(DOMSaxon_EvalXPath.java:99)
Caused by: java.io.IOException: No such file or directory

what is the problem?




On Fri, 07 Oct 2005 Michael Kay wrote :
>You evaluate an XPath expression that returns the element and then you call
>getLineNumber() on the result.
>
>Michael Kay
>Saxonica
>
>
>  _____
>
> From: [hidden email]
>[mailto:[hidden email]] On Behalf Of RamKumarDinesh
>Thillai
>Sent: 07 October 2005 04:12
>To: [hidden email]
>Subject: Re: RE: RE: RE: RE: [saxon] help in getting line number..
>
>
>
>
>yeah thats what i want it,
>  how do i get the line number of a element?could u help more on this?
>
>
>
>On Thu, 06 Oct 2005 Michael Kay wrote :
> >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: [hidden email]
> >[mailto:[hidden email]] On Behalf Of RamKumarDinesh
> >Thillai
> >Sent: 06 October 2005 12:24
> >To: [hidden email]
> >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: [hidden email]
> > >      >[mailto:[hidden email]] On Behalf Of
> > >RamKumarDinesh
> > >      >Thillai
> > >      >Sent: 05 October 2005 04:14
> > >      >To: [hidden email]
> > >      >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: [hidden email]
> > >      > >[mailto:[hidden email]] On Behalf Of
> > >RamKumarDinesh
> > >      > >Thillai
> > >      > >Sent: 04 October 2005 04:18
> > >      > >To: [hidden email]
> > >      > >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/sign
> >a
> > >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
> > >[hidden email]
> > >https://lists.sourceforge.net/lists/listinfo/saxon-help
> >
> >
> >
> >
> >
> ><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>
>