不显示表输出

时间:2014-03-22 22:30:39

标签: html xml xslt xslt-1.0

我必须编写以下xml标记的XSL样式表。

<?xml version = "1.0"?>
<?xml-stylesheet type = ""text/xsl" href = "cookies.xsl"?>


<product name="Grandma White's Cookies">

<servingsize>1 package</servingsize>

<calories>
<total>260 Calories</total>
<fat>100 Calories</fat>
</calories>

<fat>
<total>11 grams</total>
<saturated>2 grams</saturated>
</fat>


<cholesterol>
5 milligrams</cholesterol>
<sodium>
210 milligrams</sodium>

<carbohydrates>
<total>36 grams</tota`enter code here`l>
<fiber>2 grams</fiber>
<sugars>15 grams</sugars>
</carbohydrates>


<protein>5 grams</protein>

</product>

我的xsl标记如下。我需要以表格形式显示信息。我尝试输出下面的信息,但是没有显示。你能帮我解决一下吗?

<?xml version = "1.0"?>

<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>Grandma White's cookies</title>
</head>

<body>
  <h1>

         Grandma White's cookie nutrition facts </h1>
<h3>Serving size <xsl:value-of select="servingsize"/></h3>

<table>
<thead>

    <tr>

        <th>Description></th>
        <th>Information</th>
        </tr>
        </thead>




        <xsl:for-each select="/calories">
        <tr>
            <td>Calories</td>
            <td><xsl:for-each select="total"/><br></br>
            <xsl:for-each select="fat"/></td>
            </tr>

        </xsl:for-each>


</table>



</body>
</html>

</xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:1)

这是一个可以用作起点的样式表。请注意,产品的名称不是硬编码的,因此可以与任何产品一起使用。 OTOH,假设每种产品的营养数据采用相同的格式。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <html>
        <head>
            <title>
                <xsl:value-of select="product/@name"/>
            </title>
        </head>
        <body>
            <h1>
                <xsl:value-of select="product/@name"/>
                <xsl:text> Nutrition Facts</xsl:text>
            </h1>
            <h3>
                <xsl:text>Serving size: </xsl:text>
                <xsl:value-of select="product/servingsize"/>
            </h3>
            <table>
                <thead>
                    <tr>
                        <th>Description</th>
                        <th>Information</th>
                    </tr>
                </thead>
                <tr>
                    <td>Calories</td>
                    <td>
                        <xsl:text>Total: </xsl:text>
                        <xsl:value-of select="product/calories/total"/>
                        <br/>
                        <xsl:text>Fat: </xsl:text>
                        <xsl:value-of select="product/calories/fat"/>
                    </td>
                </tr>
                <!-- more rows -->
                <tr>
                    <td>Protein</td>
                    <td>
                        <xsl:value-of select="product/protein"/>
                    </td>
                </tr>
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

答案 1 :(得分:1)

此样式表

<?xml version = "1.0"?>

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

    <xsl:output method="html" doctype-system="about:legacy-compat"></xsl:output>

    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

    <xsl:template match="/">

        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta charset="utf-8"></meta>
                <link rel="stylesheet" type="text/css" href="style.css"></link>
                <title>Grandma White's cookies</title>
            </head>
            <body>
                <h1>Grandma White's cookie nutrition facts</h1>
                <h3>Serving size: <xsl:value-of select="product/servingsize"></xsl:value-of></h3>
                <table>
                    <thead>
                        <tr>
                            <th>Description</th>
                            <th>Information</th>
                        </tr>
                    </thead>
                    <tbody>
                    <xsl:for-each select="/product/*[not(self::servingsize)]">
                        <tr>
                            <td>
                                <xsl:value-of select="translate(substring(local-name(), 1, 1), $smallcase, $uppercase)"/>
                                <xsl:value-of select="substring(local-name(), 2)"/>
                            </td>
                            <td>
                                <xsl:choose>
                                    <xsl:when test="*">
                                        <xsl:for-each select="*">
                                            <xsl:choose>
                                                <xsl:when test="position() != last()">
                                                    <xsl:value-of select="concat(local-name(), ': ', .)"/><br/>
                                                </xsl:when>
                                                <xsl:otherwise>
                                                    <xsl:value-of select="concat(local-name(), ': ', .)"/>
                                                </xsl:otherwise>
                                            </xsl:choose>
                                        </xsl:for-each>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="."/>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </td>
                        </tr>

                    </xsl:for-each>
                    </tbody>
                </table>
            </body>
        </html>

    </xsl:template>
</xsl:stylesheet>

应用于输入XML时,生成:

<!DOCTYPE html
  SYSTEM "about:legacy-compat">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta charset="utf-8"></meta>
      <link rel="stylesheet" type="text/css" href="style.css"></link>
      <title>Grandma White's cookies</title>
   </head>
   <body>
      <h1>Grandma White's cookie nutrition facts</h1>
      <h3>Serving size: 1 package</h3>
      <table>
         <thead>
            <tr>
               <th>Description</th>
               <th>Information</th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td>Calories</td>
               <td>total: 260 Calories<br></br>fat: 100 Calories
               </td>
            </tr>
            <tr>
               <td>Fat</td>
               <td>total: 11 grams<br></br>saturated: 2 grams
               </td>
            </tr>
            <tr>
               <td>Cholesterol</td>
               <td>5 milligrams</td>
            </tr>
            <tr>
               <td>Sodium</td>
               <td>210 milligrams</td>
            </tr>
            <tr>
               <td>Carbohydrates</td>
               <td>total: 36 grams<br></br>fiber: 2 grams<br></br>sugars: 15 grams
               </td>
            </tr>
            <tr>
               <td>Protein</td>
               <td>5 grams</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>