Xsl如果测试要显示价格gt

时间:2016-04-16 14:09:34

标签: xml xslt xslt-1.0 xslt-2.0

我试图做一个xsl if语句,看看我能得到的价格都高于一定数量才能显示出来但是我不能让它工作不好发布代码,看看是否有人能看到我的错误我有一个dtd和一个样式表,但我认为不需要这些

欢呼

XSL:

<?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" />
    <xsl:template match="/SpecialOffers">
        <html>
            <head>
                <link href="style.css" rel="stylesheet" type="text/css" />
            </head>
            <body>
                <center>
                    <h1>Special Offer's SuperMarket </h1>
                </center>
                <div id="Title1">
                    <p>Offers Above .99c</p>
                </div>
<div id="content">
                    <table style="width:100%">
                        <tr>
                            <td>Description</td>
                            <td>Price</td>
                            <td>Offer</td>
                            <td>Number Of items</td>
                        </tr>
                        <xsl:for-each select="Hygiene/haircare">
                        <xsl:if test="Price &gt;= 10">
                            <tr>
                                <td>

                                    <xsl:value-of select="description"/>
                                </td>
                                <td>
                                    <xsl:value-of select="Price"/>
                                </td>
                                <td>
                                    <xsl:value-of select="Offer"/>
                                </td>
                                <td>
                                    <xsl:value-of select="Number_of_items"/>
                                </td>
                            </tr>



                        </xsl:if>
                        </xsl:for-each>

            </table>
            </div>              
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE SpecialOffers SYSTEM "Offers.dtd">
<!-- <?xml-stylesheet type="text/xsl" href="SpecialOffersStyleSheet.xsl"?> -->
<?xml-stylesheet type="text/xsl" href="LoopOffers.xsl"?> 
<?xml-stylesheet type="text/css" href="style.css" ?>
<SpecialOffers>
    <Hygiene>
        <haircare hid="HS">
            <Price>3.99</Price>
            <Offer> 2 for 1 </Offer>
            <Number_of_items>2</Number_of_items>
            <description>Head and Shoulders</description>
        </haircare>
        <haircare hid="HE">
            <Price>1.99</Price>
            <Offer>30% Off  </Offer>
            <Number_of_items>2</Number_of_items>
            <description>Herbal Essence</description>
        </haircare>
        <haircare hid="L">
            <Price>.99c</Price>
            <Offer> Half Price  </Offer>
            <Number_of_items>1</Number_of_items>
            <description>Loreal</description>
        </haircare>
        <face_body_care>
            <Price>1.50 </Price>
            <Offer>Half Price</Offer>
            <Number_of_items>1</Number_of_items>
            <description>Radox Shower Gel</description>
        </face_body_care>
    </Hygiene>
    <Cleaning_Products>
        <household>
            <Price>2.99</Price>
            <Offer>2 for 1</Offer>
            <Number_of_items>2</Number_of_items>
            <description>Mr Sheen</description>
        </household>
        <detergents>
            <Price>3.50</Price>
            <Offer>25% Off</Offer>
            <Number_of_items>1</Number_of_items>
            <description>Dettol</description>
        </detergents>
    </Cleaning_Products>
    <Grocery>
        <fruit fid="M">
            <name>Mango</name>
            <Price>.99c</Price>
            <Offer>6 for .99c</Offer>
            <weight>2kg</weight>
            <organic></organic>
        </fruit>
        <fruit fid="B">
            <name>Banana</name>
            <Price>1.39c</Price>
            <Offer>1kg</Offer>
            <weight>1kg</weight>
            <organic>Yes</organic>
        </fruit>
        <vegeatables>
            <name>Carrot</name>
            <Price>1.49</Price>
            <Offer>1kg for 1.49</Offer>
            <weight>1kg</weight>
            <organic>No</organic>
        </vegeatables>
        <vegeatables>
            <name>Cabbage</name>
            <Price>.89c</Price>
            <Offer>1</Offer>
            <weight>1kg</weight>
            <organic>Yes</organic>
        </vegeatables>
    </Grocery>
    <Perishables>
        <canned_goods>
            <name>Tuna</name>
            <Price>.99c</Price>
            <Offer>Half Price</Offer>
            <weight>500g</weight>
        </canned_goods>
        <canned_goods>
            <name>Beans</name>
            <Price>1.99c</Price>
            <Offer>6 For 1.99</Offer>
            <weight>500g each</weight>
        </canned_goods>
    </Perishables>
    <Alcholic_Beverages>
        <spirits>
            <name>Vodka</name>
            <Price>14.50</Price>
            <Offer>Half Price</Offer>
            <ml>70cl</ml>
            <volume_acl>37.5%</volume_acl>
        </spirits>
        <spirits>
            <name>Gin</name>
            <Price>27.50</Price>
            <Offer>20% Off</Offer>
            <ml>1 Litre</ml>
            <volume_acl>37.5%</volume_acl>
        </spirits>
        <spirits>
            <name>Gin</name>
            <Price>27.50</Price>
            <Offer>20% Off</Offer>
            <ml>1 Litre</ml>
            <volume_acl>37.5%</volume_acl>
        </spirits>
        <wine>
            <name>Proseco</name>
            <Price>13.75</Price>
            <Offer>10% Off</Offer>
            <ml>1 Litre</ml>
            <volume_acl>13.5%</volume_acl>
        </wine>
        <largers>
            <name>Heinkein</name>
            <Price>£6</Price>
            <Offer>4 for 6</Offer>
            <ml>500ml</ml>
            <volume_acl>4%</volume_acl>
        </largers>
    </Alcholic_Beverages>
    <Non-Alcholic_Beverages>
        <cordials>
            <can>
                <name>7 Up</name>
                <Price>5.00</Price>
                <Offer>12 Pack</Offer>
                <ml>330</ml>
            </can>
            <bottle>
                <name>Coke</name>
                <Price>1.39</Price>
                <Offer>2 for 1</Offer>
                <ml>1 Litre</ml>
            </bottle>
        </cordials>
        <juice>
            <name>Sunny D</name>
            <Price>.75c</Price>
            <Offer>Half Price</Offer>
            <ml>1 Litre</ml>
        </juice>
    </Non-Alcholic_Beverages>
</SpecialOffers>

1 个答案:

答案 0 :(得分:0)

样式表不生成任何表行的原因是源XML中没有价格大于或等于10的护发产品。

您的标题是“优惠高于.99c” - 如果您想展示价格高于0.99的产品,您需要提出您的条件:

<xsl:if test="Price &gt;= 1">

请注意,那些价格为.99c的XML产品不会通过此测试 - 但不是因为它们的价格低于阈值,而是因为它们的价格不是数字。要在比较中包含这些价格,您必须删除货币 - 例如:

<xsl:if test="translate(Price, 'c', '') &gt;= .5">

以上内容适用于XSLT 1.0,只要您的样式表继续标记为version="1.0",就可以使用XSLT 2.0处理器。