[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] When to use conditional constructions?


Subject: Re: [xsl] When to use conditional constructions?
From: David Rudel <fwqhgads@xxxxxxxxx>
Date: Tue, 1 Apr 2014 11:14:44 +0200

On Mon, Mar 31, 2014 at 7:15 PM, Abel Braaksma (Exselt) <abel@xxxxxxxxxx> wrote:

> In 3.0 you can assign function items to variables, allowing something
> like this:
>
> <xsl:variable name="predicates" as="function(*)*">
>    <xsl:sequence select="
>       function($a) { $a/local-name() = 'para' },
>       function($a) { $a/text() = 'hello world'}" />
> </xsl:variable>
>
> <xsl:template match="*[some $f in $predicates satisfies $f(.)]">
>     <xsl:text>Found it!</xsl:text>
> </xsl:template>
>
> Whether this proves to be handy in practical scenarios, I am not sure.

A similar technique has been very helpful for me in my practical,
day-to-day work. I say "similar" because for me the main benefit has
not been to reduce re-use of predicates but to use function-valued
parameters to make my code more modular so I can put complex templates
that might use one of these complex predicates into an imported
stylesheet. I can then call them with a small template in my main
stylesheet and pass them whatever predicate is needed for the task at
hand.

Here is how it works for me:

I run analysis on data associated to students. A given data set may
have half a million students in it, but for a given *analysis* I might
only want to inspect 50,000 or 100,000 students. To avoid time lost
parsing files I don't want, I use the following workflow:

1. When I get my data, I first create a text file catalog with one
line per file.
2. I then run a catalog-making XSLT script that inspects each of these
files and writes out some key information about the student into a
catalog file. This information will later be used to determine whether
that student is included or excluded from a given data analysis
project.
3. At the start of most of my analyses, I call a template that
includes a couple of filter parameters that take functions. For
example:
    <xsl:template name="Batch_from_Catalog">
       <xsl:param name="student.filter" select="function($x as node())
as xs:boolean{($x/@userName ne '') and not($x/@firstGradeLevelID='0')
and (+$x/@firstGradeLevelID le 6)}"/>
       <xsl:param name="stint.filter"
select="function($x){boolean(1)}" as="function(*)"/>
....

These filter functions are then used to select the relevant students
from the catalog:

<xsl:for-each select="document(concat($path,$catalog))//student[$student.filter(.)]">
...

When calling the above template, I can enter whatever filters I want
as functions for the $student.filter and $stint.filter variables.

The above template (or, rather, a more complex version that both
selects students for analysis and and organizing the output from each
student's analysis a single data brick) is contained in an imported
stylesheet.

To use it I just have to put a small-footprint initial template
(including the filters I want to use for that analysis) into the sheet
specifying the analysis I'm doing at the moment.

-David

-- 

"A false conclusion, once arrived at and widely accepted is not
dislodged easily, and the less it is understood, the more tenaciously
it is held." - Cantor's Law of Preservation of Ignorance.


Current Thread
Keywords