如何使用XPath计算具有某个属性的节点数 [英] How do I use XPath to count the number of nodes with a certain attribute

查看:332
本文介绍了如何使用XPath计算具有某个属性的节点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎不能得到一个XPath表达式为我的场景工作。我想找到所有具有类型EndDevice的设备节点。我可以计数所有的设备节点,我也可以找到所有设备节点与EndDevice属性。

  count(// Device)// works 
//设备[@xsi:type ='EndDevice'] //工作
count(//设备[@xsi:type ='EndDevice'])//无效



如果需要,我使用XPathBuilder。

解决方案

我使用XPathBuilder 2.0.0.4转载了它。
然而,XPath表达式在我尝试的在线评估器中正确工作和评估( http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm )。



编辑:也尝试与最新版本的Altova XMLspy



输入:

 <?xml version =1.0 > 
< asdf xmlns:xsi =n / a>
< Device xsi:type ='EndDevice'/>
< Device xsi:type ='EndDevice'/>
< Device xsi:type ='EndDevice'/>
< Device xsi:type ='EndDevice'/>
< / asdf>

xslt:

 code><?xml version =1.0encoding =UTF-8?> 
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transformxmlns:fo =http://www.w3.org/ 1999 / XSL / Formatxmlns:xsi =n / a>
< xsl:output indent =yes/>
< xsl:template match =*>
< output>
< xsl:value-of select =count(// Device [@xsi:type ='EndDevice'])/>
< / output>
< / xsl:template>
< / xsl:stylesheet>

输出:

 code><?xml version =1.0encoding =UTF-8?> 
< output xmlns:fo =http://www.w3.org/1999/XSL/Formatxmlns:xsi =n / a> 4< / output>

我认为这是XPathBuilder做错了。


I can't seem to get an XPath expression to work for my scenario. I want to find all the "Device" nodes that have the type "EndDevice". I'm able to count all the "Device" nodes, and I'm also able to find all "Device" nodes with the "EndDevice" attribute. However, I can't seem to combine them!

count(//Device) //works
//Device[@xsi:type='EndDevice'] //works
count(//Device[@xsi:type='EndDevice']) //doesn't work

If it matters, I'm using XPathBuilder.

解决方案

I reproduced it using XPathBuilder 2.0.0.4. However the XPath expression works and evaluates correctly in an online evaluator I tried (http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm).

EDIT: Also tried with latest version of Altova XMLspy

input:

<?xml version="1.0"?>
<asdf xmlns:xsi="n/a">
    <Device xsi:type='EndDevice'/>
    <Device xsi:type='EndDevice'/>
    <Device xsi:type='EndDevice'/>
    <Device xsi:type='EndDevice'/>
</asdf>

xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsi="n/a">
    <xsl:output indent="yes"/>
    <xsl:template match="*">
        <output>
            <xsl:value-of select="count(//Device[@xsi:type = 'EndDevice'])"/>
        </output>
    </xsl:template>
</xsl:stylesheet>

output:

<?xml version="1.0" encoding="UTF-8"?>
<output xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsi="n/a">4</output>

I think it's XPathBuilder thats doing something wrong.

这篇关于如何使用XPath计算具有某个属性的节点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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