我正在尝试从解析的XML文档中提取具有特定innertext的元素.我知道我可以选择一个具有特定innertext子元素的元素//myparent[mychild='foo']
,但实际上我只想在这个例子中选择"mychild"元素.
<myparent>
<mychild>
foo
</mychild>
</myparent>
Run Code Online (Sandbox Code Playgroud)
什么是"foo"的XPath查询将返回"mychild"节点?
我需要一个XPath来获取没有父元素的所有ChildNodes(包括Text Element,Comment Element和Child Elements).任何帮助
示例示例:
<DOC>
<PRESENTEDIN>
<X>
First Text Node #1
<y> Y can Have Child Nodes #
<child> deep to it </child>
</y>
Second Text Node #2 <z/>
</X>
<EVTS>
<evt/>
<evt>
<mtg_descr> SAE 2006 World Congress & Exhibition </mtg_descr>
<sess_descr> Advanced Hybrid Vehicle Powertrains (Part 1 of 5) </sess_descr>
<loc> Detroit,MI,United States </loc>
<sess_prod_grp_cd> TSESS </sess_prod_grp_cd>
<sess_evt_name> P13 </sess_evt_name>
<sess_gen_num> 138352 </sess_gen_num>
<mtg_start_dt> 04/03/2006 </mtg_start_dt>
<mtg_end_dt> 04/06/2006 </mtg_end_dt>
<desig> CONGRESS-2006 </desig>
</evt>
</EVTS>
<EVTTYPE>PAPER</EVTTYPE>
<SUPERTECH>
<![CDATA[C8585]]>
</SUPERTECH> …
Run Code Online (Sandbox Code Playgroud) 我是json的新手.我有一个程序从json对象生成xml.
String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}";
JSON json = JSONSerializer.toJSON( str );
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsCompatibility( false );
String xml = xmlSerializer.write( json );
System.out.println(xml);
Run Code Online (Sandbox Code Playgroud)
输出是:
<?xml version="1.0" encoding="UTF-8"?>
<o><array json_class="array"><e json_type="number">1</e><e json_type="number">2</e><e json_type="number">3</e></array><boolean json_type="boolean">true</boolean><double json_type="number">2.0</double><integer json_type="number">1</integer><name json_type="string">JSON</name><nested json_class="object"><id json_type="number">42</id></nested></o>
Run Code Online (Sandbox Code Playgroud)
我最大的问题是如何编写自己的属性而不是json_type ="number",还要编写自己的子元素.
我的问题是关于使用dot和text()
in的具体细节XPath
.例如,以下find_element
行返回相同的元素:
driver.get('http://stackoverflow.com/')
driver.find_element_by_xpath('//a[text()="Ask Question"]')
driver.find_element_by_xpath('//a[.="Ask Question"]')
Run Code Online (Sandbox Code Playgroud)
那么区别是什么呢?使用.
和有text()
什么好处和缺点?
我正在使用XPath从网页中提取数据.
我有一个链接,我想得到的href值.我可以通过它唯一的id引用它:id('unique-id')
但是这给了我<a>
标签之间的字符串.
如何获取href
属性的值?
我有这个HTML:
<tr class="even expanded first>
<td class="score-time status">
<a href="/matches/2012/08/02/europe/uefa-cup/">
16 : 00
</a>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我想在没有额外空格的情况下提取(16:00)字符串.这可能吗?
我正在尝试对xpath查找返回的节点执行xpath查找,但它似乎没有按预期工作.在文档的子节点上执行的XPaths似乎是针对文档的根节点执行的(在例如,库存标签.),而不是提供的节点的根.
我在这里错过了什么吗?我是XPath的新手.
另外,请不要回答"只做//书[作者='Neal Stephenson'/ title".我有一个合法的用例,这是一个简化的例子.
代码段
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("src/main/java/books.xml");
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
Node book = (Node) xpath.evaluate("//book[author='Neal Stephenson']", doc, XPathConstants.NODE);
Node title = (Node) xpath.evaluate("/title", book, XPathConstants.NODE); // I get null here.
Node inventory = (Node) xpath.evaluate("/inventory", book, XPathConstants.NODE); // this returns a node.
Run Code Online (Sandbox Code Playgroud)
是book.xml
<inventory>
<book year="2000">
<title>Snow Crash</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553380958</isbn>
<price>14.95</price>
</book>
<book year="2005">
<title>Burning Tower</title>
<author>Larry Niven</author>
<author>Jerry Pournelle</author>
<publisher>Pocket</publisher> …
Run Code Online (Sandbox Code Playgroud) 我正在尝试查询adium生成的xml文件.xmlwf说它形成得很好.通过使用xmllint的调试选项,我得到以下内容:
$ xmllint --debug doc.xml
DOCUMENT
version=1.0
encoding=UTF-8
URL=doc.xml
standalone=true
ELEMENT chat
default namespace href=http://purl.org/net/ulf/ns/0.4-02
ATTRIBUTE account
TEXT
content=foo@bar.com
ATTRIBUTE service
TEXT compact
content=MSN
TEXT compact
content=
ELEMENT event
ATTRIBUTE type
Run Code Online (Sandbox Code Playgroud)
一切似乎都解析得很好.但是,当我尝试查询最简单的东西时,我什么都得不到:
$ xmllint --xpath '/chat' doc.xml
XPath set is empty
Run Code Online (Sandbox Code Playgroud)
发生了什么?使用xpath运行完全相同的查询会返回正确的结果(但结果之间没有换行符).我做错了什么还是xmllint不能正常工作?
这是一个较短的,匿名的xml版本,它显示了相同的行为:
<?xml version="1.0" encoding="UTF-8" ?>
<chat xmlns="http://purl.org/net/ulf/ns/0.4-02" account="foo@bar.com" service="MSN">
<event type="windowOpened" sender="foo@bar.com" time="2011-11-22T00:34:43-03:00"></event>
<message sender="foo@bar.com" time="2011-11-22T00:34:43-03:00" alias="foo"><div><span style="color: #000000; font-family: Helvetica; font-size: 12pt;">hi</span></div></message>
</chat>
Run Code Online (Sandbox Code Playgroud) 有没有人知道从XPath表达式以编程方式创建XML层次结构的现有方法?
例如,如果我有一个XML片段,例如:
<feed>
<entry>
<data></data>
<content></content>
</entry>
</feed>
Run Code Online (Sandbox Code Playgroud)
给定XPath表达式/ feed/entry/content/@ source我会:
<feed>
<entry>
<data></data>
<content @source=""></content>
</entry>
</feed>
Run Code Online (Sandbox Code Playgroud)
我意识到使用XSLT是可能的,但由于我正在尝试完成的动态特性,固定转换将无法工作.
我在C#工作,但如果有人使用其他语言的解决方案,请加入.
谢谢您的帮助!