2010-03-05 67 views
27

我似乎无法获得适用于我的方案的XPath表达式。我想找到所有类型为“EndDevice”的“设备”节点。我能够统计所有“设备”节点,并且我还能够找到具有“EndDevice”属性的所有“设备”节点。但是,我似乎无法将它们结合起来!如何使用XPath来统计具有某个属性的节点数

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

如果重要,我使用XPathBuilder。

+0

你是什么意思,第二个“工作”?它与一些节点相匹配吗?您的XPath看起来是正确的,所以在您没有展示的代码段或您正在使用的工具中,某些内容不起作用。 – 2010-03-05 20:29:20

回答

20

我使用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:

<?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> 

输出:

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

我认为这是XPathBuilder这就是做一些事情错了。

+1

同意。这可能是XPathBuilder的一个问题。我使用Xalan,Saxon 6.5.5,Saxon HE 9.2.0.3在oXygen测试了XML/XSLT,并获得了所需的输出。 – 2010-03-06 20:02:22

3

使用保存到的test.xml上述xml和使用该工具http://kernowforsaxon.sourceforge.net/

declare namespace xsi="n/a"; 
count(doc('test.xml')//Device[@xsi:type = "EndDevice"]) 

可生产右输出。

相关问题