Reputation: 113
Please excuse any mistakes, I only started learning XML 2 days ago. I have an xml file that gets returned in this format:
<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>
I need to count how many <c>
nodes there are, but I only want to count in the first <b>
container, the rest are always exact repeats of the first <b>
set with different data for <c>
. The schema requires it.
Here is what I have right now:
<?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>
From what I can find, I think this <xsl:value-of select="count(RatedShipment/RatedPackage)"/> total boxes.
should be working, but it's returning 0.
Thank you!
Upvotes: 1
Views: 197
Reputation: 29022
I need to count how many
<c>
nodes there are, but I only want to count in the first<b>
container
The following XSLT-1.0 stylesheet achieves just that.
It counts the occurrences of the C
elements in the first b
element:
<?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>
BTW: Your posted stylesheet seems to be in no relation your posted XML (???)
Upvotes: 1