在属性上的兄弟姐妹之前的XSLT和

时间:2012-03-02 19:45:03

标签: xml xslt

我正在尝试将特定属性的所有先前值相加。我认为使用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

我可能错过了一些非常明显的东西......但我不能把手指放在上面。 提前谢谢......

1 个答案:

答案 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文档应用此转换时(已更正为正确):

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