获取相对于节点集的前同级

发布于 2024-09-14 00:08:15 字数 166 浏览 0 评论 0原文

我按字母顺序查询并排序一堆 XML 元素,并对其应用模板,以生成按字母顺序排列的列表。我想用相同的首字母作为节点的前缀:

A
Abe
Amel
Andrew
B
Bobby
Benny
...

前一个同级轴是相对于文档的,而不是节点集。我能做些什么?

I query and sort alphabetically a bunch of XML elements, to which I apply a template, to produce an alphabetical list. I'd like to prefix the nodes with the same initial by this very initial:

A
Abe
Amel
Andrew
B
Bobby
Benny
...

The preceding-sibling axis is relative to the document, not the nodeset. What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-09-21 00:08:15

您在这里描述的是一个分组问题。在 xslt 1 中处理分组的标准方法是“Muenchian”方法。这里有一个非常详细的解释:
http://www.jenitennison.com/xslt/grouping/muenchian.html

基本思想是创建一个键来指定要分组的内容。在这种情况下,您将使用该人姓名的第一个字母创建一个与该人的节点匹配的键。

然后编写一个循环来遍历人员,并检查每个人是否是第一个匹配给定键(第一个字母)的人。如果是,那么您放入一个分组分隔符。

然后你有一个嵌套循环(或应用模板),它有一个匹配规则,仅选取具有第一个字母的节点。您可以使用 xslt:sort 对它们进行排序,然后输出它们。

如果您可以发布实际 XML 的示例(而不仅仅是您想要的输出),那么我可以为您编写一个示例样式表来解析它。

What you've described here is a grouping problem. The standard way to handle grouping in xslt 1 is the "Muenchian" method. There's a very detailed explanation about it here:
http://www.jenitennison.com/xslt/grouping/muenchian.html

The basic idea is that you create a key which specifies what you want to group by. In this case, you'd create a key matching the person's node, using the first letter of their name.

Then you write a loop which loops through the people, and checks if each one is the first one to match that given key (first letter). If it is, then you put in one of your grouping dividers.

Then you have a nested loop (or apply-templates) which has a match rule that only picks up nodes with that first letter. You can sort them using xslt:sort, and output them.

If you can post a sample of your actual XML (rather than just your desired output), then I can write you an example stylesheet to parse it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文