Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add top level element to XML using XSLT?

Tags:

xml

xslt

I have a simple XML, that I want to add a new root to. The current root is <myFields> and I want to add <myTable> so it would look like.

<myTable>
    <myFields>
    .
    .
    </myFields>
</myTable>
like image 312
Marsharks Avatar asked Feb 26 '23 00:02

Marsharks


2 Answers

Something like this should work for you...

<xsl:template match="/">
  <myTable>
    <xsl:apply-templates/>
  </myTable>
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>
like image 112
Ryan Berger Avatar answered Feb 28 '23 12:02

Ryan Berger


This is probably the shortest solution :) :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
        <myTable> 
            <xsl:copy-of select="node()" /> 
        </myTable> 
    </xsl:template> 
</xsl:stylesheet>
like image 21
Dimitre Novatchev Avatar answered Feb 28 '23 12:02

Dimitre Novatchev