2012-02-16 60 views
0

是XML如何在xsl中對節點組進行排序?下面

<products> 
    <supplier> 
     <supplierid>1001</supplierid> 
     <totalprice>30</totalprice> 
     <items> 
      <item>Pen</item> 
      <price>10</price> 
     </items> 
     <items> 
      <item>Pencil</item> 
      <price>5</price> 
     </items> 
     <items> 
      <item>Bag</item> 
      <price>15</price> 
     </items> 
    </supplier> 
    <supplier> 
     <supplierid>1001</supplierid> 
     <totalprice>23</totalprice> 
     <items> 
      <item>Pencil</item> 
      <price>8</price> 
     </items> 
     <items> 
      <item>Pen</item> 
      <price>5</price> 
     </items> 
     <items> 
      <item>Bag</item> 
      <price>10</price> 
     </items>   
    </supplier> 
    <supplier> 
     <supplierid>1001</supplierid> 
     <totalprice>24</totalprice> 
     <items> 
      <item>Paper Box</item> 
      <price>7</price> 
     </items> 
     <items> 
      <item>Pen</item> 
      <price>4</price> 
     </items> 
     <items> 
      <item>Bag</item> 
      <price>13</price> 
     </items> 
    </supplier> 
    <supplier> 
     <supplierid>1002</supplierid> 
     <totalprice>26</totalprice> 
     <items> 
      <item>Sharpner Box</item> 
      <price>7</price> 
     </items> 
     <items> 
      <item>Pen</item> 
      <price>4</price> 
     </items> 
     <items> 
      <item>Bag</item> 
      <price>15</price> 
     </items> 
    </supplier> 
</products> 

我需要輸出像下面使用XSL 1.0

SupplierID  Items     TotalPrice 
1001    Pencil,Pen,Bag    23 
1001    Paper Box,Pen,Bag   24  
1002    Sharpner Box,Pen,Bag  26 
1001    Pencil,Pen,Bag    30 
  1. 總價ASC
  2. 項不同節點的一條線

希望你的幫助

回答

1

這裏是一個樣本樣式改變你的XML到HTML:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="/"> 
    <html> 
     <head> 
     <title>Example</title> 
     </head> 
     <body> 
     <xsl:apply-templates/> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="products"> 
    <table> 
     <thead> 
     <tr> 
      <th>SupplierID</th> 
      <th>Items</th> 
      <th>Total Price</th> 
     </tr> 
     </thead> 
     <tbody> 
     <xsl:apply-templates select="supplier"> 
      <xsl:sort select="totalprice" data-type="number"/> 
     </xsl:apply-templates> 
     </tbody> 
    </table> 
    </xsl:template> 

    <xsl:template match="supplier"> 
    <tr> 
     <td><xsl:value-of select="supplierid"/></td> 
     <td> 
     <xsl:apply-templates select="items/item"/> 
     </td> 
     <td><xsl:value-of select="totalprice"/></td> 
    </tr> 
    </xsl:template> 

    <xsl:template match="item"> 
    <xsl:if test="position() &gt; 1"> 
     <xsl:text>, </xsl:text> 
    </xsl:if> 
    <xsl:value-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 
0

這應該工作。您只需要更正製表符(格式)

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="text" indent="yes"/> 

<xsl:template match="/products"> 
    <xsl:text disable-output-escaping="yes">SupplierID  Items    TotalPrice 
</xsl:text> 

    <xsl:for-each select="supplier"> 
<xsl:sort order="ascending" select="totalprice/text()" /> 

<xsl:value-of select="supplierid/text()" /> 
<xsl:text disable-output-escaping="yes">   </xsl:text> 

<xsl:for-each select="items"> 
    <xsl:value-of select="item/text()" /> 
    <xsl:if test="position() != last()"> 
     <xsl:text>,</xsl:text> 
    </xsl:if> 
</xsl:for-each> 

<xsl:text disable-output-escaping="yes">  </xsl:text> 
<xsl:value-of select="totalprice/text()" /> 
<xsl:text disable-output-escaping="yes"> 
</xsl:text> 

    </xsl:for-each> 

</xsl:template> 

</xsl:stylesheet> 
相關問題