Re: [xsl] Newbie: template match using a mix of namespace prefix and none p refix

Subject: Re: [xsl] Newbie: template match using a mix of namespace prefix and none p refix
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 9 Sep 2002 10:09:09 +0100
Hi Magnus,

(Sorry, I read "Palmer Magnus" as meaning your first name was
Palmer...)

>> 1) Absolute path, not working
>> <xsl:template
>>
> match="/msg/req/dta/SHOW_PO_007/DATAAREA/SHOW_PO/POHEADER/of:USERAREA/of:TRA
>> NSMETHD" mode="copy">
>
> <<My guess would be that these paths aren't working because the msg,
> <<req, dta, SHOW_PO_007, DATAAREA, SHOW_PO and/or POHEADER elements are
> <<in a namespace in your XML document. Without seeing your source
> <<document I can't tell, but this seems to be the most likely
> <<explanation.
>
> The namespaces are as following:
> <xsl:stylesheet version="1.0" 
> xmlns="x-schema:http://scshost/schemas/oagis/7.1/011_show_po_007.xdr";
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:of="x-schema:http://scshost/schemas/oagis/7.1/oagis_fields.xdr"; 
> xmlns:os="x-schema:http://scshost/schemas/oagis/7.1/oagis_segments.xdr"; 
> xmlns:scala="x-schema:http://scshost/schemas/oagis/7.1/scala_fields.xdr";
>>
> Hence I asume that the default namespace is using the 011_show_po_007.xdr
> namespace.

Yes, that's the default namespace in the stylesheet. I assume that's
the default namespace in the XML document as well?

The thing is that XSLT (or rather XPath) doesn't use the default
namespace when interpreting element names in XPath. If you use the
path:

  SHOW_PO_007

then this always selects the SHOW_PO_007 element *in no namespace*, no
matter what the default namespace is at the point where you use that
path. Setting the default namespace in the stylesheet only influences
the namespace of the literal result elements that you create within
the stylesheet, it cannot be used when selecting nodes from the source
document.

You therefore have to give this default namespace a prefix, e.g.:

<xsl:stylesheet version="1.0"
  xmlns:po="x-schema:http://scshost/schemas/oagis/7.1/011_show_po_007.xdr";
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:of="x-schema:http://scshost/schemas/oagis/7.1/oagis_fields.xdr";
  xmlns:os="x-schema:http://scshost/schemas/oagis/7.1/oagis_segments.xdr";
  xmlns:scala="x-schema:http://scshost/schemas/oagis/7.1/scala_fields.xdr";>
...
</xsl:stylesheet>

Here I've assigned the prefix 'po' to stand in for the namespace
"x-schema:http://scshost/schemas/oagis/7.1/011_show_po_007.xdr";. You
can now refer to the SHOW_PO_007 element *in the namespace
"x-schema:http://scshost/schemas/oagis/7.1/011_show_po_007.xdr"* with:

  po:SHOW_PO_007

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread