XML : Add a root node and child to root node to XML using XSLT as well as output transformed xml

I am trying to add two nodes to the outside of some xml and output the transformed result to an xml file. I have not been able to get it to work. My xml would be:

  <message>      ...other nodes and elements in here, not always consistent  </message>    

Below is the xsl that I have tried but does not seem to work. Any ideas on what I should do to add the two nodes 'rootNode' and 'submessage' and output the results?

  <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">          <xsl:output indent="yes" method="xml" />      <xsl:variable name="root" select="/" />      <xsl:variable name="filename" select="'testOutPut'" />    <xsl:template match="/">              <xsl:result-document href="{$filename}.xml" method="xml">          <rootNode>              <submessage>                  <message>                      <xsl:apply-templates select="@*|node()"/>                  </message>              </submessage>          </rootNode>      </xsl:result-document>  </xsl:template>    <xsl:template match="DefaultNodes/*">             <xsl:variable name="sourceNode" select="$root/message/*[name() = name(current())]" />      <xsl:choose>                      <xsl:when test="$sourceNode">              <xsl:copy-of select="$sourceNode" />          </xsl:when>                      <xsl:otherwise>              <xsl:copy-of select="." />          </xsl:otherwise>                  </xsl:choose>         </xsl:template>    <xsl:template match="/rootNode">      <Request xmlns="urn:NameSpace-Definition-Message"           xmlns:i="http://www.w3.org/2001/XMLSchema-instance">                       <xsl:apply-templates select="@*|node()"/>                 </Request>  </xsl:template>    </xsl:stylesheet>    

No comments:

Post a Comment