Xquery - using variable to find node with namespace prefix
I am trying to find a node (prefixed with a namespace) using variables in my xquery like so:
declare variable $libx_ns as xs:string external;
declare namespace libx=$libx_ns;
declare variable $type as xs:string external;
return doc('xyz')/entry[libx:{$type}][1]
Parameter values: $libx_ns = 'http://libx.org/xml/libx2', $type = libapp. When I run this xquery, I see the following error:
[XPST0003] Incomplete expression.
The above code works if I modify the last line like so:
return doc('xyz')/entry[libx:libapp][1]
The purpose is to use a string variable(e.g. $type) for node-name rath开发者_如何学JAVAer than hard-wiring it (e.g. libapp). How would I do this?
Sample xml document:
<entry xmlns:libx="http://libx.org/xml/libx2">
<libx:libapp> First libapp </libx:libapp>
<libx:libapp> Second libapp </libx:libapp>
</entry>
Thanks, Sony
This XQuery:
declare variable $namespace-uri as xs:string external;
declare variable $local-name as xs:string external;
/entry[*[node-name(.) eq QName($namespace-uri,$local-name)]]
With this input:
<entry xmlns:libx="http://libx.org/xml/libx2">
<libx:libapp> First libapp </libx:libapp>
<libx:libapp> Second libapp </libx:libapp>
</entry>
Setting $namespace-uri
as 'http://libx.org/xml/libx2'
and $local-name
as libapp
, output:
<entry xmlns:libx="http://libx.org/xml/libx2">
<libx:libapp> First libapp </libx:libapp>
<libx:libapp> Second libapp </libx:libapp>
</entry>
If the namespace is not a critical difference, you could try:
return doc('xyz')/entry[*[local-name() = $type]][1]
If it is:
return doc('xyz')/entry[*[local-name() = $type and namespace-uri() = $libx_ns]][1]
精彩评论