0
votes

I am trying to evaluate a dynamic xpath using ixsl:eval() in Saxon-ce with xslt 2.0 but does not seem to be working. Here is the illustrative XML

<things>
  <thing>
    <name>widget one</name>
    <number>10</number>
    <type>metal</type>
    <subtypes>
      <subtype>red<subtype>
    </subtypes>
  </thing>
  <thing>
    <name>widget two</name>
    <number>11</number>
    <type>wood</type>
    <subtypes>
      <subtype>red</subtype>
      <subtype>blue</subtype>
    </subtypes>
  </thing>
</things>

and a piece of the xsl 2.0 style sheet I am trying to eval (the various parameters are passed by another part of the larger xsl stylesheet)

<template name="display" match="things">
  <xsl:param name="num"/>
  <xsl:param name="type" as="xs:string"/>
  <xsl:param name="subtype" as="xs:string"/>

  <xsl:variable name="xpathExp" as="xs:string">
     <xsl:text>things/thing</xsl:text>
     <xsl:if test="not($num = 'all')>
       <xsl:copy-of select="concat('[number=',$num,']')"/>
     </xsl:if>
     <xsl:if test="not($type = 'all')>
        <xsl:copy-of select="concat('[type=''',$type,''']')"/>
     </xsl:if>
     <xsl:if test="note($subtype = 'all')>
         <xsl:copy-of select="concat('[subtype/subtypes=''',$subtype,''']')/>
     </xsl:if>
   </xsl:variable>

   <xsl:result-document href="#display" method="ixsl:replace-content">

   <xsl:for-each select="ixsl:eval($xpathExp)">
      <xsl:sort select="name"/>
   </xsl:for-each>

</template>

When I replace the eval statement with a explicit xpath statement the code works, so for some reason the eval on $xpathExp is not working. Ideas?

* Edit * Here is a better XML example:

<things>
  <thing>
    <name>widget one</name>
    <number>10</number>
    <type>metal</type>
    <subtypes>
      <subtype>red<subtype>
    </subtypes>
  </thing>
  <thing>
    <name>widget two</name>
    <number>11</number>
    <type>wood</type>
    <subtypes>
      <subtype>red</subtype>
      <subtype>blue</subtype>
    </subtypes>
  </thing>
  <thing>
    <name>widget three</name>
    <number>11</number>
    <type>metal</type>
    <subtypes>
      <subtype>blue</subtype>
    </subtypes>
  </thing>
</things>

The user can select values via dropboxes for number, type and subtype. Depending on the user's selection, a list of thing names is displayed. So for example, if the user selects a number of 11 and the subtype of red it would just display widget two. If they select instead a subtype of blue it would display the name of widgets two and three.

So the base xpath filter is things/thing. If the user selects a number value I want to append [number=$num] to the xpath expression so ti would be things/thing[number=$num]. If they select more than one item, lets say number and type, [number=$num][type=$type] would be appended to the base xpath and I would have things/thing[number=$num][type=$type].

Basically what I trying to avoid is having to individually code all possible permutations and combinations of possible user selections.

Does that help?

1

1 Answers

0
votes

The ixsl:eval function is for dynamic evaluation of JavaScript, not XPath. From the Saxon-CE documentation:

...Executes Javascript code, supplied as a string. The supplied script is injected into the DOM as a function which is evaluated immediately to return a result.

For the code sample you provide, dynamic evaluation is not required. Only certain classes of XSLT application actually require dynamic XPath evaluation.

In the few cases where dynamic XPath evaluation is required, Saxon-CE has no built-in capability, so instead of just building an XPath dynamically, you would need to generate a simple 'wrapper' XSLT and execute this using the Saxon-CE JavaSCript API. This technique is used in the PathEnq online XPath 2.0 evaluator.

The following code achieves something similar to your code using a single 'static' XPath expression - in this example, a further template is applied to the required 'thing' element to create usable HTML:

rendered code (syntax-highlighted and formatted)

enter image description here

source code:

<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT"
xmlns:js="http://saxonica.com/ns/globalJS"
xmlns:prop="http://saxonica.com/ns/html-property"
xmlns:style="http://saxonica.com/ns/html-style-property"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs prop"
extension-element-prefixes="ixsl"
version="2.0"
>
<xsl:template name="main" match="/">
<xsl:call-template name="display"/>
</xsl:template>

<xsl:template name="display">
<xsl:param name="num" as="xs:string" select="'none'"/>
<xsl:param name="type" as="xs:string" select="'all'"/>
<xsl:param name="subtype" as="xs:string" select="'red'"/>

<xsl:variable name="thingsEl" select="things" as="element(things)"/>

<xsl:variable name="widget" as="element()+"
select="if ($num eq 'all')
then $thingsEl/thing[number = $num]
else if ($type ne 'all')
    then $thingsEl/thing[type = $type]
else $thingsEl/thing[subtypes/subtype = $subtype]"/>

<xsl:result-document href="#display" method="append-content">
<xsl:apply-templates select="$widget">
<xsl:sort select="name"/>
</xsl:apply-templates>
</xsl:result-document>

</xsl:template>

<xsl:template match="thing">
<ol>
<li>name: <xsl:value-of select="name"/></li>
<li>number: <xsl:value-of select="number"/></li>
<li>type: <xsl:value-of select="type"/></li>
<li>subtypes: 
<ol>
<xsl:apply-templates select="subtypes/subtype"/>
</ol>
</li>
</ol>
</xsl:template>

<xsl:template match="subtype">
<li><xsl:value-of select="."/></li>
</xsl:template>

</xsl:transform>

The HTML output (outerHTML view of the 'display' div element)

<div id="display">
    <ol>
        <li>name: widget one</li>
        <li>number: 10</li>
        <li>type: metal</li>
        <li>subtypes: 
            <ol>
                <li>red</li>
            </ol></li>
    </ol>
    <ol>
        <li>name: widget two</li>
        <li>number: 11</li>
        <li>type: wood</li>
        <li>subtypes: 
            <ol>
                <li>red</li>
                <li>blue</li>
            </ol></li>
    </ol>
</div>