Search code examples
xslt

XSL store node-set in variable


(XSLT 1.0.) Given a variable called Rows which contains the following (example):

Input

<AllResults>
    <Result>
      <subject>can be filtered by filter 1</subject>
      <type>can be filtered by filter 2</type>
      <date>can be filtered by filter 3</date>
    </Result>
    <Result> ...
    </Result>
</AllResults>

I have 3 filter variables. For each filter, I'd like to apply the filter to the input shown above if the filter variable is not empty. I'd like to store the filtered result, the items that match the filters, into a new variable. I tried the following, but I got an error message about it (filterResult) being a "result tree instead of a node-set". The Rows variable is a node-set, as I have determined by using a debugger.

Part of the XSL

<xsl:variable name="filterResult">
    <xsl:choose>
        <xsl:when test="$filter1 != '' and $filter2 != '' and $filter3 != ''">
            <xsl:copy-of select="$Rows[date=$filter1 and type=$filter2 and subject=$filter3]" />
        </xsl:when>
        <xsl:when test="$filter1 != '' and $filter2 != ''">
            <xsl:copy-of select="$Rows[date=$filter1 and type=$filter2]" />
        </xsl:when>
        <xsl:when test="$filter1 != '' and $filter3 != ''">
            <xsl:copy-of select="$Rows[date=$filter1 and subject=$filter3]" />
        </xsl:when>
        <xsl:when test="$filter3 != '' and $filter2 != ''">
            <xsl:copy-of select="$Rows[type=$filter2 and subject=$filter3]" />
        </xsl:when>
        <xsl:when test="$filter1 != ''">
            <xsl:copy-of select="$Rows[date=$filter1]" />
        </xsl:when>
        <xsl:when test="$filter3 != ''">
            <xsl:copy-of select="$Rows[subject=$filter3]" />
        </xsl:when>
        <xsl:when test="$filter2 != ''">
            <xsl:copy-of select="$Rows[type=$filter2]" />
        </xsl:when>
        <xsl:otherwise> 
            <xsl:copy-of select="$Rows" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

I realize that copy-of produces a result tree and not a node-set, but I am not sure HOW to produce a node set given my 3 filters requirement that I described above.

Additional Info

I do know that I could do something like <xsl:variable name="me" select="/set/node"/> which would create a variable containing a node set but I don't see how that helps me, since I have a lot of possible conditions (given the three filters).


Solution

  • It looks like $Rows variable is an instance of type Result Tree Fragment.

    You can't perform any operation (like the one provided by [] filter expression) on RTF other than string operations: from https://www.w3.org/TR/xslt-10/#section-Result-Tree-Fragments

    An operation is permitted on a result tree fragment only if that operation would be permitted on a string (the operation on the string may involve first converting the string to a number or boolean). In particular, it is not permitted to use the /, //, and [] operators on result tree fragments.

    Besides that, all the showed code could be simplified if $Rows is an instance of node set data type like this:

    <xsl:variable name="filterResult" 
                  select="$Rows[(date=$filter1 or $filter1='')
                                   and 
                                (type=$filter2 or $filter2='')
                                   and 
                                (subject=$filter3 or $filter3='')]"/>
    

    There are XSLT processor (mostly every one) that provide implementations of node-set() extension function for RTF to node set conversion.