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

RE: [xsl] Using a pre-processor for dynamic inputs


Subject: RE: [xsl] Using a pre-processor for dynamic inputs
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Sat, 11 Sep 2004 10:21:38 +0100

There are three approaches to this problem, all of which are quite
respectable and widely used.

(a) if the inputs are fairly predictable in structure, you can write your
stylesheet to interpret the inputs. For example, you might supply a
parameter to the stylesheet of the form:

<conditions>
  <condition>
    <attribute>name</attribute>
    <value>robin</value>
  </condition> 
  <condition>
    <attribute>size</attribute>
    <value>small</value>
  </condition>
</conditions>

and then write logic in your stylesheet to select all the birds that satisfy
all the conditions.

(b) you can generate or modify the stylesheet each time it is used, to
incorporate the specific conditions

(c) you can use an extension xx:evaluate(), available in many XSLT
processors, to intpret an XPath expression that's constructed dynamically.

My preferred approach is (c), but unfortunately it requires use of
vendor-specific extensions.

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: David Adams [mailto:dpadams@xxxxxxxxx] 
> Sent: 11 September 2004 06:05
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Using a pre-processor for dynamic inputs
> 
> I've got another database-person-turning-to-XSLT question. Again, I'm 
> starting with a 200K XML file that includes the names of the birds
> that occur in Australia, as illustrated in the fragment below:
> 
> <Australian_Birds>
> 	<Species>
> 		<Sequence>1</Sequence>
> 		<Family_Name>Struthionidae</Family_Name>
> 		<Genus_Name>Struthio</Genus_Name>
> 		<Species_Name>camelus</Species_Name>
> 		<Common_Name>Ostrich</Common_Name>
> 		<Extinct>False</Extinct>
> 	</Species>
> 	<Species>
> 		<Sequence>2</Sequence>
> 		<Family_Name>Casuariidae</Family_Name>
> 		<Genus_Name>Casuarius</Genus_Name>
> 		<Species_Name>casuarius</Species_Name>
> 		<Common_Name>Southern Cassowary</Common_Name>
> 		<Extinct>False</Extinct>
> 	</Species>
> </Australian_Birds>
> 
> What I'd like to do is to make it possible to use XSLT to produce any
> number of outputs from this source file based on dynamic inputs. For
> example, allowing for a search on genus where there can be a varible
> number of inputs. In one case, the search might be for "Struthio" and
> in another "Struthio" and "Casuarius". Based on my reading and the
> kind answer to my earlier question, I now know that there are a couple
> of ways to build the XSLT to perform this task _if_ the inputs are
> known. But that's the problem, they're dynamic inputs. If I understand
> it correctly, you can supply values through parameters which are
> basically XSLT variables set from the outside world. And, again if I
> understand it correctly, variables/parameters are something between
> constants and variables in other languages, to my way of thinking. In
> my case, the binding time sounds like it's too early.
> 
> Is there a natural solution to this situation in XSLT? Bascially what
> I want is to select a series of node sets based on an input array
> (array/list/vector/whatever.) The solution I came up with is to
> pre-process the XSLT. The unprocessed sheet isn't valid XML as it gets
> fed to a tag processor. Something like this:
> 
> 	<xsl:template match="Australian_Birds">
>                for (each item in an input array)
> 		<xsl:apply-templates
> select='/Australian_Birds/Species[Genus_Name="insert array element
> value here"]'/>
>                end loop
> 	</xsl:template>
> 
> The output is then something like this:
> 
> 	<xsl:template match="Australian_Birds">
> 		<xsl:apply-templates
> select='/Australian_Birds/Species[Genus_Name="Struthio"]'/>
> 		<xsl:apply-templates
> select='/Australian_Birds/Species[Genus_Name="Casuarius"]'/>
> 	</xsl:template>
> 
> If the input array had three elements, there would be three
> xsl:apply-templates statements, and so on.
> 
> This obviously works, but is there a native way of accomplishing the
> same task? If not, is the solution I'm using considered reasonable or
> is it regarded as an abomination?
> 
> Thanks again for any help. And, again, feel free to point me to
> existing discussions I've missed. I've tried searching but may be
> missing what I'm looking for as I don't know the appropriate local
> terminology.
> 
> -- 
> ---------------------------------------------
>  David Adams
>   dpadams@xxxxxxxxx
>  Bermagui 2546 NSW
> ---------------------------------------------
> 
> --+------------------------------------------------------------------
> XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
> --+--


Current Thread
Keywords