Description
The functx:dynamic-path function dynamically evaluates a simple path expression. The function only supports element names and attribute names preceded by @, separated by single slashes. The names can optionally be prefixed, but they must use the same prefix that is used in the input document. It does not support predicates, other axes or other node kinds. Note that most processors have an extension function that evaluates path expressions dynamically in a much more complete way.
Arguments and Return TypeName | Type | Description |
$parent |
node() |
the root to start from |
$path |
xs:string |
the path expression |
return value |
item()* |
XQuery Function DeclarationSee XSLT definition. | declare namespace functx = "http://www.functx.com";
declare function functx:dynamic-path
( $parent as node() ,
$path as xs:string ) as item()* {
let $nextStep := functx:substring-before-if-contains($path,'/')
let $restOfSteps := substring-after($path,'/')
for $child in
($parent/*[functx:name-test(name(),$nextStep)],
$parent/@*[functx:name-test(name(),
substring-after($nextStep,'@'))])
return if ($restOfSteps)
then functx:dynamic-path($child, $restOfSteps)
else $child
} ; |
Exampleslet $in-xml := | <authors>
<author test="abc">
<first>Kate</first>
<last>Jones</last>
</author>
<author>
<first>John</first>
<a:last xmlns:a="http://a">Doe</a:last>
</author>
</authors> | return |
XQuery Example | Results |
---|
functx:dynamic-path(
$in-xml,'author/first') |
<first>Kate</first>
<first>John</first> |
name(functx:dynamic-path(
$in-xml,'author/@test')) |
test |
functx:dynamic-path(
$in-xml,'author') |
<author test="abc">
<first>Kate</first>
<last>Jones</last>
</author>
<author>
<first>John</first>
<a:last xmlns:a="http://a">Doe</a:last>
</author> |
functx:dynamic-path(
$in-xml,'author/a:last') |
<a:last xmlns:a="http://a">Doe</a:last> |
Depends OnSee AlsoHistory |
Recommended Reading:
|