[xquery-talk] xquery extension function for xslt

Andrew Welch andrew.j.welch at gmail.com
Tue Sep 4 19:00:42 PDT 2007


I've knocked together a quick n dirty xquery extension function for
use with eXist and Saxon and included it in Kernow.

This sample XSLT calls the runQuery() extension passing in the uri for
eXist, the collection and the query to execute.  The function returns
a sequence of Strings (!) that can then be turned into nodesets using
saxon:parse() (which you wouldn't use if atomics are returned)  Below
it gets all <title> elements with String values start with with 'H',
from the 'mycol' collection:

<xsl:stylesheet version="2.0"
xmlns:k="net.sf.kernow.extensions.XQueryExtension"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:saxon="http://saxon.sf.net/"
extension-element-prefixes="k saxon">

<xsl:param name="db-uri"
select="'xmldb:exist://localhost:8080/exist/xmlrpc/db'"
as="xs:string"/>

<xsl:param name="collection" select="'/mycol'" as="xs:string?"/>

<xsl:param name="query" as="xs:string">
  for $x in //title
  return $x[starts-with(., 'H')]
</xsl:param>


<xsl:template match="/" name="main">

	<xsl:variable name="input" select="k:runQuery($db-uri, $collection, $query)"/>

	<xsl:copy-of select="for $x in $input return saxon:parse($x)"/>
	
</xsl:template>

</xsl:stylesheet


To try it out use the Java Web Start version of Kernow from here:
http://kernowforsaxon.sourceforge.net/jws.html  (may require patience
as the jars download for the first time)

Then use the "Standalone" tab, clicking the auto button to select the
named template.

(obviously you'll need eXist installed too)

I know this is really basic but hopefully you get the idea...

-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/


More information about the talk mailing list