Kishan Ashra
Kishan Ashra

Reputation: 146

How to convert a element node to document in xquery

I have a xml.

let $a := <a>
             <b>asd</b>
             <c>bvn</c>
          </a>

if I give return $a/a/b it does not work as this is a element node.

So I need to convert a element node to document node. In order to make it work.

I cant change the xpath. Is their anyway to get result by using same xpath '/a/b'?

Upvotes: 1

Views: 1602

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167516

You can create a document node if you need to

let $a := <a>
             <b>asd</b>
             <c>bvn</c>
          </a>
let $a := document{$a}
return $a/a/b

or directly

let $a := document {
            <a>
                <b>asd</b>
                <c>bvn</c>
            </a>
            }

return $a/a/b

Upvotes: 3

CtheGood
CtheGood

Reputation: 1019

I don't think that wrapping your xml in a document node is the solution you are looking for. Is there a specific reason why you want it in a document node?

What you really need is just one more wrapping level of xml, and then your xpath will work fine. You can pick any element to be the root of your dynamically generated xml, such as:

let $a := <wrapper>
        <a>
            <b>ABC</b>
            <c>XYZ</c>
        </a>
    </wrapper>
return $a/a/b

(: This will return the element <b>ABC</b> :)

I suggest not wrapping it in a document node unless you have a very specific reason for that. According to the requirements you listed above though, you don't. So I'd just stick with wrapping your $a xml in one more layer of an xml node.

Upvotes: 0

Related Questions