Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Using //

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 6/16/2009 1:54:00 PM
JS wrote:

>  From Java I am trying to print values for all elements where ID = "A":
> 
>  public void test(){
>    javax.xml.xpath.XPathFactory factory = 
> javax.xml.xpath.XPathFactory.newInstance();
>    javax.xml.xpath.XPath xpath = factory.newXPath();
>    javax.xml.xpath.XPathExpression expression;
>    String result;
>    try {
>      expression = xpath.compile("//content[@id =\"A\"]/@value");
>      URL url = this.getClass().getClassLoader().getResource("test.xml");
>      String path = url.toURI().getPath();
>      result = expression.evaluate(new org.xml.sax.InputSource(path));
>      System.out.println("result is = " + result);
>    } catch (Exception e) {
>      e.printStackTrace();
>    }
>  }
> 
> 
> But only:
> 
> 0_A
> 
> is printed. Does "//" not mean that all 'content' elements are chosen?

There is an overload
http://xml.apache.org/xalan-j/apidocs/javax/xml/xpath/XPathExpression.html#evaluate(org.xml.sax.InputSource,%20javax.xml.namespace.QName)
of the evaluate method where the second argument allows you to choose a 
result different from a string so use that to select 
javax.xml.xpath.XPathConstants.NODESET as the return type, then I think 
you can cast the returned object to a DOM NodeList and iterate over that 
node list to access all selected nodes.

The method you currently use converts the selected node-set to a string 
and that, with XPath 1.0, means you always get the string value of the 
first node in the node-set.


-- 

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent