Reputation: 969
I'm trying to sum all the previous values for a specific attribute. I figured the use of sum and preceding-sibling would do the trick. I am getting all zeroes instead. Here is the XSL:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/xs:schema/xs:complexType/xs:sequence">
<html> <body> <table border="1">
<tr bgcolor="#9acd32">
<th>Field</th>
<th>Width</th>
<th>Position</th>
</tr>
<xsl:variable name="position" select="1" />
<xsl:for-each select="xs:element">
<tr>
<td><xsl:value-of select="@name"/></td>
<td><xsl:value-of select="xs:simpleType/xs:restriction/xs:maxLength/@value"/></td>
<td><xsl:value-of select="sum(preceding-sibling::xs:simpleType/xs:restriction/xs:maxLength/@value)"/></td>
</tr>
</xsl:for-each>
</table> </body> </html>
</xsl:template> </xsl:stylesheet>
The XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="fw.xsl"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="xxxxxxxxxxxxx">
<xs:sequence>
<xs:element default="" name="TransactionCode" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element default="" minOccurs="0" name="ZipCodeSendingProgram" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="6"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element default="" minOccurs="0" name="ZipCodeSendingProgramEOJ" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element default="" minOccurs="0" name="ZipCodeError" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Expected output:
Field Width Position
TransactionCode 2 0
ZipCodeSendingProgram 6 2
ZipCodeSendingProgramEOJ 3 8
ZipCodeError 2 11
Actual output:
Field Width Position
TransactionCode 2 0
ZipCodeSendingProgram 6 0
ZipCodeSendingProgramEOJ 3 0
ZipCodeError 2 0
I am probably missing something really obvious...but I can't put my finger on it. Thanks in advance...
Upvotes: 2
Views: 2243
Reputation: 243529
Just change:
<td><xsl:value-of select="sum(preceding-sibling::xs:simpleType/xs:restriction/xs:maxLength/@value)"/></td>
to:
<td>
<xsl:value-of select="sum(preceding-sibling::xs:element/xs:simpleType/xs:restriction/xs:maxLength/@value)"/>
</td>
Here is the complete corrected code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/xs:schema/xs:complexType/xs:sequence">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Field</th>
<th>Width</th>
<th>Position</th>
</tr>
<xsl:variable name="position" select="1" />
<xsl:for-each select="xs:element">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="xs:simpleType/xs:restriction/xs:maxLength/@value"/>
</td>
<td>
<xsl:value-of select="sum(preceding-sibling::xs:element/xs:simpleType/xs:restriction/xs:maxLength/@value)"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Now, when this transformation is applied on the provided XML document (corrected to be wellformed):
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="xxxxxxxxxxxxx">
<xs:sequence>
<xs:element default="" name="TransactionCode" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element default="" minOccurs="0" name="ZipCodeSendingProgram" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="6"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element default="" minOccurs="0" name="ZipCodeSendingProgramEOJ" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element default="" minOccurs="0" name="ZipCodeError" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
the wanted, correct result is produced:
<html xmlns:xs="http://www.w3.org/2001/XMLSchema">
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Field</th>
<th>Width</th>
<th>Position</th>
</tr>
<tr>
<td>TransactionCode</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>ZipCodeSendingProgram</td>
<td>6</td>
<td>2</td>
</tr>
<tr>
<td>ZipCodeSendingProgramEOJ</td>
<td>3</td>
<td>8</td>
</tr>
<tr>
<td>ZipCodeError</td>
<td>2</td>
<td>11</td>
</tr>
</table>
</body>
</html>
Upvotes: 2