(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).
In XSLT 1.0, the only way to create a variable containing a set of nodes from the source document is by evaluating an XPath expression in the
select
attribute:You can't use
copy-of
,apply-templates
, orcall-template
; those will all produce a result tree fragment.Every XSLT processor I'm aware of implements an extension function that converts result tree fragments into node sets, so unless you need your transform to be utterly cross-platform, you can do something like this (this example uses Microsoft's XSLT processor):
But you don't need to do even this: as Alejandro has pointed out, you can get the result you're looking for just by writing a single XPath expression. There are applications where node selection is so complicated that you have to use a
node-set()
function, but yours isn't one of them.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 http://www.w3.org/TR/xslt#section-Result-Tree-FragmentsBesides that, all the showed code could be simplified if
$Rows
is an instance of node set data type like this:There are XSLT processor (mostly every one) that provide implementations of
node-set()
extension function for RTF to node set conversion.