将element属性放入子元素的元素中

时间:2012-12-13 09:13:42

标签: xml xslt xpath

我目前正致力于重组XML。这是我的xml示例:

<SECTION SectionID = "S">
    <DATA>
        <ITEM ID="GLOBAL_DOCSTATUS_1000">
        <D>template</D>
        <E>template</E>
        <R>шаблон</R>
        <K>шаблон</K>
        </ITEM>
    </DATA>
</SECTION>

我需要将属性@SectionID作为<ITEM>标记内的元素作为新<SECTIONID>标记及其数据。

结果应如下所示:

<SECTION>
  <DATA>
    <ITEM ID="GLOBAL_DOCSTATUS_1000">
    <D>template</D>
    <E>template</E>
    <R>шаблон</R>
    <K>шаблон</K>
        <SECTIONID>S</SECTIONID> 
    </ITEM>
  </DATA>
</SECTION>

1 个答案:

答案 0 :(得分:1)

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

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

<xsl:template match="SECTION">
  <xsl:copy>
   <xsl:apply-templates select="@*[name()!='SectionID']|node()"/>
  </xsl:copy>
</xsl:template>

  <xsl:template match="SECTION[@SectionID]/DATA/ITEM">
  <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
   <SECTIONID><xsl:value-of select="../../@SectionID" /></SECTIONID> 
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
相关问题