Таблица вывода не отображается

0

Я должен написать таблицу стилей XSL для ниже разметки xml.

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


<product name="Grandma White 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 cookies</title>
</head>

<body>
  <h1>

         Grandma White 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>
  • 0
    проверьте тег 'total', вы оставили там комментарий!
  • 0
    Пожалуйста, опубликуйте результаты, которые вы пытаетесь достичь.
Теги:
xslt
xslt-1.0

2 ответа

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 cookies</title>
            </head>
            <body>
                <h1>Grandma White 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 cookies</title>
   </head>
   <body>
      <h1>Grandma White 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>
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>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню