Dave,
Thank you for your answer.

I did notice that it was a FAQ, but the FAQ is talking about
stylesheets, while I'm interested only in using the Xpath API in my code
to analyze a SOAP response.

I succeeded extracting the text I want using expressions like you have
suggested:
/*[local-name() = 'myLocalName' and namespace-uri()='myNamespaceURI']

However, I'm not sure it is the most efficient way of extracting the
text.
I would still like to use the specific node name instead of '*'.

According to the FAQ and other places, you can add the namespace
definition to the stylesheet, and "inform" the stylesheet about the
namespaces used in your xml document.
I wonder if you can do the same using the Xpath API. 
For example, in my test, if I could "teach" the API that prefix 'sv' is
'http://www.SigValue.com/SVCrm', then I could write the expression:
/soap:Envelope/soap:Body/sv:CheckCustomerPINResponse/sv:CheckCustomerPIN
Result/text()

But actually, even the 'soap' prefix is used here only because the
document defines it as the prefix of
"://schemas.xmlsoap.org/soap/envelope/". In real life, I would not be
able to tell if the soap server defines it as "soap" or maybe "s" or
"soap12" etc. The only sure thing is the namespace uri and the local
node name.

So now I have 3 options:
1) Use the API with expression containing '*' and local-name() and
namespace-uri() functions (not efficient???)
2) Use the API with expression containing myPrefix:myNodeName, but
"teach" the API first what is the namespace uri of 'myPrefix' (not sure
it's possible)
3) Use a stylesheet instead of the Xpath API. I will create the
stylesheet dynamically in my code with all the known namespaces uri and
prefixes. And then extract the text using xsl. (not sure that the result
of xsl can be a simple text and not xml, and also not sure it is more
efficient then the first method).

What do you think is the best option? Is option 2 feasible at all?

Regards,
Ori.

 

-----Original Message-----
From: David Bertoni [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, October 17, 2006 5:49 PM
To: xalan-c-users@xml.apache.org
Subject: Re: extracting a node from a document containing default
namespace

Ori Doolman wrote:
> Hello,
> I'm quite new to Xalan and Xpath, and I have a problem extracting a
node
> text from an XML document.
> I'm trying to extract the text node of CheckCustomerPINResult in the
> following document:
> 
> <?xml version="1.0" encoding="utf-8"?>
> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"; 
>               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
>               xmlns:xsd="http://www.w3.org/2001/XMLSchema";> 
>       <soap:Body>
>               <CheckCustomerPINResponse
> xmlns="http://www.SigValue.com/SVCrm";>
>       
> <CheckCustomerPINResult>xyz</CheckCustomerPINResult>
>                       <ErrCode>1234</ErrCode>
>               </CheckCustomerPINResponse>
>       </soap:Body>
> </soap:Envelope>
> 
> I'm trying the following test using the supplied example:
> 
> SimpleXPathAPI.exe d:\response.xml /
>
/soap:Envelope/soap:Body/CheckCustomerPINResponse/CheckCustomerPINResult
> /text()
> 
> But I get no output.
> 
> I noticed that if I remove the default namespace from
> CheckCustomerPINResponse or even if I use a prefix like
> <CheckCustomerPINResponse
> xmlns:myprefix="http://www.SigValue.com/SVCrm";> then I do get the
right
> value 'xyz'.
> 
> I tried reading the mailing lists and searched the internet, but still
> didn't find a way to extract the text() when the default namespace
> exist.
> The only suggested solutions are changing the document to have a
prefix.
> This is something I cannot do, because the document is not generated
by
> me. It is a response from a soap server.

This is a FAQ:

http://xml.apache.org/xalan-j/faq.html#faq-N101EE
http://www.dpawson.co.uk/xsl/sect2/N5536.html#d6682e1012

> 
> Another thing I don't understand: why should I use the 'soap' prefix
at
> all? For as much as I know the prefix is arbitrary and what really
> important is the URI. Therefore, I expected that the following
> expression (assuming I remove the default namespace) would work too,
but
> it doesn't.

In an XPath expression, the prefix is _not_ arbitrary.  If you remove
the 
default namespace, it doesn't affect the elements in the SOAP namespace.

>   
>
/Envelope[namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']/B
>
ody[namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']/CheckCu
> stomerPINResponse/CheckCustomerPINResult/text()
> 
> Isn't this expression equivalent to the first one I wrote above?

No.  You need to understand namespaces:

http://www.dpawson.co.uk/xsl/sect2/N5536.html

If you really want to use the namespace-uri() function, you could use 
something like this:

/*[local-name() = 'Envelope' and 
namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']/*[local-nam
e() 
= 'Body' and 
namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']/CheckCu
stomerPINResponse/CheckCustomerPINResult/text()

Dave

Reply via email to