【问题标题】:Table output is not displayed表输出不显示
【发布时间】:2014-04-30 08:09:18
【问题描述】:

我必须为下面的 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>

【问题讨论】:

  • 检查'total'标签,你在stackoverflow上留下了评论!
  • 请发布您想要实现的输出。

标签: html xml xslt xslt-1.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>

【讨论】:

    【解决方案2】:

    这个样式表

    <?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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-10
      • 2021-07-02
      • 2018-02-27
      • 2020-03-16
      • 2019-09-19
      • 2021-02-03
      • 2013-02-23
      • 1970-01-01
      相关资源
      最近更新 更多