[英]XSLT 1.0 sorting by child elements value
I have tried around with this for far too long, but I am not getting my data sorted correctly. 我已经尝试了很长时间,但是我没有正确排序我的数据。 Basically I want to sort all my collectedKeys under the root element by their keyId. 基本上,我想根据根元素的keyId对所有我的collectKeys进行排序。 All other information should go unchanged into the output. 所有其他信息应保持不变,进入输出。
Input: 输入:
<?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>
Expected Output: 预期产量:
<?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>
Unfortunately my current xslt is not doing the sorting and I cannot figure out why not. 不幸的是,我当前的xslt没有进行排序,我不知道为什么不这样做。
This is the XLST I am using: 这是我正在使用的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>
Any help would be appreciated. 任何帮助,将不胜感激。
If you want to sort the collectedKeys
nodes, you must do so when you apply templates to them - not to their children. 如果要对collectedKeys
节点进行排序,则在将模板应用于它们而不是其子对象时必须这样做。 Try: 尝试:
XSLT 1.0 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.