[xsl] Copy idiom and overriding namespaces: trying to be smart, finding approaches

Subject: [xsl] Copy idiom and overriding namespaces: trying to be smart, finding approaches
From: Abel Braaksma Online <abel.online@xxxxxxxxx>
Date: Wed, 26 Jul 2006 20:10:28 +0200
Dear List,

Working through some lists of examples online and in books, I found some ways of overriding the target namespace, but strangely enough, all seem to create an explicit name for elements with a namespace prefix that contains a certain namespace. I don't think that is in the true nature of namespace, even more so, because ns prefixes are just placeholders for the namespace and should not be manipulated by hand.

Actually, I am just looking for the best xslt-minded approach here. The solution below works, but I would like some expert opinion on it. Just using "copy-namespace" does not work, as the namespace fixup process will set the original namespace back.

Requirement: Let the calling program decide what the target namespace will be.
Input: any document or node
Parameters: $target-namespace, a string representing the target namespace


<!-- matches all attributes and all elements,
except for elements that have a name -->
<xsl:template match="*[local-name() = ''] | @*" >
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- matches only elements that have a name,
excluding text nodes, attributes etc -->
<xsl:template match="*[local-name() != '']" >


       <!-- assign namespace -->
       <xsl:element
            name="{local-name()}"
            namespace="{$target-namespace}">

           <xsl:apply-templates  select="@* | node()" />
       </xsl:element>
   </xsl:template>


Best regards,


Abel Braaksma
www.nuntia.nl

Current Thread