본문 바로가기

IT/XSL

xsl에서 노드를 카피하면서 attribute의 값을 바꾸기

if you want to override a template and change its attribute , you can do this.  Here in this example, you are changing @weight value to '2'. 

###xml
<xsl:template match="/">
  <xsl:apply-templates select="*" mode="copy" />
</xsl:template>
<xsl:template match="content[@name='xxx']" mode="copy">
  <xsl:copy>
    <xsl:copy-of select="@*" />  <!--copy @* here -->
    <xsl:attribute name="name">attachment</xsl:attribute> <!-- override here-->
    <xsl:attribute name="weight">2</xsl:attribute>
    <xsl:apply-templates select="* | text() | comment()" mode="copy" />  <!-- don't copy @* here, it will reuse input @ -->
  </xsl:copy>
</xsl:template>
<xsl:template match="@* | text() | comment()" mode="copy">
  <xsl:copy />
</xsl:template>
<!-- Default action, keep recursing and copying -->
<xsl:template match="*" mode="copy">
  <xsl:copy>
    <xsl:apply-templates select="@* | * | text() | comment()" mode="copy" />
  </xsl:copy>
</xsl:template>