2
我想总结一个特定属性的所有以前的值。我认为使用sum和before-sibling可以做到这一点。我得到所有的零。下面是XSL:XSLT之前的属性上的兄弟姐妹总和
<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
我可能失去了一些东西真的很明显......但我不能把我的手指放在上面。 在此先感谢...
aaaaarrrghh!如此简单,当你知道如何!非常感谢!之前的xsl:value-of我选择@value属性不需要引用以xs:element开始,它将sum(...)部分扔掉。 – banncee 2012-03-02 20:07:02
@cbannerjee:不客气。 :) – 2012-03-02 20:08:52