eoglasi
eoglasi

Reputation: 169

Copy XML node with added changes to child nodes

I have one specific problem. I have XML which i would need transformed so one node gets copied.

Input XML is:

<root>
    <book>
        <name>
        ... some data
        </name>
        <info>
        ... some data
        </info>
        <trees>
            <tag1></tag1>
            <tag2></tag2>
            <tag3></tag3>
            .... other tags
            <tag n+1></tag n+1> 
        </trees>
        .
        .
        .
        other nodes
        .
        .
        .
        .
        </book>
        <book>
        .
        .
        .
        </book>
</root>

Now i would need to copy node "trees" 3 times and write it like this with little changes to sub nodes. Output XML would need to be like:

<root>
    <book>
        <name>
        ... some data
        </name>
        <info>
        ... some data
        </info>

        <trees>
            <tag1></tag1>
            <tag2></tag2>
            <tag3></tag3>
            .... other tags
            <tag n+1></tag n+1> 
        </trees>
        <treesA>
            <tag1A></tag1A>
            <tag2A></tag2A>
            <tag3A></tag3A>
            .... other tags
            <tag n+1></tag n+1> 
        </treesA>
        <treesA>
            <tag1B></tag1B>
            <tag2B></tag2B>
            <tag3B></tag3B>
            .... other tags
            <tag n+1></tag n+1> 
        </treesB>
        <treesC>
            <tag1C></tag1C>
            <tag2C></tag2C>
            <tag3C></tag3C>


    .... other tags
        <tag n+1></tag n+1> 
    </treesC>
    .
    .
    .
    other nodes
    .
    .
    .
    .
</book>
<book>
.
.
.
</book>

Thanks for all your help regarding my problem. Eoglasi

Upvotes: 0

Views: 206

Answers (2)

Ian Roberts
Ian Roberts

Reputation: 122414

This can be done quite nicely in XSLT 2.0 using a tunnel parameter to hold the suffix:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

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

  <xsl:template match="*">
    <xsl:param name="suffix" tunnel="yes" select="''" />
    <xsl:element name="{name()}{$suffix}" namespace="{namespace-uri()}">
      <xsl:apply-templates select="@*|node()" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="trees">
    <xsl:next-match />
    <xsl:next-match>
      <xsl:with-param name="suffix" tunnel="yes" select="'A'" />
    </xsl:next-match>
    <xsl:next-match>
      <xsl:with-param name="suffix" tunnel="yes" select="'B'" />
    </xsl:next-match>
    <xsl:next-match>
      <xsl:with-param name="suffix" tunnel="yes" select="'C'" />
    </xsl:next-match>
  </xsl:template>

</xsl:stylesheet>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167716

Use

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

and

<xsl:template match="trees">
  <xsl:call-template name="identity"/>
  <xsl:apply-templates select="." mode="change">
    <xsl:with-param name="suffix" select="'A'"/>
  </xsl:apply-templates>
  <xsl:apply-templates select="." mode="change">
    <xsl:with-param name="suffix" select="'B'"/>
  </xsl:apply-templates>
  <xsl:apply-templates select="." mode="change">
    <xsl:with-param name="suffix" select="'C'"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="*" mode="change">
  <xsl:param name="suffix"/>
  <xsl:element name="{name()}{$suffix}" namespace="{namespace-uri()}">
    <xsl:apply-templates select="@* | node()" mode="change">
      <xsl:with-param name="suffix" select="$suffix"/>
    </xsl:apply-templates>
  </xsl:element>
</xsl:template>

Upvotes: 0

Related Questions