【问题标题】:why does the XSL style sheet display html table with no XML (info/values)为什么 XSL 样式表显示没有 XML(信息/值)的 html 表
【发布时间】:2018-09-21 11:55:02
【问题描述】:

我正在尝试创建一个 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>

【问题讨论】:

    标签: xml xslt xsd


    【解决方案1】:

    您在 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】:

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

      没有nutrition/item,而是nutrition:items/item

      您可以走捷径试试//item(如果涉及其他命名空间,则使用/*:items/*:item//*:item 之类的通配符)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-13
        • 1970-01-01
        • 2018-07-05
        • 1970-01-01
        • 1970-01-01
        • 2015-11-24
        • 2012-11-07
        • 1970-01-01
        相关资源
        最近更新 更多