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

Recommended Answers

All 10 Replies

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

Click Here

tutorial
Click Here
example5
Click Here
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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.