2012-03-02 73 views
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 

我可能失去了一些東西真的很明顯......但我不能把我的手指放在上面。 在此先感謝...

回答

2

只要改變

<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文檔應用(校正,以使其簡潔(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> 

想要的,正確的結果產生

 <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> 
+0

aaaaarrrghh!如此簡單,當你知道如何!非常感謝!之前的xsl:value-of我選擇@value屬性不需要引用以xs:element開始,它將sum(...)部分扔掉。 – banncee 2012-03-02 20:07:02

+0

@cbannerjee:不客氣。 :) – 2012-03-02 20:08:52