XSLT计算特定类型的节点

时间:2015-05-23 04:01:16

标签: xml xslt

我有一个xml,我想计算所有具有公寓类型并平均它们的房产的价格。我该怎么做呢?我尝试过用自己的方式,但它不起作用。至于具有便宜租赁价格的总物业,我如何计算它以便我可以得到低于400的所有价格的总和。

$string

1 个答案:

答案 0 :(得分:0)

我对你想要的结果感到困惑。看看这是否有帮助:

 ( SELECT u.name
        , u.id
        , l.url
        , l.online
     FROM users u
     JOIN livechat 
       ON l.url = CONCAT('http://www.software.com/', u.chat_handle)
      AND l.online = 1
    WHERE u.live_account = 'y'
    ORDER 
       BY u.name ASC
    LIMIT 0,24
 )
 UNION ALL
 ( SELECT u.name
        , u.id
        , NULL AS url
        , 0    AS online
     FROM users u
     LEFT
     JOIN livechat 
       ON l.url = CONCAT('http://www.software.com/', u.chat_handle)
      AND l.online = 1
    WHERE l.url IS NULL
      AND u.live_account = 'y'
    ORDER 
       BY u.name ASC
    LIMIT 0,24
 )
 ORDER BY 4 DESC, 1 ASC
 LIMIT 0,24 

给出以下示例输入:

<xsl:template match="/rentalProperties">
    <xsl:variable name="apartments" select="property[normalize-space(type)='apartment']" />
    <div>
        <p>Total number of apartments: <xsl:value-of select="count($apartments)"/></p>
        <p>Average apartment price: <xsl:value-of select="sum($apartments/price) div count($apartments)"/></p>
        <p>Total properties with cheap rental price: <xsl:value-of select="count(property[price &lt; 400])"/></p>
    </div>
</xsl:template>

结果将是:

<rentalProperties>
  <property contact="123">
    <type>apartment</type>
    <price>475</price>
  </property>
  <property contact="456">
    <type>house</type>
    <price>350</price>
  </property>
  <property contact="78">
    <type>apartment</type>
    <price>375</price>
  </property>
</rentalProperties>