计算具有多个条件和精确属性的xml元素 [英] Count xml elements with multiple conditions and precise properties

查看:72
本文介绍了计算具有多个条件和精确属性的xml元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(xml和xslt),我想用2个条件计算一些xml元素。

I have the following code (xml and xslt) and I would like to count some xml elements with 2 conditions.

XMLcode:

<home>
    <place Value='place1'>
        <property Name="Type" Value="house" />
        <property Name="Context" Value="roof" />
        <property Name="Color" Value="blue" />
    </place>
    <place Value='place2'>
        <property Name="Type" Value="house" />
        <property Name="Context" Value="kitchen" />
        <property Name="Color" Value="red" />
    </place>
    <place Value='>
        <property Name="Type" Value="house" />
        <property Name="Context" Value="floor" />
        <property Name="Color" Value="black" />
    </place>
    <place Value='place4'>
        <property Name="Type" Value="house" />
        <property Name="Context" Value="kitchen" />
        <property Name="Color" Value="black" />
    </place>
    <place Value='place5'>
        <property Name="Type" Value="apartment" />
        <property Name="Context" Value="roof" />
        <property Name="Color" Value="blue" />
    </place>
    <place Value='place6'>
        <property Name="Type" Value="apartment" />
        <property Name="Context" Value="kitchen" />
        <property Name="Color" Value="red" />
    </place>
</home>

xslt代码:

<html>
<body>
<table border="1" cellspacing="0" cellpadding="3">
    <tr>
    <td>Place</td>
    <td>Type</td>
    <td>Context</td>
    <td>Color</td>
    </tr>
    <xsl:for-each select="place">
        <tr>
            <td><xsl:value-of select="@Value" /></td>
            <td><xsl:value-of select="property[@Name='Type']/@Value" /></td>
            <td><xsl:value-of select="property[@Name='Context']/@Value" /></td>
            <td><xsl:value-of select="property[@Name='Color']/@Value" /></td>
        </tr>
    </xsl:for-each>
</table>

<table>
    <tr>
        <td>
            Number of house:
        </td>
        <td>
            <xsl:value-of select="count(place/property[@Name='Type' and @Value='house'])"/>
        </td>
    </tr>
    <tr>
        <td>
            Number of kitchen:
        </td>
        <td>
            <xsl:value-of select="count(place/property[@Name='Context' and @Value='kitchen'])"/>
        </td>
    </tr>
    <tr>
        <td>
             Number of house with kitchen:
        </td>
        <td>
            <xsl:value-of select="count(place/property[@Name='Type' and @Value='house'][@Name='Context' and @Value='kitchen'])"/>
        </td>
    </tr>
</table>

</body>
</html>

前两个计数工作正常(这些计数只有一个条件),但是第三个计数不起作用,因为有两个条件。有什么建议如何使其工作?
谢谢。

The first two counts are working correctly (there is just one condition on these counts) but the third count does not work because there are two conditions. Any suggestion how to make it work? Thanks.

推荐答案

如果要计算房屋且有厨房的地方,请使用此表达式:

Use this expression, if you want to count places which are "house" and have "kitchen":

count(home/place[property[@Name='Type' and @Value='house'] and property[@Name='Context' and @Value='kitchen']])

这篇关于计算具有多个条件和精确属性的xml元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆