Xslt:如何用条件解析和回显xml数据

时间:2016-04-27 06:42:54

标签: xml xslt

我需要使用xslt来做这样的事情:

<xsl:for-each>

我在2天后尝试使用xslt <xsl:if>语句和<myShop> <product itemType="processor"> <brand>Intel</brand> <price secondHand="false">230$</price> <nbCore>4</nbCore> </product> <product itemType="processor"> <brand>Amd</brand> <price secondHand="true">90$</price> <nbCore>2</nbCore> </product> <product itemType="laptop"> <brand>Dell</brand> <price secondHand="false">600$</price> <color>black</color> </product> <product itemType="laptop"> <brand>Apple</brand> <price secondHand="true">900$</price> <color>silver</color> </product> </myShop> 语句,但我绝对不知道 如何在看过一些网络教程后正确使用它。有人可以帮我吗?

<agency>
    <product productType="appartement">
        <price transactionType="rent">1000€</prix>
        <location>Paris</location>
        <surface>80m²</surface>
        <bedroom>1</bedroom>
        <bathoom>2</bathoom> 
        <heating heatingType="gaz">yes</heating>
    </product>       
    </product>
    <product productType="house">
        <price productType="sell">280000€</prix>
        <location>London</location>
        <surface>190m²</surface>
        <bedroom>3</bedroom>
        <bathoom>2</bathoom> 
        <heating heatingType="electric">yes</heating>
    </product>
</agency>

编辑: 请查看此Xml代码

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>agence.xsl</title>
            </head>
            <body>                
                //help me echo house having price > 200000 and price < 300000 and having bedroom >= 3 order by location, surface desc                 
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

这是我的Xslt样式表

Activity

你能帮我回复房价吗? 200000和价格&lt; 300000并且卧室&gt; = 3个按位置排序,表面描述?

1 个答案:

答案 0 :(得分:1)

应该有两个小模板:

  

if(itemType ==&#34; processor&#34;&amp;&amp; price&lt; 100 $)echo processor [&#34;品牌&#34;]

此处,我们使用 translate()删除$

    <xsl:template match="product[@itemType='processor' and number(translate(price,'$','') ) &lt; 100 ]">
        <xsl:value-of select="brand"/>
    </xsl:template>
  

if(itemType ==&#34; laptop&#34;&amp;&amp; color ==&#34; black&#34;)echo laptop [&#34; price&#34;]

    <xsl:template match="product[@itemType='laptop' and color = 'black']">
        <xsl:value-of select="price"/>
    </xsl:template>

第三个是&#34;否则&#34; (忽略不匹配的那些)

    <xsl:template match="product"/>