[
Lists Home |
Date Index |
Thread Index
]
- To: "Mark Heieis" <mark.heieis@shaw.ca>, <xml-dev@lists.xml.org>
- Subject: RE: [xml-dev] Dynamically creating a pattern for xsl:apply-templates select
- From: "Hunsberger, Peter" <Peter.Hunsberger@STJUDE.ORG>
- Date: Fri, 30 Jul 2004 09:59:27 -0500
- Thread-index: AcR1vrxF6R3+jH/jTzq6QQw9N0u/6gAhdu9g
- Thread-topic: [xml-dev] Dynamically creating a pattern for xsl:apply-templates select
Mark Heieis <mark.heieis@shaw.ca> writes:
>
> I have some challenges to solve:
>
> I need to parse a string that contains n number of args,
> which is in the
> form of "arg1, arg2,...argn". The args define either an attribute or
> element value. The goal is to prune a large tree using the
> given arguments.
>
> I want to dynamically create a pattern for the following
> using the above
> input string, such that
>
> <xsl:apply-template select=path/node[@name=$arg1 or
> @name=$arg2 or
> @name=$argn]/>
>
> is made to look something like
>
> ...
> <xsl: variable name="pattern">@name=$arg1 or @name=$arg2 or
> @name=$argn</xsl:pattern>
> or
> <xsl: variable name="pattern">@name='$arg1 | $arg2 |
> $argn'</xsl:pattern>
> ....
>
> which would then be used as
> ...
> <xsl:template match='somecondition''>
> <xsl:apply-templates select=path/node[$pattern]/>
> </xsl:template>
>
> The questions are:
> Can the select attribute accept pattern defined by a
> variable? So far I
> haven't been able to make it work.
> If yes, how should it be constructed based on the given input
> string? If no, what would be an approach to filter a tree
> based on the given input? Or even if yes, is there a better
> way to filter a large tree?
You don't say what tools you have access to or are willing to use, but
I'll outline one approach you can use. The general issue here is
traversing the arguments and the suggestion of building an intermediate
style sheet doesn't really help you if you don't know in advance how
many arguments you are going to get. If that's the case I'd convert the
arguments into a node set. Two ways to do this: an XSLT extension (if
you're willing to do that), or some pre-processor (in procedural code)
that builds an XML file (or SAX stream) of some kind out of them. Once
you've got a node set filtering one node set based on the contents of
another node set is a pretty straight forward process.
If you've got a lot of this kind of thing to do then you may want to
look at the Apache Cocoon project. It, for example, includes a standard
component to turn HTTP requests parameters into a SAX stream that can be
aggregated with a XML file reader component Sax stream and the combined
output can then be fed into a regular XSLT, no Java coding required.
However, be forewarned, jumping into Cocoon isn't exactly trivial; it's
a big piece of machinery meant for solving complex problems..
|