0

Environment: eXist-db 4.2.1 , XQuery 3.1, XSLT 2.0

I am required to perform an XSLT transformation within eXist-DB using XQuery. At one point the XSLT needs to search across hundreds of documents for matches on a node attribute value. Calling collection() from XSLT in eXist-DB seems to not work .

I've done some searching on other ways to solve this problem, and having failed to find anything, I'm posting two questions here:

  1. Is is possible to dynamically write and transform XSLT from XQuery, thus allowing me to dynamically inject values from XQuery itself (parameters on xquery transform:transform() don't suffice here)

  2. Is it possible to call/retrieve results from an (eXist) XQuery document/function from XSLT in any way?

Thanks for any opinions and references.

jbrehr
  • 775
  • 6
  • 19

1 Answers1

2

As XSLT is XML and with XQuery you can construct XML you can of course construct XSLT on the fly and inject data you gathered elsewhere in XQuery, the following is a silly example obviously but it constructs some data in XQuery, creates an XSLT stylesheet on the fly injecting some of that data directly inline as a parameter value and then runs the XSLT:

declare namespace xsl = "http://www.w3.org/1999/XSL/Transform";

let $elements := (1 to 3)!<root><data>{.}</data></root>,
    $stylesheet := 
      <xsl:stylesheet version="2.0">
        <xsl:param name="data-elements" as="element()*">{$elements!data}</xsl:param>
        <xsl:template match="@* | node()">
          <xsl:copy>
             <xsl:apply-templates select="@* | node()"/>
          </xsl:copy>
        </xsl:template>
        <xsl:template match="foo[. = $data-elements]"/>
      </xsl:stylesheet>,
    $input := <root><list><foo>a</foo><foo>2</foo><foo>10</foo><foo>1</foo></list></root>
return transform:transform($input, $stylesheet, ())
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110