0

我正在处理我的硬件 xml/xsl。我真的是这个领域的新手。这是我的代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"               
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:pre="/home/sbtge/Documents/1.SCHOOL/Webove_technologie/vypracovanie"
            exclude-result-prefixes="pre">
<xsl:output method="html"/>

<xsl:template match="pre:document">
    <html>
                <link rel="stylesheet" href="nutri.css" type="text/css"/>
        <head>
            <title> Nutričné hodnoty </title>

        </head>
        <body>
                            <h1> Nutričné hodnoty </h1>
            <div>   
                                <table>
                                <tr>
                <th>Name</th>
                                    <th>Kategória <br/> (g) </th>
                                    <th>Cukry <br/> (g) </th>
                                    <th>Tuky <br/> (g) </th>
                                    <th>Bielkoviny <br/> (g) </th>
                                    <th>Energia <br/> (kJ) </th>
                                </tr>
                <xsl:apply-templates select="pre:food"/>
                                </table>
            </div>
        </body>
    </html>
</xsl:template>

<xsl:template match="pre:food">     
                                <tr>
                                    <td><xsl:value-of select="pre:name"/></td>  

                                    <xsl:choose>

                                        <xsl:when test="pre:category='Zelenina'">
                                           <div class="Zelenina">
                                                 <td><xsl:value-of select="pre:category"/></td> 
                                           </div>
                                        </xsl:when>
                                        <xsl:when test="pre:category='Ovocie'">
                                            <div class="Ovocie">
                                                 <td><xsl:value-of select="pre:category"/></td>
                                            </div>
                                        </xsl:when>
                                        <xsl:when test="pre:category='Ryža'">
                                            <div class="Ryza">
                                                 <td><xsl:value-of select="pre:category"/></td>
                                            </div>
                                        </xsl:when>
                                        <xsl:when test="pre:category='Mäso'">
                                            <div class="Maso">
                                                 <td><xsl:value-of select="pre:category"/></td>
                                            </div>
                                        </xsl:when>
                                    </xsl:choose>
                                    <xsl:apply-templates select="pre:values"/>
                                </tr>

</xsl:template>

<xsl:template match="pre:values">
        <td><xsl:value-of select="pre:sacharides"/> g</td>
        <td><xsl:value-of select="pre:fat"/> g</td>
        <td><xsl:value-of select="pre:proteins"/> g</td>
        <td><xsl:value-of select="pre:energy"/> g</td>
    </xsl:template>

一切正常,但 xsl:choose 被忽略。我认为这是因为糟糕的“测试”声明。我应该如何纠正它?非常感谢。我尝试了所有类似的方法:@pre:category、pre:@category、@category。

4

1 回答 1

0

xml 文件中有错误定义的属性。XSL 应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"               
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:ns= "/home/sbtge/Documents/1.SCHOOL/Webove_technologie/vypracovanie"
            exclude-result-prefixes="ns">

<xsl:output method="html"/>

<xsl:template match="ns:document">
    <html>
                <link rel="stylesheet" href="nutri.css" type="text/css"/>
        <head>
            <title> Nutričné hodnoty </title>

        </head>
        <body>
                            <h1> Nutričné hodnoty </h1>
            <div>   
                                <table>
                                <tr>
                <th>Name</th>
                                    <th>Kategória <br/> (g) </th>
                                    <th>Cukry <br/> (g) </th>
                                    <th>Tuky <br/> (g) </th>
                                    <th>Bielkoviny <br/> (g) </th>
                                    <th>Energia <br/> (kJ) </th>
                                </tr>
                <xsl:apply-templates select="ns:food"/>

                                </table>
            </div>

        </body>
    </html>
</xsl:template>

   <!-- <xsl:template match="document">
        sdafsa
        <xsl:apply-templates select="food"/>
    </xsl:template> -->

<xsl:template match="ns:food">  

                                <tr>
                                    <td><xsl:value-of select="ns:name"/></td>  
                                    <xsl:choose>    
                                        <xsl:when test="@category='Zelenina'">
                                          <div class="Zelenina">
                                                <xsl:value-of select="@category"/>
                                          </div> 
                                        </xsl:when>
                                        <xsl:when test="@category='Ovocie'">
                                            <div class="Ovocie">
                                                 <xsl:value-of select="@category"/>
                                            </div>
                                        </xsl:when>
                                        <xsl:when test="@category='Ryža'">
                                            <div class="Ryza">
                                                 <xsl:value-of select="@category"/>                                           </div>
                                        </xsl:when>
                                        <xsl:when test="@category='Mäso'">
                                            <div class="Maso">
                                                 <xsl:value-of select="@category"/>
                                            </div>
                                        </xsl:when>
                                    </xsl:choose>

                                    <xsl:apply-templates select="ns:values"/>
                                </tr>

</xsl:template>

<xsl:template match="ns:values">
        <td><xsl:value-of select="ns:sacharides"/> g</td>
        <td><xsl:value-of select="ns:fat"/> g</td>
        <td><xsl:value-of select="ns:proteins"/> g</td>
        <td><xsl:value-of select="ns:energy"/> Kj</td>
    </xsl:template>

每个人都应该帮助自己:D

于 2012-11-11T10:29:58.397 回答