RE: [xsl] Problem caused by Name space

Subject: RE: [xsl] Problem caused by Name space
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Mon, 21 Oct 2002 14:38:41 -0400
[ Jeff Nester]

> I have a SOAP document that is being returned back to me that 
> I want to 
> use XSLT to format for display on my web page. I am getting the SOAP 
> content from a 3rd party and don't have control over it. Here is a 
> snippet of the SOAP xml:
> 
> <soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope";>
>   <soap:body>
>    <eraRequest xmlns="http://www.somewhere.com/era";>
....
>    </eraRequest>
>   </soap:body>
> </soap:envelope>
> 
> I am using this:
> 
> <?xml version="1.0" encoding="iso-8859-1"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
> version="1.0">
> <xsl:template match="/">
...
> </xsl:stylesheet>
> 
> Here are my questions:
> 
> 1st) The XSLT never finds anything below root. If I specify a 
> prefix in 
> the eraRequest xmlns section then everything works correctly. 
> Is there a 
> way to write the XSLT so that it doesn't need the prefix to local the 
> <data>?
> 

Xpath does not understand default namespaces (see below), but all your
eraRequest and its child elements are in the
"http://www.somewhere.com/era"; namespace.  That is why you do not get
any matches - the match expression is not specifying a namespace, and
there are no such elements that are not in any namespace.  

The standard way to work with this is to declare the namespace in the
stylesheet and to use the corresponding prefix in the match expressions
in your templates.

It is usually convenient to put the namespace declarations in the
stylesheet element, like so -

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
  xmlns:sw='http://www.somewhere.com/era' 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope"version="1.0";>

It does not matter what prefix you assign as long as it is declared for
the right namespace.   Your template match expressions should now look
something like this:

match='/soap:envelope/soap:body/sw:eraRequest/sw:parameters'

It is generally considered better to use a known path (as I have above)
rather than the form "//parameters"because the latter forces the
processor to look through the entire tree looking for "parameter"
elements, which can be slow.  This may not matter in practice for a
short document, but it is better to get into good habits early.

> 2nd) is the statement    <eraRequest 
> xmlns="http://www.somewhere.com/era";> in the xml legal? do 
> you not have 
> to put a prefix with the name space?

Yes, it is legal, and it is called the "default"namespace. The default
namespace applies to all elements that do not have an explicit prefix.
That is, it applies to the element with the declaration and to its child
elements.

Cheers,

Tom P

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


Current Thread