代码之家  ›  专栏  ›  技术社区  ›  Manny0

为什么XSL样式表显示没有XML(信息/值)的html表

  •  0
  • Manny0  · 技术社区  · 7 年前

    我试图创建一个XSL样式表,将XML代码中的值显示到HTML表中。浏览器中会显示表格标题,但没有任何值。

    xsl:for each select=“”和xsl:value of select=“”都链接到我的xml代码,我相信这是正确的,但可能这只是一个xml问题,不太确定。

    <xsl:stylesheet version = "1.0"
        xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output method = "html" doctype-system = "about:legacy-compat"/>
    <xsl:template match = "/">
        <html xmlns = "http://www.w3.org/1999/xhtml">
            <head>
                <meta charset = "utf-8"/>        
                <link rel = "stylesheet" type = "text/css" href = "style.css"/>
            </head>
            <body>
                <table border = "1" syle = "background-color:blue">
                    <thead>
                        <tr> 
                            <th> Calories</th>
                            <th> Fat - Calories</th>
                            <th> Fat - Grams </th>
                            <th> Saturated Fat</th>
                        </tr>
                    </thead>
                    <xsl:for-each select="nutrition/item">
                        <tr>
                            <td><xsl:value-of select="calories/amount"/></td>
                            <td><xsl:value-of select="caloriesFat/amount"/></td>
                            <td><xsl:value-of select="gramsFat/amount"/></td>
                   </tr>                   
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
    </xsl:stylesheet>
    <?xml version = "1.0"?>
    <?xml-stylesheet type = "text/xsl" href = "nutrition.xsl"?>
    <nutrition:items xmlns:nutrition = "http://www.grandmascookies.com/nutrition">
    <title>Grandma White's Cookies</title>
    <item>
            <servingsize> 
                    <amount> 1 </amount> 
                    <unit> package </unit> 
                    </servingsize>  
            <calories> 
                    <amount> 260 </amount> 
                    <unit> calories </unit> 
                    </calories>
            <caloriesFat> 
                    <amount> 100 </amount> 
                    <unit> grams </unit> 
                    </caloriesFat>    
            <gramsFat> 
                    <amount> 11 </amount> 
                    <unit> grams </unit> 
                    </gramsFat>  
    </item>
    </nutrition:items>
    2 回复  |  直到 7 年前
        1
  •  1
  •   zx485 potemkin    7 年前

    XSLT中缺少名称空间,因此将其添加到样式表的根元素中

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

    然后在 xsl:for-each :

    <xsl:for-each select="nutrition:items/item">       <!-- here -->
      <tr>
        <td><xsl:value-of select="calories/amount"/></td>
        <td><xsl:value-of select="caloriesFat/amount"/></td>
        <td><xsl:value-of select="gramsFat/amount"/></td>
      </tr>  
    

    您只提供了名称空间前缀 nutrition 没有元素 items

        2
  •  1
  •   Gottfried Lesigang    7 年前

    您必须声明名称空间“xmlns:nutrition”,并相应地使用它。

    没有 nutrition/item 但是 nutrition:items/item

    你可以走捷径试试 //item (或使用通配符,如 /*:items/*:item //*:item 如果涉及其他名称空间)

    推荐文章