循环 XSLT
我有一些看起来像这样的 XML
<Data>
<MainItem>
<ItemGroup>Foo</ItemGroup>
<ItemDetails>Details</ItemDetails>
</MainItem>
<MainItem>
<ItemGroup>Bar</ItemGroup>
<ItemDetails>Details</ItemDetails>
</MainItem>
<MainItem>
<ItemGroup>Baz</ItemGroup>
<ItemDetails>Details</ItemDetails>
</MainItem>
<OtherData>
<ItemGroup>Foo</ItemGroup>
<OtherDataDetails>Blah</OtherDataDetails>
</OtherData>
<OtherData>
<ItemGroup>Bar</ItemGroup>
<OtherDataDetails>BlahBlah</OtherDataDetails>
</OtherData>
<OtherData>
<ItemGroup>Baz</ItemGroup>
<OtherDataDetails>BlahBlahBlahBlahBlah</OtherDataDetails>
</OtherData>
</Data>
我试图转换的内容与此类似:
Foo
- Details
- Blah
Bar
- Details
- BlahBlah
Baz
- Details
- BlahBlahBlahBlahBlah
使用 XSLT 1.0。
我目前正在通过执行类似于 Muenchian 方法 的操作来完成分组;但我不确定如何将标签中的数据引入我的分组中。有什么建议吗?
I've got some XML that appears like this
<Data>
<MainItem>
<ItemGroup>Foo</ItemGroup>
<ItemDetails>Details</ItemDetails>
</MainItem>
<MainItem>
<ItemGroup>Bar</ItemGroup>
<ItemDetails>Details</ItemDetails>
</MainItem>
<MainItem>
<ItemGroup>Baz</ItemGroup>
<ItemDetails>Details</ItemDetails>
</MainItem>
<OtherData>
<ItemGroup>Foo</ItemGroup>
<OtherDataDetails>Blah</OtherDataDetails>
</OtherData>
<OtherData>
<ItemGroup>Bar</ItemGroup>
<OtherDataDetails>BlahBlah</OtherDataDetails>
</OtherData>
<OtherData>
<ItemGroup>Baz</ItemGroup>
<OtherDataDetails>BlahBlahBlahBlahBlah</OtherDataDetails>
</OtherData>
</Data>
What I'm trying to transform is something similar to this:
Foo
- Details
- Blah
Bar
- Details
- BlahBlah
Baz
- Details
- BlahBlahBlahBlahBlah
using XSLT 1.0.
I'm currently accomplishing the grouping by doing something similar to the Muenchian method; but I'm not sure how to bring in the data from the tags into my grouping. Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的操作:
我为您设置了一个工作示例。
Try something like this:
I set up a working example for you.
以下分组解决方案不使用循环并处理
ItemGroup
后面的任何其他同级元素。此外,仅使用基于MainItem
的小密钥来识别组。Saxon 6.5.5 下的 XSLT 1.0
生成文本:
应用于您的输入,生成:
生成 XML 输出:
生成:
The following grouping solution does not use loops and take care of any other sibling element following the
ItemGroup
. Moreover only a small key based onMainItem
is used to identify the groups.XSLT 1.0 under Saxon 6.5.5
Producing text:
Applied on your input, produces:
Producing XML output:
produces:
这是一个非常简短且最有效的转换,仅使用模板和键:
当应用于提供的 XML 文档时:
生成所需的正确结果:< /strong>
** 说明**:
慕尼黑分组 获取
ItemGroup
的不同值。Key 用于索引所有非
ItemGroup 元素
通过其
ItemGroup
同级元素。匹配任何文本节点的空模板,以防止内置 XSLT模板输出任何文本节点。
Here is a very short and most efficient transformation that uses only templates and keys:
When applied on the provided XML document:
the wanted, correct result is produced:
** Explanation**:
Muenchian grouping to get the distinct values of
ItemGroup
.Key used to index all non-
ItemGroup
elements by theirItemGroup
sibling.Empty template matching any text node to prevent the built-in XSLT template to output any text node.