我正在尝试对特定属性的所有先前值求和。我认为使用 sum 和 previous-sibling 就可以解决问题。我得到的全是零。这是 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>
XML 如下所示:
<?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>
预期输出:
Field Width Position
TransactionCode 2 0
ZipCodeSendingProgram 6 2
ZipCodeSendingProgramEOJ 3 8
ZipCodeError 2 11
实际输出:
Field Width Position
TransactionCode 2 0
ZipCodeSendingProgram 6 0
ZipCodeSendingProgramEOJ 3 0
ZipCodeError 2 0
我可能错过了一些非常明显的东西......但我无法指出它。 提前致谢...
最佳答案
只需更改:
<td><xsl:value-of select="sum(preceding-sibling::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>
这是完整的更正代码:
<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>
现在,当此转换应用于提供的 XML 文档时(已更正为格式正确):
<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>
产生了想要的正确结果:
<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>
关于xml - XSLT 对属性上的前同级求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9539411/