Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] namespaces and the not() function

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Regier Avery J <RegierAveryJ AT JohnDeere.com>
  • To: XOM API for Processing XML with Java <xom-interest AT lists.ibiblio.org>
  • Subject: [XOM-interest] namespaces and the not() function
  • Date: Fri, 4 May 2012 08:20:55 -0500

Here's as simple a test case as I can muster.

import java.io.IOException;
import java.io.StringReader;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Nodes;
import nu.xom.ParsingException;
import nu.xom.ValidityException;
import nu.xom.XPathContext;

import org.junit.Test;
import static org.junit.Assert.assertEquals;


public class ERHTest {
// This test succeeds
@Test
public void testNot() throws ValidityException, ParsingException,
IOException {
Document doc = new Builder().build(new StringReader("<hi/>"));
Nodes query = doc.query("//*[not(self::human)]",
XPathContext.makeNamespaceContext(doc.getRootElement()));
assertEquals(1, query.size());
}

// This test fails with
// nu.xom.XPathException: XPath error: No Such Function
{there://mr.space/alien}:not
@Test
public void testNotWithNamespace() throws ValidityException,
ParsingException, IOException {
Document doc = new Builder().build(new StringReader("<hi
xmlns='there://mr.space/alien'/>"));
XPathContext context =
XPathContext.makeNamespaceContext(doc.getRootElement());
context.addNamespace("a",
doc.getRootElement().getNamespaceURI());
Nodes query = doc.query("//*[not(self::a:human)]", context);
assertEquals(1, query.size());
}

// This test succeeds
@Test
public void testNotWithNamespaceWorkaround() throws
ValidityException, ParsingException, IOException {
Document doc = new Builder().build(new StringReader("<hi
xmlns='there://mr.space/alien'/>"));
XPathContext context =
XPathContext.makeNamespaceContext(doc.getRootElement());
context.addNamespace("a",
doc.getRootElement().getNamespaceURI());
context.addNamespace("f", "");
Nodes query = doc.query("//*[f:not(self::a:human)]", context);
assertEquals(1, query.size());
}
}

nu.xom.XPathException: XPath error: No Such Function
{there://mr.space/alien}:not
at nu.xom.Node.query(Unknown Source)
at ERHTest.testNotWithNamespace(ERHTest.java:30)
<snip>
Caused by: nu.xom.jaxen.UnresolvableException: No Such Function
{there://mr.space/alien}:not
at nu.xom.jaxen.SimpleFunctionContext.getFunction(Unknown Source)
at nu.xom.jaxen.ContextSupport.getFunction(Unknown Source)
at nu.xom.jaxen.Context.getFunction(Unknown Source)
at nu.xom.jaxen.expr.DefaultFunctionCallExpr.evaluate(Unknown Source)
at nu.xom.jaxen.expr.DefaultPredicate.evaluate(Unknown Source)
at nu.xom.jaxen.expr.PredicateSet.applyPredicate(Unknown Source)
at nu.xom.jaxen.expr.PredicateSet.evaluatePredicates(Unknown Source)
at nu.xom.jaxen.expr.DefaultNameStep.evaluate(Unknown Source)
at nu.xom.jaxen.expr.DefaultLocationPath.evaluate(Unknown Source)
at nu.xom.jaxen.expr.DefaultAbsoluteLocationPath.evaluate(Unknown
Source)
at nu.xom.jaxen.expr.DefaultXPathExpr.asList(Unknown Source)
at nu.xom.jaxen.BaseXPath.selectNodesForContext(Unknown Source)
at nu.xom.jaxen.BaseXPath.selectNodes(Unknown Source)
at nu.xom.JaxenConnector.selectNodes(Unknown Source)
... 24 more

While debugging I can see that the SimpleFunctionContext object has a
function list that includes not, but it has an empty namespace. It appears
the namespace of the root node of the XML document doesn't match the
namespace of the 'not' function, resulting in the 'not' function not being
found.

Why should I have to declare a namespace for functions? Does the XPath spec
require this?

Would specifying a namespace for the not() function work across other XPath
implementations?

Avery J. Regier
RegierAveryJ AT JohnDeere.com



Archive powered by MHonArc 2.6.24.

Top of Page