You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Jim Maskus <jm...@collegenet.com> on 2003/09/24 22:54:49 UTC

XpathAPI and namespaces

Source
<dsl:select-list-set xmlns:dsl="http://www.applyweb.com/dsl">
	<dsl:select-list name="MAJOR" id="Major" />
	<dsl:select-list name="DEGREE" id="Degree" />
</dsl:select-list-set>

I'm trying to query a DOM tree in Java as follows...

// Set up a DOM tree to query.
InputSource in = new InputSource(new FileInputStream(inFile));
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
Document doc = dfactory.newDocumentBuilder().parse(in);
		
Node n1 = XPathAPI.selectSingleNode(doc, "//name");
Node n2 = XPathAPI.selectSingleNode(doc, "//address");

The call to selectSingleNode() code works until I look for an element 
with a namespace which results in the following error...
Node n3 = XPathAPI.selectSingleNode(doc, "//dsl:select-list-set");
Exception in thread "main" javax.xml.transform.TransformerException: 
Prefix must resolve to a namespace: dsl

How do I declare and pass the method selectSingleNode() a namespace 
node as a third parameter.
Jim


Re: XpathAPI and namespaces

Posted by Joaquín Sánchez Jiménez <qu...@fi2net.com>.
Hi:

Try this:


                final Element java =
doc.getOwnerDocument().createElement("DslContext");
                java.setAttribute("xmlns:dsl",
"http://www.applyweb.com/dsl");

                // for eval function
                XPathAPI.eval(node, xpath_expr, new
PrefixResolverDefault(java));

                // for select*** function
                XPathAPI.selectSingleNode(node, xpath_expr, java);

Bye.



----- Original Message -----
From: "Jim Maskus" <jm...@collegenet.com>
To: <xa...@xml.apache.org>
Sent: Wednesday, September 24, 2003 10:54 PM
Subject: XpathAPI and namespaces


> Source
> <dsl:select-list-set xmlns:dsl="http://www.applyweb.com/dsl">
> <dsl:select-list name="MAJOR" id="Major" />
> <dsl:select-list name="DEGREE" id="Degree" />
> </dsl:select-list-set>
>
> I'm trying to query a DOM tree in Java as follows...
>
> // Set up a DOM tree to query.
> InputSource in = new InputSource(new FileInputStream(inFile));
> DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
> dfactory.setNamespaceAware(true);
> Document doc = dfactory.newDocumentBuilder().parse(in);
>
> Node n1 = XPathAPI.selectSingleNode(doc, "file://name");
> Node n2 = XPathAPI.selectSingleNode(doc, "file://address");
>
> The call to selectSingleNode() code works until I look for an element
> with a namespace which results in the following error...
> Node n3 = XPathAPI.selectSingleNode(doc, "file://dsl:select-list-set");
> Exception in thread "main" javax.xml.transform.TransformerException:
> Prefix must resolve to a namespace: dsl
>
> How do I declare and pass the method selectSingleNode() a namespace
> node as a third parameter.
> Jim
>
>