2015-10-06 137 views
0

我想使用xsl將xml轉換爲html,但我努力使其工作,儘管它應該非常簡單。將XML轉換爲HTML表格

輸入XML:

<EEExport Version="201510" Language="International" LanguageIsoCode="int-INT"> 
 
    <Navigation> 
 
    <Navigation Name="Navigation" NavigationID="E905C889-63EF-431E-BD74-A470685F65E0"> 
 
     <Navigation Name="Oven" NavigationID="B929861A-57D1-4C20-BE2C-0DC006F4D6CE"> 
 
     </Navigation> 
 
     <Navigation Name="Cooker" NavigationID="643B0A58-D83B-4D8F-9E97-A5C8E572B1E5"> 
 
     </Navigation> 
 
     <Navigation Name="Dishwasher" NavigationID="FAB9B544-109D-4B83-A6E4-25114E1D25BA"> 
 
     <Product ProductID="887B5B3C-F9C7-4F4A-8E0B-1FC62830C508"> 
 
      <MasterData> 
 
      <Brand>Brand1</Brand> 
 
      <ProductCode>911 424 310</ProductCode> 
 
      <Category ID="B1679547-77B2">Dish_Washer</Category> 
 
      </MasterData> 
 
      <Containers> 
 
      <Container ContainerName="STechSpec_Dish_Washer"> 
 
       <Attribute AttributeID="36214A61" Name="ModelD" >F55600IM0P</Attribute> 
 
       <Attribute AttributeID="C3C36430" Name="BarCode" >7332543317288</Attribute> 
 
       <Attribute AttributeID="0B38C70E" Name="CutleryShelves" >None</Attribute> 
 
      </Container> 
 
     </Containers> 
 
     </Product> 
 
     <Product ProductID="D4DB8281-42D7-4240-8731-220FD728481D"> 
 
      <MasterData> 
 
      <Brand>Brand2</Brand> 
 
      <ProductCode>911 536 090</ProductCode> 
 
      <Category ID="B1679547-77B2">Dish_Washer</Category> 
 
      </MasterData> 
 
      <Containers> 
 
      <Container ContainerName="STechSpec_Dish_Washer"> 
 
       <Attribute AttributeID="36214A61" Name="ModelD" >ZDT26020FA</Attribute> 
 
       <Attribute AttributeID="C3C36430" Name="BarCode" >7332543397303</Attribute> 
 
       <Attribute AttributeID="0B38C70E" Name="CutleryShelves" >Cutlery Drawer</Attribute> 
 
      </Container> 
 
      </Containers> 
 
     </Product> 
 
     </Navigation> 
 
    </Navigation> 
 
    </Navigation> 
 
</EEExport>

我只想顯示分類 '廚房' 的特定產品領域。 所需的輸出:

<html> 
 
    <body> 
 
    <table border="1"> 
 
     <tr bgcolor="#e3e3e3"> 
 
     <th>Brand</th> 
 
     <th>Product Code</th> 
 
     <th>Model D</th> 
 
     <th>Cutlery Shelves</th> 
 
     </tr> 
 
     <tr> 
 
     <td>Brand1</td> 
 
     <td>911 424 310</td> 
 
     <td>F55600IM0P</td> 
 
     <td>None</td> 
 
     </tr> 
 
     <tr> 
 
     <td>Brand2</td> 
 
     <td>911 536 090</td> 
 
     <td>ZDT26020FA</td> 
 
     <td>Cutlery Drawer</td> 
 
     </tr> 
 
    </table> 
 
    </body> 
 
</html>

當前的xsl:

<?xml version="1.0"?> 
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
 
<xsl:template match="/"> 
 
    <html> 
 
    <body> 
 
    <table border="1"> 
 
     <tr bgcolor="#e3e3e3"> 
 
     <th>Brand</th> 
 
     <th>Product Code</th> 
 
     <th>Model D</th> 
 
     <th>Cutlery Shelves</th> 
 
     </tr> 
 
     <xsl:for-each select="/EEExport/Navigation/Navigation/Navigation[@Name='Dishwasher']/Product"> 
 
     <tr> 
 
      <td><xsl:value-of select="/Masterdata/Brand"/></td> 
 
      <td><xsl:value-of select="/Masterdata/ProductCode"/></td> 
 
      <td><xsl:value-of select="/Containers/Container/Attribute[@Name='ModelD']"/></td> 
 
      <td><xsl:value-of select="/Containers/Container/Attribute[@Name='CutleryShelves']"/></td> 
 
     </tr> 
 
     </xsl:for-each> 
 
    </table> 
 
    </body> 
 
    </html> 
 
</xsl:template> 
 
</xsl:stylesheet>

很顯然,我做錯了什麼。任何幫助是極大的讚賞。

回答

1

您的XSLT有兩個問題。

首先,當您使用/啓動XPath表達式時,這表示頂級文檔節點,因此搜索將相對於該節點,而不是您當前定位的節點。

所以不是這樣:

<xsl:value-of select="/Masterdata/Brand"/> 

這樣做:

<xsl:value-of select="Masterdata/Brand"/> 

說了這麼多,這導致對第二個問題。 XML和XSLT區分大小寫。在您的XML中,您使用MasterData,但在您的XSLT中,請參閱Masterdata。因此,表達式實際上應該是這樣的:

<xsl:value-of select="MasterData/Brand"/> 

試試這個XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <html> 
    <body> 
    <table border="1"> 
     <tr bgcolor="#e3e3e3"> 
     <th>Brand</th> 
     <th>Product Code</th> 
     <th>Model D</th> 
     <th>Cutlery Shelves</th> 
     </tr> 
     <xsl:for-each select="EEExport/Navigation/Navigation/Navigation[@Name='Dishwasher']/Product"> 
     <tr> 
      <td><xsl:value-of select="MasterData/Brand"/></td> 
      <td><xsl:value-of select="MasterData/ProductCode"/></td> 
      <td><xsl:value-of select="Containers/Container/Attribute[@Name='ModelD']"/></td> 
      <td><xsl:value-of select="Containers/Container/Attribute[@Name='CutleryShelves']"/></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 
+0

謝謝蒂姆,你的XSLT的伎倆! – Mill