我是使用XPath的新手,这可能是一个基本问题.请耐心帮助我解决问题.我有一个像这样的XML文件:
<RootNode>
<FirstChild>
<Element attribute1="abc" attribute2="xyz">Data</Element>
<FirstChild>
</RootNode>
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方式验证<Element>标签的存在:
//Element[@attribute1="abc" and @attribute2="xyz"]
现在我还想检查字符串的标签值"Data".为实现这一目标,我被告知要使用:
//Element[@attribute1="abc" and @attribute2="xyz" and Data]
当我使用后面的表达式时,我收到以下错误:
断言失败消息:没有匹配的节点
//Element[@attribute1="abc" and @attribute2="xyz" and Data]
请向我提供您的建议,我使用的XPath表达式是否有效.如果没有,那么有效的XPath表达式是什么?
使用xpath查询如何查找是否存在节点(标记)?
例如,如果我需要确保网站页面具有正确的基本结构,如/ html/body和/ html/head/title
试图根据其中一个孩子的孩子的价值选择一个元素
想到以下但不工作,感谢任何帮助,谢谢
./book[/author/name = 'John'] 要么
./book[/author/name text() = 'John']
Run Code Online (Sandbox Code Playgroud)
想要所有作者姓名='John'的书籍
Xml文件
<list>
<book>
<author>
<name>John</name>
<number>4324234</number>
</author>
<title>New Book</title>
<isbn>dsdaassda</isbn>
</book>
<book>...</book>
<book>...</book>
</list>
Run Code Online (Sandbox Code Playgroud) 我有以下XML.
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee id="3">
<age>40</age>
<name>Tom</name>
<gender>Male</gender>
<role>Manager</role>
</Employee>
<Employee id="4">
<age>25</age>
<name>Meghna</name>
<gender>Female</gender>
<role>Manager</role>
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
我想选择id ="4"的Employee元素.
我正在使用XPath表达式下面没有返回任何东西.
//Employee/[@id='4']/text()
Run Code Online (Sandbox Code Playgroud)
我在http://chris.photobooks.com/xml/default.htm检查了它,它说无效的xpath,不知道问题出在哪里.
对于Ubuntu和/或CentOS,是否有一个包,它有一个命令行工具,可以执行XPath单线程,foo //element@attribute filename.xml或者foo //element@attribute < filename.xml逐行返回结果?
我正在寻找一些可以让我只是apt-get install foo或者yum install foo然后只是开箱即用,没有包装或其他必要的改编的东西.
以下是一些接近的事例:
引入nokogiri.如果我写这个包装器,我可以用上面描述的方式调用包装器:
#!/usr/bin/ruby
require 'nokogiri'
Nokogiri::XML(STDIN).xpath(ARGV[0]).each do |row|
puts row
end
Run Code Online (Sandbox Code Playgroud)
XML :: XPath的.可以使用这个包装器:
#!/usr/bin/perl
use strict;
use warnings;
use XML::XPath;
my $root = XML::XPath->new(ioref => 'STDIN');
for my $node ($root->find($ARGV[0])->get_nodelist) {
print($node->getData, "\n");
}
Run Code Online (Sandbox Code Playgroud)
xpath来自XML :: XPath返回太多噪音,-- NODE --和attribute = "value".
xml_grep 来自XML :: Twig无法处理不返回元素的表达式,因此无法在不进一步处理的情况下提取属性值.
编辑:
echo cat //element/@attribute | xmllint --shell filename.xml返回类似的噪音xpath …
我该如何验证我的XPath?
我正在使用Chrome Developers工具检查元素并形成我的XPath.我使用Chrome插件XPath Checker验证它,但它并不总能给我结果.什么是验证我的XPath的更好方法.
我也尝试使用Firebug来检查错误并使用FirePath进行验证.但Firepath还验证了XPath.
我的最后一个选择是使用Selenium WebDriver来确认我的XPath.
给定此XML,XPath将返回其prop属性包含的所有元素Foo(前三个节点):
<bla>
<a prop="Foo1"/>
<a prop="Foo2"/>
<a prop="3Foo"/>
<a prop="Bar"/>
</bla>
Run Code Online (Sandbox Code Playgroud) 我想写一些类似的东西:
//a[not contains(@id, 'xx')]
Run Code Online (Sandbox Code Playgroud)
(意思是'id'属性的所有链接都不包含字符串'xx')
我找不到合适的语法.
拥有以下XML:
<node>Text1<subnode/>text2</node>
Run Code Online (Sandbox Code Playgroud)
如何通过XPath选择第一个或第二个文本节点?
像这样的东西:
/node/text()[2]
Run Code Online (Sandbox Code Playgroud)
当然不起作用,因为它是节点内每个文本的合并结果.
我可以使用什么XPath来选择具有指定名称属性的任何类别以及具有指定值的任何子节点作者.
我尝试过以下路径的不同变体而没有成功:
//quotes/category[@name='Sport' and author="James Small"]
Run Code Online (Sandbox Code Playgroud)
XML:
<?xml version="1.0" encoding="utf-8"?>
<quotes>
<category name="Sport">
<author>James Small<quote date="09/02/1985">Quote One</quote><quote date="11/02/1925">Quote nine</quote></author>
</category>
<category name="Music">
<author>Stephen Swann
<quote date="04/08/1972">Quote eleven</quote></author>
</category>
</quotes>
Run Code Online (Sandbox Code Playgroud) xpath ×10
xml ×6
xslt ×2
dom ×1
expression ×1
firefox ×1
selection ×1
selenium ×1
shell ×1
xpathquery ×1