eoglasi
eoglasi

Reputation: 169

How to wrap XML tags?

I have XML with tags:

Data before ....

<table1> some data 1</table1>
<table1> some data 2 </table1>

Data after ....

How can I wrap this tags with some XSLT so I will get this:

Data before ....

<tab1><table1> some data 1</table1></tab1>
<tab1><table1> some data 2 </table1></tab1>

Data after ....

Can this be done?

Upvotes: 1

Views: 1135

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

The standard approach to this kind of thing is to start with an identity template

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

which copies the input XML to the output unchanged, except when overridden by more specicfic templates. You then define additional templates for the things you want to change - to wrap every table1 element in an extra layer of tab1 you could use

<xsl:template match="table1">
  <tab1>
    <xsl:call-template name="ident" />
  </tab1>
</xsl:template>

You will find many more examples of this technique if you search for "identity template" on Stack Overflow (or elsewhere on the web).

Upvotes: 6

Related Questions