XSL to copy root node to + add attributes

I am a new user for XSLT and have been struggling with this issue.

XML source:

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

XML Result:

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

So I need

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

I can do it separately, but I can not do it all in one XSLT.

+5
source share
2 answers

Given your assumptions, it seems you need a 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 want:

<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>
+2
source

XML (. @empo), " ". XML- . , , , .

, , :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

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

</xsl:stylesheet>

. " " XML, match="/ABC" , . , - XML, .

, XML:

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

( , , <xsl:apply-templates/> /ABC):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="/ABC">
    <CDE F="">
      <ABC G="">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
      </ABC>
    </CDE>
  </xsl:template>

</xsl:stylesheet>

:

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

All Articles