xslt 计数孙容器中的节点

xslt count nodes in grandchild container

如有错误请见谅,我才开始学习XML 2天前。 我有一个以这种格式返回的 xml 文件:

<a>
  <b>
    <d></d>
    <e></d>
    <c></c>
    <c></c>
    <c></c>
    <c></c>
    <c></c>
  </b>
  <b>
    <d></d>
    <e></d>
    <c></c>
    <c></c>
    <c></c>
    <c></c>
    <c></c>
  </b>
  <b>
    <d></d>
    <e></d>
    <c></c>
    <c></c>
    <c></c>
    <c></c>
    <c></c>
  </b>
</a>

我需要统计有多少<c>个节点,但我只想统计第一个<b>个容器,其余的都是always 第一个 <b> 组的精确重复,<c> 的数据不同。架构需要它。

这是我现在拥有的:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <select name="shipping_options" id="shipping_options">
        <xsl:for-each select="/RatingServiceSelectionResponse/RatedShipment">
          <xsl:choose>
            <xsl:when test="Service/Code = 01">
                <option value="01">UPS Next Day Air - $<xsl:value-of select="format-number(TotalCharges/MonetaryValue, '###,###.##')"/>
                </option>
            </xsl:when>
            <xsl:when test="Service/Code = 02">
                <option value="02">UPS 2nd Day Air - $<xsl:value-of select="format-number(TotalCharges/MonetaryValue, '###,###.##')"/>
                </option>
            </xsl:when>
            <xsl:otherwise>
                <option>No Data</option>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
    </select><br />
    <xsl:value-of select="count(RatedShipment/RatedPackage)"/> total boxes.
</xsl:template>
</xsl:stylesheet>

据我所知,我认为这个 <xsl:value-of select="count(RatedShipment/RatedPackage)"/> total boxes. 应该有效,但它返回 0。

谢谢!

I need to count how many <c> nodes there are, but I only want to count in the first <b> container

以下 XSLT-1.0 样式表实现了这一点。
它计算第一个 b 元素中 C 元素的出现次数:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="//b[1]">
      <CountOfC>
        <xsl:value-of select="count(c)" />
      </CountOfC>
    </xsl:template>

</xsl:stylesheet>

顺便说一句:您发布的样式表似乎与您发布的 XML (???)

无关