XSL xsl:when-test,前缀

时间:2012-11-10 17:34:04

标签: testing

我正在使用我的HW 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。

1 个答案:

答案 0 :(得分: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

相关问题