Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSL对内部循环下的xml节点进行计数_Xml_Xslt - Fatal编程技术网

XSL对内部循环下的xml节点进行计数

XSL对内部循环下的xml节点进行计数,xml,xslt,Xml,Xslt,我的xml如下所示,我想得到我在下面尝试过的标题节点的计数,但我总是得到计数为1,因为它在for each循环下 <xsl:if test="$countryVal !='' and $countryVal = 'USA'"> <tr> <td><xsl:value-of select="title"/></td> <h3>Count of titles <xsl:value-of s

我的xml如下所示,我想得到我在下面尝试过的标题节点的计数,但我总是得到计数为1,因为它在for each循环下

    <xsl:if test="$countryVal !='' and $countryVal = 'USA'">
<tr>
      <td><xsl:value-of select="title"/></td>
      <h3>Count of titles <xsl:value-of select="count(catalog/cd/title)"/></h3>
    </tr>
    </xsl:if>
    </xsl:for-each>

<catalog>
  <cd>
    <title>USD</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>USD</title>
    <artist>Bonnie Tyler</artist>
    <country>USA</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd>
    <title>CAD</title>
    <artist>Dolly Parton</artist>
    <country>CAN</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
  <cd>
    <title>USD</title>
    <artist>Gary Moore</artist>
    <country>USA</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
</catalog>
xml共享是大xml的一部分,它共享xml片段。
有任何指针吗?

我建议您使用一个变量来保存过滤后的节点集-这样您只需要应用过滤一次:

    <xsl:if test="$countryVal !='' and $countryVal = 'USA'">
<tr>
      <td><xsl:value-of select="title"/></td>
      <h3>Count of titles <xsl:value-of select="count(catalog/cd/title)"/></h3>
    </tr>
    </xsl:if>
    </xsl:for-each>

<catalog>
  <cd>
    <title>USD</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>USD</title>
    <artist>Bonnie Tyler</artist>
    <country>USA</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd>
    <title>CAD</title>
    <artist>Dolly Parton</artist>
    <country>CAN</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
  <cd>
    <title>USD</title>
    <artist>Gary Moore</artist>
    <country>USA</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
</catalog>
<xsl:variable name="eligible-cds" select="/catalog/cd[country='USA']" />
然后你可以做:

    <xsl:if test="$countryVal !='' and $countryVal = 'USA'">
<tr>
      <td><xsl:value-of select="title"/></td>
      <h3>Count of titles <xsl:value-of select="count(catalog/cd/title)"/></h3>
    </tr>
    </xsl:if>
    </xsl:for-each>

<catalog>
  <cd>
    <title>USD</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>USD</title>
    <artist>Bonnie Tyler</artist>
    <country>USA</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd>
    <title>CAD</title>
    <artist>Dolly Parton</artist>
    <country>CAN</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
  <cd>
    <title>USD</title>
    <artist>Gary Moore</artist>
    <country>USA</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
</catalog>
<xsl:for-each select="$eligible-cds">
    <tr>
        <td><xsl:value-of select="title"/></td>
    </tr>
</xsl:for-each>
<h3>Count of titles <xsl:value-of select="count($eligible-cds)"/></h3>

您是在寻找每个节点的数量,还是整个节点的数量,还是其他什么?如果您要在整个目录中查找编号,则不需要;计数将检查节点下的所有结果。我只在CD下查找美国国家/地区的标题节点计数。如果是这种情况,您可以这样做,但此语句需要在xsl:for each后面,而不是在其中。请注意,您还可以简化xsl:for each,以避免在其中使用变量和if条件。感谢您的帮助,但我正在寻找基于循环内计数结果的数据,我需要显示表数据,例如,如果标题节点的计数大于一,则我只需要显示一个标题,因为在我的示例中,标题具有由于相同的值,标题值将被复制。我已经更新了xml,我期望的xsl逻辑如下所示,这很有帮助,但基于循环内的计数结果,我需要显示表数据。例如,如果标题节点的计数大于一,那么我只需要显示一个标题,因为在我的例子中,标题具有相同的值,因此标题值会被复制。更新的xml预期标题数量我恐怕不理解您的评论。请编辑您的问题,并提供一个示例,说明建议的方法无法提供预期的输出。-注意,您不能在xsl:for-each中计数,因为它不是一个循环。如果count不会给出所需的结果,那么如何在每个循环中获得count xml节点。还有其他函数或指针吗?示例xml是我的第一篇文章。