Hi,
I am very new to this forum. I started learning XPath queries.
Can anyone please assist me on how to get elements from an xml where it doesn't contains any root element.
Thanks,
Vishwanath
Hi,
I am very new to this forum. I started learning XPath queries.
Can anyone please assist me on how to get elements from an xml where it doesn't contains any root element.
Thanks,
Vishwanath
Short answer: XPath runs against a parsed XML document (an infoset), so you must feed a well-formed XML document to your parser. As pointed out, XML requires exactly one document element; the DOMDocument error you saw happens because the fragment contains multiple top-level elements. Two practical options follow.
Wrap the fragment in a temporary root before parsing (example in PHP):
<?php
$fragment = '<item id="i2"><title>Example</title></item><item id="i3"><title>Other</title></item>';
$fragment = preg_replace('/^<\?xml.*?\?>\s*/', '', $fragment); // strip any XML declaration
$xml = '<root>' . $fragment . '</root>';
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$node = $xpath->query('/root/item[@id="i2"]/title')->item(0);
echo $doc->saveXML($node); Or do the same with Python + lxml:
import re
from lxml import etree
fragment = '<item id="i2"><title>Example</title></item><item id="i3"><title>Other</title></item>'
fragment = re.sub(r'^<\?xml.*?\?>\s*', '', fragment)
doc = etree.fromstring('<root>' + fragment + '</root>')
print(doc.xpath('/root/item[@id="i2"]/title/text()')[0]) Notes and gotchas: remove any internal XML declaration before wrapping (two declarations will break parsing); if your elements use a default namespace you must register a prefix for XPath lookups; for very large streams prefer a streaming parser (iterparse, XmlReader, SAX) and handle elements one by one instead of loading the whole string; never try to reliably parse XML with plain regex. This approach lets you run XPath against fragments even when the original input lacks a single root, and it addresses the exact DOMDocument error that encountered.
Jump to Post— xml_looser 3Each xml file must have a root node, this is a
xml conditionxpath but can also be wendt without root knot
tutorial
Click Here
example5
Jump to Post— xml_looser 3copy in xml window
<?xml version="1.0"?> <bookstore> <book id="bk101"> <author>Gambardella, Matthew</author> <description>with XML.</description> </book> <book id="bk201"> <author>Gambardella, Matthew</author> <description>with XML.</description> </book> <book id="bk301"> <author>Gambardella, Matthew</author> <description>with XML.</description> </book> </bookstore>and in xpath window
//book[@id="bk201"]/authorresult
<author>Gambardella, Matthew</author>if …
Jump to Post— xml_looser 3xml rules
rules click Here
For example, I wan tto find the book element where the book id is bk101 from the below xml conetent:
<book id="bk101">
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
<book id="bk201">
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
<book id="bk301">
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
Thanks in advance,
Vishwanath
Each xml file must have a root node, this is a
xml condition
xpath but can also be wendt without root knot
tutorial
Click Here
example5
example6
Click Here
without rootnode
//book[@id="bk201"]/author
result
<author>Gambardella, Matthew</author>
I hope this helps
Thanks for the quick reply.
I tried the same in http://www.online-toolz.com/tools/xpath-editor.php
But I got an error:
*
Error:DOMDocument::loadXML() [<a href='domdocument.loadxml'>domdocument.loadxml</a>]: Extra content at the end of the document in Entity, line: 5*
Any help is appreciated
Thanks,
Vishwanath
copy in xml window
<?xml version="1.0"?>
<bookstore>
<book id="bk101">
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
<book id="bk201">
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
<book id="bk301">
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
</bookstore>
and in xpath window
//book[@id="bk201"]/author
result
<author>Gambardella, Matthew</author>
if you not root node indicates it will never go
there are semi-regular in xml to which one must adhere to in order to apply it to
without rootnode
error
Error:DOMDocument::loadXML() [<a href='domdocument.loadxml'>domdocument.loadxml</a>]: Extra content at the end of the document in Entity, line: 5
Hi,
If the xml has root node, I am able to get the result. But I want to try XPath to get a particular book where the xml will not have a root node. There are multiple book elements exists with different ids but these elements are not wrapped under a root node.
As I mentioned in my first post, there is no root element.
Thanks,
Vishwanath
xpath evaluations go only with valid xml files
for me the question is answered herewith
to regulate the keeping of xml
in xpath then ask without rootnode with '/ /'
the means to address all child nodes
So, We can't do XPath query agaonst the xml data which doesn't have a root node?
everything which does not comply with the rules of xml
is not xml
see html xml is similar but is not xml
only by restrictions and user-dtd
and so from version 5 it is always more to xml
I suspect your question is create xpath request
without the root node to use and comes with 2 flash
the node and determine with the help xpath and attribute
is the node found
xml file
<?xml version="1.0"?>
<bookstore>
<book id="bk101">
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
<book id="bk201">
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
<book id="bk301">
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
</bookstore>
xpath
//*[@id="bk101"]
result
<author>Gambardella, Matthew</author>
<description>with XML.</description>
</book>
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.