[xsl] XPathAPI: How to associate namespaces

Subject: [xsl] XPathAPI: How to associate namespaces
From: Torsten Schlabach <TSchlabach@xxxxxxx>
Date: Tue, 6 May 2003 14:11:38 +0200 (MEST)
Dear list,

I found the recent posting attached, but the thread ended there. This is
about Xalan-J 2.x, so I am not too sure if it belongs here, but I'll try.

Q: I have a document with the root element being in a default namespace:

<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns="http://abc.com/abc/abc";;>
    <Type>memo</Type>
    <Type>rep</Type>
    <Link name="xyz">123</Link>
</Report>

I would like to extract just the Link from that document trying to use 

XPathAPI.selectNodeList(myDoc,"/report/link")

will return an empty result set.

Using

XPathAPI.selectNodeList(myDoc,"/abc:report/abc:link")

will result in a transformer exception telling me that the namespace abc
needs to be assiciated somewhere.

Where can I do that? Inside the XPath expression? I read the spec; it look
to me as if XPointer has a syntax for that while XPath hasn't. But I could
not
find any method to call in order to do this either.

Torsten


Your <Report> element is in a namespace. You are going to have to
specify this namespace in your XPath expression, using a prefix such as
/xx:Report. I don't know the XPathAPI in detail, but you will find it
provides some mechanism to associate the prefix xx with the namespace
http://abc.com/abc/abc

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 

> -----Original Message-----
> From: Steve Ge [mailto:ges@xxxxxxxxxx] 
> Sent: 04 October 2002 17:17
> To: P2P_XSLT
> Subject: [xslt] Problem with XPathAPI
> 
> 
> Has anyone tried XPathAPI class?   I'm having trouble with the 
> selectNodeList() method returning an empty NodeList after a 
> valid XPath 
> search.
> 
> After parsing the xml file, I used XPathAPI to get a 
> NodeList.   But no 
> matter what xpath I used, the returned NodeList is always 
> emtpy (not null 
> though).  The call to NodeList.getLength() returns 0, and the 
> NodeLiist.item(0) is null.   
> 
> I've tried the example in the book "Java XML 
> Programmer's Reference", p676, XPathQuery.java.  It failed 
> with the same 
> symptons.   I figure this might be a setup / jar conflict 
> problem.   I am 
> using Xcerces for parsing, and Xalan for XPath/XSLT, and I am 
> running a 
> jsp on the WebLogic 6.1 server.
> 
> Any help would be appreciated.
> 
> 
> The code is below: 
> ==============================================================
> ==========
> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
> Document doc = factory.newDocumentBuilder().parse(sFilePath);
> 
> String expr = "/Report/Type/text()";
> NodeList list = XPathAPI.selectNodeList(doc, expr, doc);
> Node node =  list.item(0);
> 
> 
> The xml file is below: 
> ==============================================================
> ==========
> <?xml version="1.0" encoding="UTF-8"?>
> <Report xmlns="http://abc.com/abc/abc";;>
>     <Type>memo</Type>
>     <Type>rep</Type>
>     <Link name="xyz">123</Link>
> </Report>




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread