Flavio
Flavio

Reputation: 1

XSLT 1.0 - String to XML for each node

I need to transform the content of a specific node from string to XML, replacing this node with the resulting XML. This node can be an array. So I probably need to use for-each instruction, but I don't know how...

I've something like this XML below as example:

<?xml version="1.0" encoding="UTF-8"?>
  <NodeA><NodeB>&lt;tagA xmlns="http://www.aaa.com"&gt;&lt;tagB&gt;valor1&lt;/tagB&gt;&lt;/tagA&gt;</NodeB><NodeB>&lt;tagA xmlns="http://www.aaa.com"&gt;&lt;tagB&gt;valor2&lt;/tagB&gt;&lt;/tagA&gt;</NodeB></NodeA>

I'm using the below XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8"/>
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="//NodeB">
    <xsl:value-of select="/" disable-output-escaping="yes"/>
</xsl:template>

The result is that the string is being transformed to a XML, the NodeB is being replaced, but it is becoming duplicated, like below:

<?xml version="1.0" encoding="UTF-8"?>
<NodeA>
<tagA xmlns="http://www.aaa.com">
    <tagB>valor1</tagB>
</tagA>
<tagA xmlns="http://www.aaa.com">
    <tagB>valor2</tagB>
</tagA>
<tagA xmlns="http://www.aaa.com">
    <tagB>valor1</tagB>
</tagA>
<tagA xmlns="http://www.aaa.com">
    <tagB>valor2</tagB>
</tagA>
</NodeA>

I need the following result:

<?xml version="1.0" encoding="UTF-8"?>
<NodeA>
<tagA xmlns="http://www.aaa.com">
    <tagB>valor1</tagB>
</tagA>
<tagA xmlns="http://www.aaa.com">
    <tagB>valor2</tagB>
</tagA>
</NodeA>

If I had, for example, 3 nodes in the array, the result would be triplicated.

Upvotes: 0

Views: 688

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167426

I think instead of

<xsl:template match="//NodeB">
    <xsl:value-of select="/" disable-output-escaping="yes"/>
</xsl:template>

you want

<xsl:template match="NodeB">
    <xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>

Upvotes: 1

Related Questions