xml 如何在 XQuery 中将字符串转换为节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/121237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you convert a string to a node in XQuery?
提问by Sixty4Bit
I would like to convert a string into a node. I have a method that is defined to take a node, but the value I have is a string (it is hard coded). How do I turn that string into a node?
我想将字符串转换为节点。我有一个定义为获取节点的方法,但我拥有的值是一个字符串(它是硬编码的)。如何将该字符串转换为节点?
So, given an XQuery method:
所以,给定一个 XQuery 方法:
define function foo($bar as node()*) as node() {
(: unimportant details :)
}
I have a string that I want to pass to the foo method. How do I convert the string to a node so that the method will accept the string.
我有一个要传递给 foo 方法的字符串。如何将字符串转换为节点,以便该方法接受该字符串。
回答by Sixty4Bit
MarkLogic solutions:
MarkLogic 解决方案:
The best way to convert a string into a node is to use:
将字符串转换为节点的最佳方法是使用:
xdmp:unquote($string).
Conversely if you want to convert a node into a string you would use:
相反,如果要将节点转换为字符串,则可以使用:
xdmp:quote($node).
Language agnostic solutions:
语言不可知的解决方案:
Node to string is:
节点到字符串是:
fn:string($node)
回答by Sixty4Bit
If you want to create a textnode out of the string, just use a textnode constructor:
如果要从字符串中创建文本节点,只需使用文本节点构造函数:
text { "your string goes here" }
or if you prefer to create an elementwith the string content, you can construct an elementsomething like this:
或者如果你更喜欢用字符串内容创建一个元素,你可以构造一个像这样的元素:
element (some-element) { "your string goes here" }
回答by mb21
If you are talking about strings that contain XML markup, there are standardized solutions (from XPath/XQuery Functions 3.0) as well:
如果您谈论的是包含 XML 标记的字符串,那么也有标准化的解决方案(来自 XPath/XQuery Functions 3.0):
- string to node: fn:parse-xml()
- node to string: fn:serialize()
- 字符串到节点:fn:parse-xml()
- 节点到字符串:fn:serialize()
回答by Jim Burger
The answer to this question depends on what engine is being used. For instance, users of Saxon, use the saxon:parsemethod.
这个问题的答案取决于所使用的引擎。例如,Saxon 的用户,使用该saxon:parse方法。
The fact is the XQueryspec doesn't have a built in for this.
事实是XQuery规范没有为此内置。
Generally speaking you would only really need to use this if you needed to pull some embedded XMLfrom a CDATAsection. Otherwise you can read files in from the filesystem, or declare XMLdirectly inline.
一般来说,如果您需要从CDATA部分提取一些嵌入的XML,您才真正需要使用它。否则,您可以从文件系统中读取文件,或直接内联声明XML。
For the most you would use the declarative form, instead of a hardcoded string e.g. (using Stylus studio)
大多数情况下,您会使用声明形式,而不是硬编码的字符串,例如(使用 Stylus Studio)
declare namespace my = "http://tempuri.org";
declare function my:foo($bar as node()*) as node() {
<unimportant></unimportant>
} ;
let $bar := <node><child></child></node>
return my:foo(bar)

