XSLT 按属性递归排序
我发现了有关使用 XSLT 嵌套/递归排序的多个问题和答案,但无法将其映射到我的情况。
我的情况:
- “set”元素可以由 0 个或多个“property”元素组成
- “set”元素可以由 0 个或多个子集组成
- set 和 property 元素都由属性“key”组成
- 我想对“set”进行排序“key”属性,以及每个“set”:按“properties”元素的“key”排序
XML 看起来像:
<set key="...">
<property key="..."/>
<property key="..."/>
<property key="..."/>
<set key="...">
<set key="...>
<property key="..."/>
<property key="..."/>
<property key="..."/>
</set>
</set>
</set>
建议?
I found multiple questions and answers regarding nested/recursive sort using XSLT, but was not able to map it on my situation.
My situation:
- "set" element can consist of 0 or more "property" elements
- a "set" element can consist of 0 or more subsets
- both set and property elements consist of attribute "key"
- I want to sort the "set"s by "key" attribute, and per "set": sort by "key" of "properties" elements
The XML looks something like:
<set key="...">
<property key="..."/>
<property key="..."/>
<property key="..."/>
<set key="...">
<set key="...>
<property key="..."/>
<property key="..."/>
<property key="..."/>
</set>
</set>
</set>
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在简单的情况下,可以这样实现:
应用于此 XML:
结果将是:
我猜现实生活中的应用程序更复杂,但您的示例没有显示这一点。
In simple situations it can be achieved like this:
Applied to this XML:
Result will be:
I guess real-life app is more complex, but your example doesn't show that.
就我个人而言,我认为 XSL-T 不适合这类事情。它是一种 XML 转换语言,而不是一种用于排序的编程语言。
我认为最好使用正确的工具来完成这项工作:使用 Java 或 C# 等语言以排序形式生成 XML,然后使用 XSL-T 对其进行转换。
如果您正在为此苦苦挣扎,您可能应该重新考虑该方法。
Personally, I don't think XSL-T is suited for this sort of thing. It's an XML transformation language, not a programming language for sorting.
I think it'd be better to use the right tool for the job: Use a language like Java or C# to produce the XML in sorted form, then use XSL-T to transform it.
If you're struggling with it, you probably should re-think the approach.