【问题标题】:XSL - Adding Values of nodes based on conditionXSL - 根据条件添加节点的值
【发布时间】:2013-02-11 20:04:43
【问题描述】:

所以基本上我需要一个循环遍历 XML 节点的函数,如果条件为真,它会将值添加到变量中。我正在汇总社交帖子,需要计算每个社交帖子中有多少在提要中。这是我的 XML:

 <feed>
   <channel>
     <sources>
       <source>
           <name>Facebook</name>
           <count>3</count>
       </source>
       <source>
           <name>Twitter</name>
           <count>2</count>
       </source>
       <source>
           <name>Twitter</name>
           <count>8</count>
        </source>
     </sources>
   </channel>
  </feed>

问题是同一个来源可以出现多次,我需要将它们加在一起。因此,对于上述 XML,我需要 10 个 twitter 计数。这是我目前所处的位置:

<xsl:variable name="num_tw">
<xsl:for-each select="feed/channel/sources/source">
  <xsl:choose>
    <xsl:when test="name, 'twitter')">
      <xsl:value-of select="count"/>
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:for-each>
</xsl:variable>

<xsl:variable name="num_fb">
<xsl:for-each select="feed/channel/sources/source">
  <xsl:choose>
    <xsl:when test="name, 'facebook')">
      <xsl:value-of select="count"/>
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:for-each>
</xsl:variable>

这不起作用,因为如果有两个 twitter 提要,它会将数字并排放置并输出“28”而不是“10”。任何帮助表示赞赏!

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    您不需要在此处使用 xsl:for-each 迭代节点。相反,您可以使用 sum 运算符。例如,您的 num_tw 变量可以像这样重写

    <xsl:variable name="num_tw" select="sum(feed/channel/sources/source[name='Twitter']/count)"/>
    

    但是,您真的想在此处硬编码您的提要名称吗?这实际上是一个“分组”问题,在 XSLT 1.0 中,您使用一种称为 Muencian Grouping 的技术来解决它。在您的情况下,您希望通过 name 元素对 source 元素进行分组,因此您可以像这样定义一个键:

    <xsl:key name="source" match="source" use="name" />
    

    然后,您查看所有 source 元素,并为给定的 name 元素选择组中的第一个元素:

    <xsl:apply-templates 
       select="feed/channel/sources/source[generate-id() = generate-id(key('source', name)[1])]" />
    

    然后,在与此匹配的模板中,您可以像这样总结计数:

    <xsl:value-of select="sum(key('source', name)/count)" />
    

    这是完整的 XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
        <xsl:key name="source" match="source" use="name" />
    
        <xsl:template match="/">
        <xsl:apply-templates select="feed/channel/sources/source[generate-id() = generate-id(key('source', name)[1])]" />
    
        </xsl:template>
    
        <xsl:template match="source">
            <source>
                <xsl:copy-of select="name" />
                <count><xsl:value-of select="sum(key('source', name)/count)" /></count>
            </source>
        </xsl:template>
    </xsl:stylesheet>
    

    当应用于您的 XML 时,将输出以下内容:

    <source>
       <name>Facebook</name>
       <count>3</count>
    </source>
    <source>
       <name>Twitter</name>
       <count>10</count>
    </source>
    

    请注意,如果您确实想了解特定提要的计数,例如“Facebook”,则最好在此处使用密钥。例如:

    <xsl:variable name="num_fb" select="sum(key('source', 'Facebook')/count)"/>
    

    【讨论】:

    • 使用 SUM() 效果很好!感谢一堆正是我需要的。他们的 Muencian Grouping 方法实际上看起来很理想,我只需要弄清楚如何使用您的示例代码来实现它。我将不得不使用“调用模板”而不是“应用模板”,因为我的 XSL 文档中有一堆模板,我只需要它来影响一个。我应该能够弄清楚,您的评论非常有见地。所以要清楚一点,我正在创建 ab XML 节点集,然后我需要遍历这些节点集以获取正确的值?
    • 由于 XSLT 是一种函数式语言,您通常应该尽量避免考虑更多的过程概念,例如“循环”。在这里,您正在选择要处理的节点,在这种情况下,是对一个值求和。
    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多