Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL to Copy root node into + add attributes

Tags:

copy

xml

xslt

I am new user to XSLT and have been struggling with this problem.

Source XML:

<ABC X="" Y="" Z=""/>

Result XML:

<CDE F="">
<ABC X="" Y="" Z"" G=""/>
</CDE>

Thus I need to

  • Create a root node with an attribute with a default value in the result xml.
  • Copy node ( source has one node only) from source to result xml.
  • Add additional attributes to node copied from source xml.

I am able to do these separately but I am not able to do all of these in one XSLT.

like image 519
Jules Avatar asked Nov 04 '22 19:11

Jules


1 Answers

Given your assumptions, seems you need one minimal template:

<xsl:template match="ABC">
 <CDE F="">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:attribute name="G">hello</xsl:attribute>
   </xsl:copy>
 </CDE>
</xsltemplate>

or, if you prefer:

<xsl:template match="/">
 <CDE F="">
  <xsl:apply-templates select="ABC"/>
 </CDE>
</xsl:template>

<xsl:template match="ABC">
   <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:attribute name="G">hello</xsl:attribute>
   </xsl:copy>
</xsl:template>
like image 146
Emiliano Poggi Avatar answered Nov 15 '22 05:11

Emiliano Poggi