Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mimic copy-namespaces="no" in XSLT 1.0?

Tags:

xml

xslt

I want to rewrite this xslt piece in XSLT 1.0, which does not support "copy-namespaces".

<xsl:copy-of copy-namespaces="no" select="maml:alertSet/maml:alert" />

How?

like image 674
Nestor Avatar asked Jan 26 '12 22:01

Nestor


People also ask

How do you add a namespace in XSLT?

Provided that you're using XSLT 2.0, all you have to do is add the xpath-default-namespace attribute on the document element of each stylesheet: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://example.com"> ...

What is copy of in XSLT?

XSLT <xsl:copy-of> The <xsl:copy-of> element creates a copy of the current node. Note: Namespace nodes, child nodes, and attributes of the current node are automatically copied as well! Tip: This element can be used to insert multiple copies of the same node into different places in the output.

What is XSLT namespace?

The namespace http://www.w3.org/1999/XSL/Transform is referred to as "the XSLT namespace". The prefix xsl is conventionally used to refer to this namespace (and is so used both within this document and within the XSLT specification), but it has no special status: any prefix may be used.

What is mode in XSLT?

The mode attribute allows multiple ways of processing the same XML elements. A template must have a match attribute if wanting to use a mode attribute, so they are not meant for templates relying solely upon the name attribute for calling.


1 Answers

The following mimics the XSLT 2.0 construct:

Create templates in a mode that will re-construct your nodes without namespaces:

<!-- generate a new element in the same namespace as the matched element,
     copying its attributes, but without copying its unused namespace nodes,
     then continue processing content in the "copy-no-namepaces" mode -->

<xsl:template match="*" mode="copy-no-namespaces">
    <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()" mode="copy-no-namespaces"/>
    </xsl:element>
</xsl:template>

<xsl:template match="comment()| processing-instruction()" mode="copy-no-namespaces">
    <xsl:copy/>
</xsl:template>

Apply-templates for the desired element(s) in that mode:

<xsl:apply-templates  select="maml:alertSet/maml:alert" mode="copy-no-namespaces"/>
like image 60
Michael Kay Avatar answered Sep 23 '22 11:09

Michael Kay