我的XML。
<List>
<Item name="B" name2="cyprus">
<ItemID value="ID_B"/>
<InnerList>
<InnerItem description="porto"><more_nested_data></more_nested_data></InnerItem>
<InnerItem description="c#"><more_nested_data></more_nested_data></InnerItem>
</InnerList>
</Item>
<Item name="B" name2="alele">
<ItemID value="ID_B"/>
<InnerList>
<InnerItem description="porto"><more_nested_data></more_nested_data></InnerItem>
<InnerItem description="c#"><more_nested_data></more_nested_data></InnerItem>
</InnerList>
</Item>
<Item name="A" name2="alele">
<ItemID value="ID_A"/>
<InnerList>
<InnerItem description="basic"><more_nested_data></more_nested_data></InnerItem>
<InnerItem description="algarve"><more_nested_data></more_nested_data></InnerItem>
</InnerList>
</Item>
</List>
我想按照 Item.name
, Item.name2
. 我还想整理一下 InnerItem
在 InnerList
由 description
属性。
理想的结果。
<List>
<Item name="A" name2="alele">
<ItemID value="ID_A"/>
<InnerList>
<InnerItem description="algarve"><more_nested_data></more_nested_data></InnerItem>
<InnerItem description="basic"><more_nested_data></more_nested_data></InnerItem>
</InnerList>
</Item>
<Item name="B" name2="alele">
<ItemID value="ID_B"/>
<InnerList>
<InnerItem description="c#"><more_nested_data></more_nested_data></InnerItem>
<InnerItem description="porto"><more_nested_data></more_nested_data></InnerItem>
</InnerList>
</Item>
<Item name="B" name2="cyprus">
<ItemID value="ID_B"/>
<InnerList>
<InnerItem description="c#"><more_nested_data></more_nested_data></InnerItem>
<InnerItem description="porto"><more_nested_data></more_nested_data></InnerItem>
</InnerList>
</Item>
</List>
我的XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="List">
<xsl:copy>
<xsl:apply-templates select="Item">
<xsl:sort select="@name"/>
<xsl:sort select="@name2"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
它的顺序是 Item
正确。如何进行分类 InnerItem
?
我可以加上这个 for-each
:
<xsl:template match="List">
<xsl:for-each select="Item">
<xsl:for-each select="InnerList">
<xsl:for-each select="InnerItem">
<xsl:sort select="@description"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
但后来我失去了我的XML结构,得到这个。
<InnerItem description="c#">
<more_nested_data/>
</InnerItem><InnerItem description="porto">
<more_nested_data/>
</InnerItem><InnerItem description="c#">
<more_nested_data/>
</InnerItem><InnerItem description="porto">
<more_nested_data/>
</InnerItem><InnerItem description="algarve">
<more_nested_data/>
</InnerItem><InnerItem description="basic">
<more_nested_data/>
</InnerItem>
任何提示什么是正确的方法感谢。
只要再加一个模板就可以了。
<xsl:template match="InnerList">
<xsl:copy>
<xsl:apply-templates select="InnerItem">
<xsl:sort select="@description"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>