xml XSLT 计数子节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14150454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:50:25  来源:igfitidea点击:

XSLT counting child nodes

xmlxsltxpath

提问by ron tornambe

I rarely use XLST and get confusing results when I try to count child nodes in a parent node.

当我尝试计算父节点中的子节点时,我很少使用 XLST 并得到令人困惑的结果。

Edit:

编辑:

The XML is structured as follows:

XML 的结构如下:

<?xml version="1.0"?>
<Response>
  <result>
    <name>Someone</name>
    **<rating>4.5</rating>**
    <review>
      <text>Some review.</text>
    </review>
    <review>
      <text>Another review.</text>
    </review>
  </result>
  <result>
    <name>Another one</name>
    **<rating>2</rating>**
    <review>
      <text>Blah, grieve, blah.</text>
    </review>
    <review>
      <text>Blah, grrrrr, blah.</text>
    </review>
    <review>
      <text>Blah, good grrrrr, blah.</text>
    </review>
  </result>
  ...
  ...
</Response>

The template (simplified) is as follows:

模板(简化版)如下:

**<body>
      <xsl:apply-templates/>
</body>**

<xsl:template match="Response/result">
  <div class="item">
    <div class="name">
      <xsl:value-of select="name"/>
    </div>
    <xsl:if test="rating">
        <span class="review-count">
          **(<xsl:value-of select="count(review)"/>)**
        </span>
    </xsl:if>
  </div>
</xsl:template>

I do not get the correct child node count from this approach. In addition to the count(review), I tried count(descendant::review)and several xPath variations. I know I'm missing something simple - but what?

我没有从这种方法中得到正确的子节点数。除了count(review),我还尝试count(descendant::review)了几种 xPath 变体。我知道我错过了一些简单的东西 - 但什么?

回答by Dimitre Novatchev

<xsl:if test="rating">
    <span class="review-count">
      **(<xsl:value-of select="count(review)"/>)**
    </span>
</xsl:if>
<xsl:if test="rating">
    <span class="review-count">
      **(<xsl:value-of select="count(review)"/>)**
    </span>
</xsl:if>

This will never generate even a single character, because there is no ratingelement in the provided XML document. In case you remove the above conditional instruction, then the result contains the wanted count values.

这甚至不会生成单个字符,因为rating提供的 XML 文档中没有元素。如果您删除上述条件指令,则结果将包含所需的计数值。

If you indeed have ratingchild for at least some reviewelements (but failed to show this to us), then you probably also didn't show other important parts of the XML document -- such as a default namespace.

如果您确实rating至少有一些review元素的子元素(但未能向我们展示),那么您可能也没有展示 XML 文档的其他重要部分 —— 例如默认名称空间。

Update:

更新

The OP has provided a more precise XML document, that contains ratingelements.

OP 提供了更精确的 XML 文档,其中包含rating元素。

I can't repro the problem.

我无法重现这个问题

This XSLT transformation:

这个 XSLT 转换

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="Response/result">
      <div class="item">
        <div class="name">
          <xsl:value-of select="name"/>
        </div>
        <xsl:if test="rating">
            <span class="review-count">
              **(<xsl:value-of select="count(review)"/>)**
            </span>
        </xsl:if>
      </div>
    </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

当应用于提供的 XML 文档时

<Response>
  <result>
    <name>Someone</name>
      <rating>4.5</rating>
    <review>
      <text>Some review.</text>
    </review>
    <review>
      <text>Another review.</text>
    </review>
  </result>
  <result>
    <name>Another one</name>
      <rating>2</rating>
    <review>
      <text>Blah, grieve, blah.</text>
    </review>
    <review>
      <text>Blah, grrrrr, blah.</text>
    </review>
    <review>
      <text>Blah, good grrrrr, blah.</text>
    </review>
  </result>
  ...
  ...
</Response>

produces the wanted, correct result:

产生想要的、正确的结果:

  <div class="item">
   <div class="name">Someone</div>
   <span class="review-count">
              **(2)**
            </span>
</div>

<div class="item">
   <div class="name">Another one</div>
   <span class="review-count">
              **(3)**
            </span>
</div>
  ...
  ...