Conditional xquery based on sibling node

Silddiombretma

New Member
I have an xml file containing a bit with this general structure:\[code\]<List> <Outer> <CheckValue> no </CheckValue> <Inner> 2345 </Inner> </Outer> <Outer> <CheckValue> yes </CheckValue> <Inner> 1234 </Inner> </Outer></List>\[/code\]I'd like to select the Inner nodes associated with certain CheckValues. In this example, that would be finding the Outer nodes having CheckValues of yes and returning the Inner nodes there.My current attempt involves pulling out all the Outer nodes with xquery, and checking the CheckValue with another xquery on each node so that I could retrieve the Inner node if the CheckValue were appropriate:\[code\]DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse("filePath.xml");XPathFactory xPathfactory = XPathFactory.newInstance();XPath xpath = xPathfactory.newXPath();XPathExpression expr = xpath.compile("/List/Outer");NodeList nodes = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);for (int i = 0; i < nodes.getLength(); i++) { Node curr = nodes.item(i); expr = xpath.compile("/CheckValue/text()"); NodeList nodesInside = (NodeList)expr.evaluate(doc, XPathConstants.NODESET); for (int j = 0; j < nodesInside.getLength(); j++) { //Here I'd like to check the value of CheckValue and act appropriately, however execution never enters this for loop } System.out.println(curr.getNodeValue());}\[/code\]From what I can tell, my inner xquery finds nothing. Do I have to use different syntax when caling xquery on a node rather than a document? I suspect there's a much better way to do this, writing the whole query in a single command, but this is my first time using xquery, and I've not yet figured all that out. (pointing me toward how to go about that would also be excellent)
 
Back
Top