当所有节点都显示为属性时,如何使用xslt过滤数据?

时间:2013-01-29 04:56:27

标签: xml xslt

我对xsl完全陌生。我正在尝试过滤此xml中的数据。

<SectorWeightings Currency="xxx">
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="A B||16.324500000000" Date="8/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="C||16.744400000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="C S||10.177400000000" Date="8/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="C S||8.8950000000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="E||12.841600000000" Date="8/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="E||13.551700000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="Fi S||14.157900000000" Date="8/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="Fi S||15.221200000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="H Care||12.508900000000" Date="7/31/2011" />
      <SectorWeightingsEntry Type="Sector Name||Sector Weight (%)" Value="H Care||13.198200000000" Date="8/31/2011" />
 </SectorWeightings>

以下是我用来过滤数据的代码

 <xsl:for-each select="FundPricePerf/Group[@Name=nds']/SubGroup[@Name='S']/Fund[@FundCode='xx']/SectorWeightings">
        <tr> 
         <td>     <xsl:value-of select="SectorWeightingsEntry[@Type='Sector Name||Sector Weight (%)']/@Value"/>   </td> 
        </tr>
    </xsl:for-each>

上面的代码只会填充第一行A B || 16.324500000000。

如何打印整个列表?

更新

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>


<html>
<body>
<h2> zzzzzzzzz  </h2>




    <table style="width:50%;font-size:12px;" cellspacing="0" cellpadding="0">


    <tr style="width:50%; text-align:left;background-color:E6F1F9;">  

    <th>    xxxxx   </th> 
    <th>    yyyyy     </th>      
    </tr> 



    <xsl:apply-templates 
      select="FundPricePerf/Group[@Name='xxxxx']/SubGroup[@Name='S']/Fund[@FundCode='vv']/SectorWeightings/SectorWeightingsEntry[@Type='Sector Name||Sector Weight (%)']"/>


<xsl:template match="SectorWeightingsEntry">
  <tr>
    <td>
      <xsl:value-of select="@Value"/>
    </td>
  </tr>
</xsl:template>


    </table>

</body>

</html>


</xsl:stylesheet>

1 个答案:

答案 0 :(得分:3)

XSLT的主要部分需要放在自己的模板中,SectorWeightingsEntry的模板需要与之分开,如下所示:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:template match="/">
    <html>
      <body>
        <h2> zzzzzzzzz  </h2>

        <table style="width:50%;font-size:12px;" cellspacing="0" cellpadding="0">
          <tr style="width:50%; text-align:left;background-color:E6F1F9;">
            <th>    xxxxx   </th>
            <th>    yyyyy     </th>
          </tr>

          <xsl:apply-templates
            select="FundPricePerf/Group[@Name='xxxxx']/SubGroup[@Name='S']
                    /Fund[@FundCode='vv']/SectorWeightings
                    /SectorWeightingsEntry[@Type='Sector Name||Sector Weight (%)']">
            <xsl:sort select="substring-after('||', @Value)" data-type="number"
                      order="descending" />
          </xsl:apply-templates>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="SectorWeightingsEntry">
   <xsl:if test="position() &lt;= 5">
    <tr>
      <td>
        <xsl:value-of select="format-number(substring-after(@Value, '||'), '#.#')"/>
      </td>
    </tr>
   </xsl:if>
  </xsl:template>

</xsl:stylesheet>