XSL 唯一值键
目标
(XSLT 1.0)。我的目标是获取一组元素 S,并生成另一组元素 T,其中 T 包含 S 中的唯一元素。并且尽可能高效地执行此操作。 (注意:我不必创建包含该集合的变量或类似的东西。我只需要循环访问唯一的元素)。
示例输入和密钥
<!-- 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"/>
我认为上面的方法有效,但是当我使用我的密钥时,速度非常慢。下面是我如何使用密钥的代码。
<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(类似于上面显示的示例输入)。
Goal
(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).
Example Input and Key
<!-- 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>
Additional Info
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从给出的信息中我看不出任何原因说明为什么代码应该很慢。值得一看的是,缓慢是否是所有 XSLT 处理器上都会发生的情况,或者是否是某个处理器所特有的。
I can't see any reason from the information given why the code should be slow. It might be worth seeing if the slowness is something that happens on all XSLT processors, or if it's peculiar to one.
尝试替换
为:
在某些 XSLT 处理器中,后者要快得多。
Try substituting
with:
In some XSLT processors the latter is much faster.