0

I'm using a for-each in my XSLT template.

This is my example input XML:

<products>
  <data>
    <label_1>some_label1</label_1>
    <label_2>some_label2</label_2>
    <values>
      <a>a</a>
      <b>b</b>
    </values>
  </data>
  <data>
    <label_1>some_label1</label_1>
    <label_2>some_label2</label_2>
    <values>
      <c>c</c>
      <d>d</d>
    </values>
  </data>
</products>

Now based on my template:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http:/example.com/ns">
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="data">
        <data>
            <xsl:variable name="values" select="values" />
            <xsl:for-each select="$values">
                <xsl:apply-templates select="@*|node()" />
            </xsl:for-each>
        </data>
    </xsl:template>
</xsl:stylesheet>

I get only <values></values> and that is ok for me.

That's my output:

<products>
  <data>
    <a>a</a>
    <b>b</b>
  </data>
  <data>
    <c>c</c>
    <d>d</d>
  </data> 
</products>

What i need in my output is namespace like this:

<products>
  <data>
    <ns:a>a</ns:a>
    <ns:b>b</ns:b>
  </data>
  <data>
    <ns:c>c</ns:c>
    <ns:d>d</ns:d>
  </data> 
</products>

So what i understand is "each element of values is applied by template". How can I add namespace ?

Tomas
  • 61
  • 13

2 Answers2

1

You can get output similar to what you show (albeit well-formed) by using:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="data">
    <xsl:copy>
        <xsl:apply-templates select="values/*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="values/*">
    <xsl:element name="ns:{local-name()}" namespace="http:/example.com/ns">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

Or, if you prefer:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http:/example.com/ns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/products">
    <products>
        <xsl:for-each select="data">
            <xsl:copy>
                <xsl:for-each select="values/*">
                    <xsl:element name="ns:{local-name()}">
                        <xsl:value-of select="."/>
                    </xsl:element>
                </xsl:for-each>
            </xsl:copy>
        </xsl:for-each>
    </products>
</xsl:template>

</xsl:stylesheet>

Replace http:/example.com/ns with your own namespace URI.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

Credits

This answer follows the technique used in this SO answer to a similar problem.

Solution

Add namespace information to all descendants of specific elements. Augment the stylesheet by a template matching this set of nodes:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns="http://my.ns.uri"
>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="data">
        <data>
            <xsl:variable name="values" select="values" />
            <xsl:for-each select="$values">
                <xsl:apply-templates select="@*|node()" />
            </xsl:for-each>
        </data>
    </xsl:template>

    <!--
        Added template.
    -->
    <xsl:template match="data//*">
        <xsl:element name="ns:{name()}" namespace="http://my.ns.uri">
            <xsl:for-each select=".">
                <xsl:apply-templates select="@*|node()" />
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
collapsar
  • 17,010
  • 4
  • 35
  • 61