XSLT中独立于深度的不同项目计数 [英] Counting distinct items in XSLT independent of depth
问题描述
如果我运行以下XSLT代码:
If I run the following XSLT code:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kValueByVal" match="variable_name/@value"
use="."/>
<xsl:template match="assessment">
<xsl:for-each select="
/*/*/variable/attributes/variable_name/@value
[generate-id()
=
generate-id(key('kValueByVal', .)[1])
]
">
<xsl:value-of select=
"concat(., ' ', count(key('kValueByVal', .)), '
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
在以下XML上:
<assessment>
<variables>
<variable>
<attributes>
<variable_name value="FRED"/>
</attributes>
</variable>
</variables>
<variables>
<variable>
<attributes>
<variable_name value="MORTIMER"/>
</attributes>
</variable>
</variables>
<variables>
<variable>
<attributes>
<variable_name value="FRED"/>
</attributes>
</variable>
</variables>
</assessment>
我得到所需的输出:
FRED 2
MORTIMER 1
(请参阅我的原始问题(如果需要)以获取更多信息。)
(See my original question for more info, if you wish.)
但是,如果我在此输入上运行它:
However, if I run it on this input:
<ExamStore>
<assessment>
<variables>
<variable>
<attributes>
<variable_name value="FRED"/>
</attributes>
</variable>
</variables>
<variables>
<variable>
<attributes>
<variable_name value="MORTIMER"/>
</attributes>
</variable>
</variables>
<variables>
<variable>
<attributes>
<variable_name value="FRED"/>
</attributes>
</variable>
</variables>
</assessment>
</ExamStore>
我什么也没得到。 (请注意,我只是将原始输入包装在ExamStore标记中。)我期望并希望获得相同的输出。
I get nothing. (Note that I just wrapped the original input in an ExamStore tag.) I was expecting and hoping to get the same output.
为什么不呢?如何更改原始XSLT代码以获取相同的输出?
Why don't I? How can I change the original XSLT code to get the same output?
推荐答案
好吧,选择xpath / * / * / variable / attributes / variable_name /...
不再正确,因为您在节点树中添加了更高的另一个节点。
Well your select xpath /*/*/variable/attributes/variable_name/...
is no longer correct because you added another node higher in the node-tree.
如果您想拥有真正的独立性,则需要使用类似的东西:
If you want to have true independence you need to use something like:
//variable/attributes/variable_name/...
...(开始时不是双斜线),但这是相当危险的,因为它将抓住该结构的所有出现-一定要确保这就是您的意思。
...(not the double slash at the start) but this is fairly dangerous because it will catch all occurences of that structure - be really sure that's what you mean.
否则,只需在xpath前面加上另一个 / *
Otherwise, just prepend your xpath with another /*
这篇关于XSLT中独立于深度的不同项目计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!