计算 XSLT 中的不同项目并仅列出一次
我有以下 XML:
<assessment>
<section>
<item>
<attributes>
<variables>
<variable>
<variable_name value="MORTIMER"/>
</variable>
</variables>
</attributes>
</item>
<item>
<attributes>
<variables>
<variable>
<variable_name value="FRED"/>
</variable>
</variables>
</attributes>
</item>
<item>
<attributes>
<variables>
<variable>
<variable_name value="MORTIMER"/>
</variable>
</variables>
</attributes>
</item>
</section>
</assessment>
我有以下 XSLT 来处理该 XML:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kValueByVal" match="item//variables//variable_name"
use="@value"/>
<xsl:template match="assessment">
<xsl:for-each select="
.//item//variables//variable_name/@value
">
<xsl:value-of select=
"concat(., ' ', count(key('kValueByVal', .)), '
')"/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
它输出以下内容,这几乎就是我想要的:
MORTIMER 2
FRED 1
MORTIMER 2
它列出了每个变量名称以及每个变量出现的次数。唯一的问题是,每次变量名称出现时,它都会给出一次计数,而不是只给出一次。
这就是我想要它输出的内容:
MORTIMER 2
FRED 1
How do I修改 XSLT 代码来给我这个输出?请注意,我们使用的是 XSLT 1.0。
以下解决方案似乎应该有效,但没有输出任何内容:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kValueByVal" match="item//variables//variable_name"
use="@value"/>
<xsl:template match="assessment">
<xsl:for-each select=".//item//variables//variable_name/@value[generate-id()
=
generate-id(key('kValueByVal',.)[1])]">
<xsl:value-of select=
"concat(., ' ', count(key('kValueByVal', .)), '
')"/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I have the following XML:
<assessment>
<section>
<item>
<attributes>
<variables>
<variable>
<variable_name value="MORTIMER"/>
</variable>
</variables>
</attributes>
</item>
<item>
<attributes>
<variables>
<variable>
<variable_name value="FRED"/>
</variable>
</variables>
</attributes>
</item>
<item>
<attributes>
<variables>
<variable>
<variable_name value="MORTIMER"/>
</variable>
</variables>
</attributes>
</item>
</section>
</assessment>
I have the following XSLT to process that XML:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kValueByVal" match="item//variables//variable_name"
use="@value"/>
<xsl:template match="assessment">
<xsl:for-each select="
.//item//variables//variable_name/@value
">
<xsl:value-of select=
"concat(., ' ', count(key('kValueByVal', .)), '
')"/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
It outputs the following, which is almost what I want:
MORTIMER 2
FRED 1
MORTIMER 2
It lists each of the variable_names and how many times each occurs. The only problem is that it gives this count once for each time the variable_name occurs instead of only once.
This is what I want it to output:
MORTIMER 2
FRED 1
How do I modify the XSLT code to give me that? Note that we're using XSLT 1.0.
The following solution, which seems like it should work, outputs nothing:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kValueByVal" match="item//variables//variable_name"
use="@value"/>
<xsl:template match="assessment">
<xsl:for-each select=".//item//variables//variable_name/@value[generate-id()
=
generate-id(key('kValueByVal',.)[1])]">
<xsl:value-of select=
"concat(., ' ', count(key('kValueByVal', .)), '
')"/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您确实需要了解慕尼黑分组的工作原理,否则您将永远提出相同问题的各种变体。
请阅读 Jeni Tennison 的教程。
这是您最新问题的解决方案:
当对提供的 XML 文档执行此转换时,会生成所需的结果:
You really need to understand how the Muenchian grouping works, otherwise you'd be asking variations of the same questions forever.
Do read Jeni Tennison's tutorial.
Here is a solution for your latest question:
When this transformation is performed on the provided XML document, the wanted result is produced:
鉴于您使用的是 XSLT 2.0,我将使用内置的 分组功能。 (对于早期版本,您可能需要研究慕尼黑分组。)
Seeing as you're using XSLT 2.0, I'd use the built-in grouping features. (For earlier versions, you'd probably want to look into Muenchian grouping.)
这将在 XSLT 1.0 中完成。
我得到的输出是
注意,它假设了更多关于文档结构(祖先::项目位)的信息,但您应该能够从那里获取它。
This will do it in XSLT 1.0.
The output I get is
Note that it assumes a bit more about the document structure (the ancestor::item bit), but you should be able to take it from there.
你得到了你所要求的:
这意味着:对于这些属性中的每一个
分组时,你必须说:对于这些属性中的每一个都
并且,如何你知道哪些是独一无二的吗?使用慕尼黑方法:
这意味着:第一个拥有该钥匙的人。
编辑:此外,当您知道输入架构时,请避免
//
。编辑:现在我可以看到您更改了密钥...那么,对于您的新密钥,谁是第一个?是的!
variable_name
元素:You get what you are asking for:
Wich means: for each one of these attributes
When grouping, you must say: for each one of these one of a kind
And, how do you know wich are one of a kind? With Muenchian method:
That means: the ones been the first with that key.
EDIT: Also, avoid
//
when you know input schema.EDIT: Now I can see that you change the key... So, for you new key, who is first of a kind? Yes!
variable_name
element: