Subject: Re: BaseX request from Saxon XSL transformation

Wednesday, 6 January 2016 · 1 message
SN
ski n6 Jan 2016 15:44 #
Hi Christophe, It's possible to query Basex directly from XSLT. However this can be cumbersome because you need a lot of escapes to correctly execute the query. If you only need to query a relative small XML you can avoid this by getting by getting the complete XML and use a normal xpath in the xslt (in stead of xquery): 1) First create a function in a separate XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs basex saxon"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:functx="http://www.functx.com"
        xmlns:org="http://www.example.org"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:saxon="http://saxon.sf.net/"
        xmlns:basex="java:org.basex.server.ClientSession">

    <xsl:function name="org:getBasexDoc">
        <xsl:param name="database"/>
        <xsl:variable name="session" select="basex:new('localhost',
1984, 'admin','admin')"/>
        <xsl:variable name="query">
            <xsl:text>XQUERY for $doc in
db:open('</xsl:text><xsl:value-of select="$database"/><xsl:text>')
return $doc</xsl:text>
            </xsl:variable>
            <xsl:variable name="result">
                <xsl:value-of select="basex:execute($session,string($query))"/>
            </xsl:variable>
        <xsl:copy-of select="saxon:parse($result)"/>
    </xsl:function>
</xsl:stylesheet>
2) Include the stylesheet in you main stylesheet: xsl:include href="basex.xsl"/> 3) Set the XML document in a variable
<xsl:variable name="dbname">
     <xsl:copy-of select="drs:getBasexDoc('dbname')"/>
 </xsl:variable>
4) Query the document with xpath
<xsl:value-of select="$dbname//xpath"/>

⚡Generated with XQuery