XSL 唯一值键 [英] XSL unique value key

查看:32
本文介绍了XSL 唯一值键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

(XSLT 1.0).我的目标是获取一组元素 S,并生成另一个集合 T,其中 T 包含 S 中的唯一元素.并且尽可能高效地执行此操作.(注意:我不必创建包含集合的变量或类似的东西.我只需要遍历唯一的元素即可).

(XSLT 1.0). My goal is to take a set of elements, S, and produce another set, T, where T contains the unique elements in S. And to do so as efficiently as possible. (Note: I don't have to create a variable containing the set, or anything like that. I just need to loop over the elements that are unique).

示例输入和键

<!-- My actual input consists of a bunch of <Result> elements -->
<AllMyResults>
<Result>
<someElement>value</state>
<otherElement>value 2</state>
<subject>Get unique subjects!</state>
</Result>
</AllMyResults>

<xsl:key name="SubjectKey" match="AllMyResults/Result" use="subject"/>

我认为上述方法有效,但是当我使用我的密钥时,速度非常慢.下面是我如何使用我的密钥的代码.

I think the above works, but when I go to use my key, it is incredibly slow. Below is the code for how I use my key.

<xsl:for-each select="Result[count(. | key('SubjectKey', subject)[1]) = 1]">
    <xsl:sort select="subject" />
            <!-- Do something with the unique subject value -->
    <xsl:value-of select="subject" />
</xsl:for-each>

附加信息

我认为我做错了,因为它大大减慢了我的 XSL.作为一些附加信息,上面显示的代码与我的主 XSL 文件位于一个单独的 XSL 文件中.在主 XSL 中,我正在调用一个包含 xsl:key 和上面显示的 for-each 的模板.此模板的输入是一个 xsl:param 包含我的节点集(类似于上面显示的示例输入).

I believe I am doing this wrong because it slowed down my XSL considerably. As some additional info, the code shown above is in a separate XSL file from my main XSL file. From the main XSL, I am calling a template that contains the xsl:key and the for-each shown above. The input to this template is an xsl:param containing my node-set (similar to the example input shown above).

推荐答案

尝试替换

count(. | key('SubjectKey', subject)[1]) = 1

:

generate-id() = generate-id(key('SubjectKey', subject)[1])

在某些 XSLT 处理器中,后者要快得多.

In some XSLT processors the latter is much faster.

这篇关于XSL 唯一值键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆