2015-10-06 231 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