如何在HTML5表中显示XSL样式表

时间:2018-04-10 01:37:54

标签: xml xslt xsd

我有一个XML代码,应该在HTML5表格中显示XSL样式表,但我不确定我是否正朝着正确的方向前进

XSD代码是否会影响样式表是否在HTML5表中显示XML内容或者是否没有相关性

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "nutrition.xsl"?>
<nutrition:items xmlns:nutrition = "http://www.grandmascookies.com/nutrition">
<product name = "Grandma White's Cookies">
<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>
</product>
</nutrition:item>
<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"/>
            <title> Cookies </title>
        </head>
        <body>
            <table border = "1" syle = "background-color: blue">
                <thead>
                    <tr> 
                        <th> Calories</th>
                        <th> Fat - Calories</th>
                        <th> Fat - Grams </th>
                    </tr>
                </thead>
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

XSD是无关紧要的,除非你想验证xml输入 - 完全从XSL中删除。

我认为你对XSL有误解。 HTML5表格不显示XSL,它正在生成HTML(或任何您想要的其他内容)。

您的样式表与根节点(“/”,在您的情况下为营养:项目)匹配,并在每次找到时生成代码(即一次)。现在,您希望在正确的位置匹配表格中某一行的每个项目:

                </thead>
              <xsl:for-each select="./product/item">
                  <!-- Generate html table row  -->
                  <tr>
                  <!-- Calories (relative to current node)  -->
                      <td><xsl:value-of select="calories/amount"/></td>
                  <!-- Repeat for other fields. Probably want the name somewhere, too  -->
                  </tr>
              </xsl:for-each>
        </table>

您还可以使用&lt; xsl:apply-templates ...&gt;和&lt; xsl:template-match select =“calories / amount”&gt;