计算 XSLT 中的不同项目并仅列出一次

发布于 2024-09-10 01:39:44 字数 2730 浏览 6 评论 0原文

我有以下 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', .)), '&#xA;')"/>
         <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', .)), '&#xA;')"/>
         <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷迭香的记忆 2024-09-17 01:39:44

您确实需要了解慕尼黑分组的工作原理,否则您将永远提出相同问题的各种变体。

请阅读 Jeni Tennison 的教程

这是您最新问题的解决方案:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:key name="kVarNameByVal" match="variable_name"
          use="@value"/>

 <xsl:template match=
  "variable_name[generate-id()
                =
                 generate-id(key('kVarNameByVal', @value)[1])
                ]
  ">
        <xsl:value-of select=
        "concat(@value, ' ', count(key('kVarNameByVal', @value)), '
')"/>
         <br/>
 </xsl:template>
</xsl:stylesheet>

当对提供的 XML 文档执行此转换时,会生成所需的结果:

MORTIMER 2
FRED 1

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:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:key name="kVarNameByVal" match="variable_name"
          use="@value"/>

 <xsl:template match=
  "variable_name[generate-id()
                =
                 generate-id(key('kVarNameByVal', @value)[1])
                ]
  ">
        <xsl:value-of select=
        "concat(@value, ' ', count(key('kVarNameByVal', @value)), '
')"/>
         <br/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is performed on the provided XML document, the wanted result is produced:

MORTIMER 2
FRED 1
一念一轮回 2024-09-17 01:39:44

鉴于您使用的是 XSLT 2.0,我将使用内置的 分组功能。 (对于早期版本,您可能需要研究慕尼黑分组。)

<?xml version="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-group select=".//item//variables//variable_name" group-by="@value">
                <xsl:value-of select="concat(current-grouping-key(), ' ', count(current-group()), '
')"/>
        <br/>
     </xsl:for-each-group>
 </xsl:template>
</xsl:stylesheet>

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.)

<?xml version="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-group select=".//item//variables//variable_name" group-by="@value">
                <xsl:value-of select="concat(current-grouping-key(), ' ', count(current-group()), '
')"/>
        <br/>
     </xsl:for-each-group>
 </xsl:template>
</xsl:stylesheet>
梦一生花开无言 2024-09-17 01:39:44

这将在 XSLT 1.0 中完成。

<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[not(@value=ancestor::item/preceding-sibling::item//variables//variable_name/@value)]
      ">
        <xsl:value-of select=
        "concat(@value, ' ', count(key('kValueByVal', @value)), '
')"/>
         <br/>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

我得到的输出是

MORTIMER 2
<br />FRED 1
<br />

注意,它假设了更多关于文档结构(祖先::项目位)的信息,但您应该能够从那里获取它。

This will do it in XSLT 1.0.

<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[not(@value=ancestor::item/preceding-sibling::item//variables//variable_name/@value)]
      ">
        <xsl:value-of select=
        "concat(@value, ' ', count(key('kValueByVal', @value)), '
')"/>
         <br/>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

The output I get is

MORTIMER 2
<br />FRED 1
<br />

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.

眼波传意 2024-09-17 01:39:44

你得到了你所要求的:

<xsl:for-each select=".//item//variables//variable_name/@value"> 

这意味着:对于这些属性中的每一个

分组时,你必须说:对于这些属性中的每一个都

并且,如何你知道哪些是独一无二的吗?使用慕尼黑方法:

<xsl:for-each select=".//item//variables//variable_name/@value[generate-id()
                                                               =
                                                               generate-id(key('kValueByVal',.)[1])]">

这意味着:第一个拥有该钥匙的人。

编辑:此外,当您知道输入架构时,请避免 //

编辑:现在我可以看到您更改了密钥...那么,对于您的新密钥,谁是第一个?是的! variable_name 元素:

<xsl:for-each select=".//item//variables//variable_name[generate-id()
                                                        =
                                                        generate-id(key('kValueByVal',@value)[1])]">

You get what you are asking for:

<xsl:for-each select=".//item//variables//variable_name/@value"> 

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:

<xsl:for-each select=".//item//variables//variable_name/@value[generate-id()
                                                               =
                                                               generate-id(key('kValueByVal',.)[1])]">

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:

<xsl:for-each select=".//item//variables//variable_name[generate-id()
                                                        =
                                                        generate-id(key('kValueByVal',@value)[1])]">
泪是无色的血 2024-09-17 01:39:44
<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[
                          generate-id() = 
                              generate-id(key('kValueByVal', @value)[1])]">
        <xsl:value-of select=
        "concat(./@value, ' ', count(key('kValueByVal', ./@value)), '
')"/>
         <br/>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
<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[
                          generate-id() = 
                              generate-id(key('kValueByVal', @value)[1])]">
        <xsl:value-of select=
        "concat(./@value, ' ', count(key('kValueByVal', ./@value)), '
')"/>
         <br/>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文