使用 xslt 从 xml 中提取唯一组
我意识到您不能在 xsl 中使用数组,并且通常执行下面的任务需要一个数组。这就是我需要的...
示例 xml 代码...
<products>
<product>
<productNumber>1</productNumber>
<productType>TypeA</productType>
</product>
<product>
<productNumber>2</productNumber>
<productType>TypeB</productType>
</product>
<product>
<productNumber>3</productNumber>
<productType>TypeA</productType>
</product>
<product>
<productNumber>4</productNumber>
<productType>TypeC</productType>
</product>
<product>
<productNumber>5</productNumber>
<productType>TypeA</productType>
</product>
</products>
上面是唯一“产品”的列表,每个产品都分配有一个“productType”,可以在整个 xml 中重复多次。我希望 xsl 为每个“productType”提取一个条目而不重复。
上面的最终结果会是这样的......
TypeA
TypeB
TypeC
而不是......
TypeA
TypeB
TypeA
TypeC
TypeA
我不能是唯一一个寻找这种功能的人。
想法?
I realize you can't use arrays in xsl and normally to do the task below it would take an array. Here's what I need...
Sample xml code...
<products>
<product>
<productNumber>1</productNumber>
<productType>TypeA</productType>
</product>
<product>
<productNumber>2</productNumber>
<productType>TypeB</productType>
</product>
<product>
<productNumber>3</productNumber>
<productType>TypeA</productType>
</product>
<product>
<productNumber>4</productNumber>
<productType>TypeC</productType>
</product>
<product>
<productNumber>5</productNumber>
<productType>TypeA</productType>
</product>
</products>
Above is a listing of unique "products" and each product is assigned a "productType" that could be repeated several times throughout the xml. I would like the xsl to pull a single entry for each "productType" without repeats.
The end result of above would be something like...
TypeA
TypeB
TypeC
And not ....
TypeA
TypeB
TypeA
TypeC
TypeA
I can't be the only one that has looked for this kind of functionality.
thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此转换:
当应用于提供的 XML 文档时:
生成所需的正确分组:
请注意:是著名的 Muenchian 分组方法 的一个示例,它是 XSLT 1.0 中最快的已知分组技术。
XSLT 2.0 解决方案:
当将此 XSLT 2.0 转换应用于所提供的 XML 文档时,会生成完全相同的、正确分组的结果。
This transformation:
when applied on the provided XML document:
produces the wanted, correct grouping:
Do note: This is an example of the well-known Muenchian method for grouping, which is the fastest known gouping technique in XSLT 1.0.
XSLT 2.0 solution:
when this XSLT 2.0 transformation is applied on the provided XML document, exactly the same, correctly-grouped result is produced.
Dimitre 让我走上了正确的道路。这是我想出的适合我的需求的代码,一个简单的分隔输出流来支持 AJAX 调用...
给定一个 xml,它有很多看起来像这样的成员...
输出看起来像这样...
Dimitre got me going on the right path. Here is the code that I came up with that worked for my needs, a simple deliniated stream of output to support an AJAX call...
Given an xml that has many many members that look something like this...
The output looks like this...