简体   繁体   English

如何在xquery中将元素节点转换为文档

[英]How to convert a element node to document in xquery

I have a xml. 我有一个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. 如果我给返回$a/a/b它不起作用,因为这是一个元素节点。

So I need to convert a element node to document node. 所以我需要将元素节点转换为文档节点。 In order to make it work. 为了使它工作。

I cant change the xpath . 我不能改变xpath Is their anyway to get result by using same xpath '/a/b'? 无论如何他们通过使用相同的xpath '/ a / b'得到结果?

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

I don't think that wrapping your xml in a document node is the solution you are looking for. 我不认为将xml包装在document node是您正在寻找的解决方案。 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. 你真正需要的只是xml的一个包装级别,然后你的xpath将正常工作。 You can pick any element to be the root of your dynamically generated xml, such as: 您可以选择任何元素作为动态生成的xml的根,例如:

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. 所以我只是坚持将$a xml包装在一个xml节点的另一层中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM