xsl-list
[Top] [All Lists]

RE: Namespaces and template matching

2002-11-08 02:14:44
I have the following stylesheet which renames any 'x' element to 'y':

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
   <xsl:template match="x">
      <xsl:element name="y">
         <xsl:apply-templates select="@*"/>
         <xsl:apply-templates/>
      </xsl:element>
   </xsl:template>
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*"/>
         <xsl:apply-templates/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

But when a namespace is placed on the root node, it ceases to work:

<?xml version="1.0" encoding="UTF-8"?>
<a xmlns="b">
   <x a="1" b="2" c="3" d="4" />
</a>

becomes:

<?xml version="1.0" encoding="UTF-8"?>
<a xmlns="b">
   <x a="1" b="2" c="3" d="4"/>
</a>

Why doesn't the second document translate properly?

Because it doesn't contain an <x> element, it contains a <{b}x> element.

Declare the namespace in the stylesheet with an explicit prefix p, and
change the template rule to match="p:x".

This is a FAQ.

Michael Kay
Software AG
home: Michael(_dot_)H(_dot_)Kay(_at_)ntlworld(_dot_)com
work: Michael(_dot_)Kay(_at_)softwareag(_dot_)com 



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



<Prev in Thread] Current Thread [Next in Thread>