2012-12-13 145 views
0

我目前正在进行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
<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> 
+0

它的工作。我对此感到困惑: – macnur