我已经尝试了很长时间,但我没有正确地对我的数据进行排序。基本上我想用keyId对根元素下的所有collectKeys进行排序。所有其他信息应保持不变为输出。
输入:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<base>
<id>a</id>
<name>b</name>
<subId>c</subId>
</base>
<key>lvl1</key>
<key>lvl3</key>
<collectedKeys>
<keyInformation>
<keyId>100</keyId>
<content>
<contentID>4</contentID>
</content>
<keyStatus>40001</keyStatus>
<initLang>false</initLang>
</keyInformation>
</collectedKeys>
<collectedKeys>
<keyInformation>
<keyId>200</keyId>
<content>
<contentID>4</contentID>
</content>
<keyStatus>40002</keyStatus>
<initLang>false</initLang>
</keyInformation>
</collectedKeys>
<collectedKeys>
<keyInformation>
<keyId>100</keyId>
<content>
<contentID>4</contentID>
</content>
<keyStatus>40003</keyStatus>
<initLang>false</initLang>
</keyInformation>
</collectedKeys>
<collectedKeys>
<keyInformation>
<keyId>300</keyId>
<content>
<contentID>4</contentID>
</content>
<keyStatus>40004</keyStatus>
<initLang>false</initLang>
</keyInformation>
</collectedKeys>
<additionalKeys>50</additionalKeys>
<additionalKeys>70</additionalKeys>
<ignoredKeys>
<keyInformation>
<keyId>500</keyId>
<content>
<contentID>3</contentID>
</content>
<keyStatus>60001</keyStatus>
<initLang>true</initLang>
</keyInformation>
</ignoredKeys>
<ignoredKeys>
<keyInformation>
<keyId>600</keyId>
<content>
<contentID>1</contentID>
</content>
<keyStatus>50001</keyStatus>
<initLang>false</initLang>
</keyInformation>
</ignoredKeys>
</root>
预期产出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<base>
<id>a</id>
<name>b</name>
<subId>c</subId>
</base>
<key>lvl1</key>
<key>lvl3</key>
<collectedKeys>
<keyInformation>
<keyId>100</keyId>
<content>
<contentID>4</contentID>
</content>
<keyStatus>40001</keyStatus>
<initLang>false</initLang>
</keyInformation>
</collectedKeys>
<collectedKeys>
<keyInformation>
<keyId>100</keyId>
<content>
<contentID>4</contentID>
</content>
<keyStatus>40003</keyStatus>
<initLang>false</initLang>
</keyInformation>
</collectedKeys>
<collectedKeys>
<keyInformation>
<keyId>200</keyId>
<content>
<contentID>4</contentID>
</content>
<keyStatus>40002</keyStatus>
<initLang>false</initLang>
</keyInformation>
</collectedKeys>
<collectedKeys>
<keyInformation>
<keyId>300</keyId>
<content>
<contentID>4</contentID>
</content>
<keyStatus>40004</keyStatus>
<initLang>false</initLang>
</keyInformation>
</collectedKeys>
<additionalKeys>50</additionalKeys>
<additionalKeys>70</additionalKeys>
<ignoredKeys>
<keyInformation>
<keyId>500</keyId>
<content>
<contentID>3</contentID>
</content>
<keyStatus>60001</keyStatus>
<initLang>true</initLang>
</keyInformation>
</ignoredKeys>
<ignoredKeys>
<keyInformation>
<keyId>600</keyId>
<content>
<contentID>1</contentID>
</content>
<keyStatus>50001</keyStatus>
<initLang>false</initLang>
</keyInformation>
</ignoredKeys>
</root>
不幸的是我当前的xslt没有进行排序,我无法弄清楚为什么不这样做。
这是我正在使用的XLST:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- -->
<xsl:template match="collectedKeys">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="keyInformation">
<xsl:sort select="keyId" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
任何帮助都将不胜感激。
答案 0 :(得分:0)
如果要对collectedKeys
节点进行排序,则必须在向其应用模板时执行此操作,而不是对其子节点应用。尝试:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates select="base | key"/>
<xsl:apply-templates select="collectedKeys">
<xsl:sort select="keyInformation/keyId" data-type="number"/>
</xsl:apply-templates>
<xsl:apply-templates select="additionalKeys | ignoredKeys"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>