Using xquery in a xslt stylesheet

Thursday, 7 March 2013 · 1 message
RM
Raymond Meester7 Mar 2013 16:18 #
I thought I share this one, just in case someone needs to call BaseX (xquery the database in this example) from xslt: Greets, Raymond Example:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xsl basex"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:basex="java:org.basex.server.ClientSession">

    <!-- create a new ClientSession object (only static methods can be
called directly) -->
    <xsl:variable name="session" select="basex:new('localhost', 1984,
'admin','admin')"/>

    <xsl:template match="/">

        <!-- putting the xquery in a variable makes it easier to work with
quotes or xpath results from the input xml -->
        <xsl:variable name="query"><xsl:text>XQUERY for $doc in
db:open(&quot;SampleDB&quot;) return $doc</xsl:text></xsl:variable>

        <example>
            <!-- execute the query with a BaseX command -->
            <xsl:value-of
select="basex:execute($session,string($query))"/>
        </example>

    </xsl:template>

</xsl:stylesheet>

⚡Generated with XQuery