I’m trying to figure out how to use a Xpath expression to verify that there is only one matching node in an XPath expression. Here is some sample XML:
<a>
<b>
<c>X:1 Y:0</c>
<c>X:1 Y:0</c>
<c>X:2 Y:0</c>
</b>
<b>
<c>X:1 Y:0</c>
<c>X:2 Y:0</c>
</b>
</a>
So, I tried code similar to this, but it doesn’t work:
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
try {
XPathExpression expr = xpath.compile( "count(//a/b/c)" ) );
} catch ( XPathExpressionException e ) {
printErr( e );
}
if ( expr == true ) {
// edit XML node
}
There is supposedly a way for the count expression to return a Boolean = true . How do I get this?
Maybe something like this:
where n is a Node/Element from your document
First prints 5.0, second prints true