Xpath和VTD-XML,获取节点索引
我正在使用VTD-XML
用Xpath表达式解析一些XML文件,现在出现以下内容:我需要知道xml树中节点的索引,经过一番搜索后我发现像这样的表达式
count(//ds[name='cpu1']/preceding-sibling::*)+1
为我提供了所需的索引 - 我的问题是:到目前为止我正在使用 VTD-XML(因为它很快)是否有可能在 VTD-XML
中使用这种表达式?使用给定的表达式只会导致 XpathParseException。
我的 xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<rrd>
<version>0003</version>
<step>5</step>
<lastupdate>1290585118</lastupdate>
<ds>
<name>cpu1</name>
<type>DERIVE</type>
<minimal_heartbeat>300.0000</minimal_heartbeat>
<min>0.0</min>
<max>1.0000</max>
<last_ds>69375.6708</last_ds>
<value>0.0001</value>
<unknown_sec>0</unknown_sec>
</ds>
<ds>
<name>cpu0</name>
<type>DERIVE</type>
<minimal_heartbeat>300.0000</minimal_heartbeat>
<min>0.0</min>
<max>1.0000</max>
<last_ds>69375.6708</last_ds>
<value>0.0001</value>
<unknown_sec>0</unknown_sec>
</ds>
...
I'm using VTD-XML
to parse some XML files with Xpath expressions, now following showed up: I need to know the index of a node from the xml tree, after a bit of searching I found that a expression like this
count(//ds[name='cpu1']/preceding-sibling::*)+1
Provides me the needed index - my question is: As far I'm using VTD-XML (because its fast) is there a possibility to use this kind of expression within VTD-XML
? Using the given expression causes just a XpathParseException.
My xml look like this:
<?xml version="1.0" encoding="UTF-8"?>
<rrd>
<version>0003</version>
<step>5</step>
<lastupdate>1290585118</lastupdate>
<ds>
<name>cpu1</name>
<type>DERIVE</type>
<minimal_heartbeat>300.0000</minimal_heartbeat>
<min>0.0</min>
<max>1.0000</max>
<last_ds>69375.6708</last_ds>
<value>0.0001</value>
<unknown_sec>0</unknown_sec>
</ds>
<ds>
<name>cpu0</name>
<type>DERIVE</type>
<minimal_heartbeat>300.0000</minimal_heartbeat>
<min>0.0</min>
<max>1.0000</max>
<last_ds>69375.6708</last_ds>
<value>0.0001</value>
<unknown_sec>0</unknown_sec>
</ds>
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据常见问题解答,
因此,如果 VTD-XML 拒绝有效的 XPath 1.0 表达式,则 VTD-XML 中似乎存在错误。但问题可能出在 XPath 表达式中,或者出在使用 VTD-XML 的代码中。
您给出的 XPath 表达式似乎是有效的。但它的效率不是很高,因此可能有更好的方法在 VTD-XML 中执行此操作,而无需使用 XPath。例如,遍历输入记录并计数,直到找到名称为
cpu1
的ds
元素。According to the FAQ,
So if VTD-XML is rejecting a valid XPath 1.0 expression, there would seem to be a bug in VTD-XML. But the problem could be in the XPath expression, or in your code that uses VTD-XML.
The XPath expression you give appears to be valid. It's not very efficient though, so there may be a better way to do this in VTD-XML, without using XPath. E.g. going through the input records and counting till you get to the
ds
element whose name iscpu1
.