Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xquery function for Xslt key function

Tags:

xquery

I want to convert the xslt key function to a Xquery function . Can anyone help me in this?

like image 477
user1890342 Avatar asked Feb 04 '26 06:02

user1890342


1 Answers

If we have an xsl:key instruction:

<xsl:key name='someName' match="patExpr" use="Expr"/>

and a call to the key() function:

key('someName', someExpr, $someDocNode)

this is equivalent to:

($someDocNode//patExpr)[Expr = someExpr]

So, for any specific key, you need to declare a function (name it my:keySomeName() ) that returns a sequence of nodes and whose body is the above expression.

Example:

If we have this xsl:key instruction:

  <xsl:key name='kNameByVal' match='Name' use='.'/>

and this call to the key() function:

key('kNameByVal', 'Peter', $doc)

then the corresponding XQuery function will have this body:

$doc//Name[. = 'Peter']

In case the second operand of the key() function is a more complex expression, a function that calculates that expression must be passed as the second argument to your key-implementing function (so this is only possible in XQuery 3.0 and up) and we end up with something like this:

declare function my:keyNameByVal($funExpr as function($context as node()) as item()*, 
                                 $currenDoc as document-node()
                                )  as node()*
{
   $currenDoc//Name[. = $funExpr(.) ]
}

A more traditional, non-3.0 way is that the caller calculates the expression and passes the result of this calculation as the first argument to the my:keyNameByVal() function:

declare function my:keyNameByVal($useExpr as item()*, 
                                 $currenDoc as document-node()
                                )  as node()*
{
   $currenDoc//Name[. = $useExpr]
}

}

like image 99
Dimitre Novatchev Avatar answered Feb 06 '26 19:02

Dimitre Novatchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!