dynamic matching

D

Daniel Frey

Hello

I'd like to match a dynamic node, given as a parameter to the
stylesheet. Something like:

<xsl:stylesheet ...>
<xsl:param name="tomatch"/>
<xsl:template match="{$tomatch}">
Hallo
</xsl:template>
</xsl:stylesheet>

However, this seems not to be so simple, as "{" and if I remove the
parentesis the "$" are not allowed in the match value.

Certainly, I could take this as a row xslt and pre-transform it with
another script to merge the value of the variable into the {$tomatch}
location. However, then I would need two steps, one to create the
merged xslt, and another to transform the xml with it.

Is there a way to get it simlier? Any idea how to match dynamic nodes
given in parameters would be highly appreciated.

Thanks in advance.
Daniel Frey
 
J

Joe Kesselman

This is related to the thread "expand parts of an xml, which is
transformed by xslt to html". Either use the EXSLT
dynamic-XPath-evaluation extensions (if available), or figure out a way
to express your selection request in a form that lets you write an
interpreter in XSLT to evaluate it.

EXSLT is supported by many XSLT processors these days... but not all.
Depending on what you're trying to do, that possible loss of portability
may or may not be a problem.
 
D

Daniel Frey

Thanks Joe for the hint. I looked at EXSLT and tried node-set(),
evaluate() and document(). But still, the parser does complain about
"$" in the match value. I read in Michael Kays XSLT 2nd Edition that
any sort of variable is disalowed in the match attribute to prevent
circular definitions. So it seem that there is no way to use the match
attribute.

Maybe there is another way to achive the same result. I'd like to use a
template to handle dynamic structures as described in my first post. Is
there another way to handle dynamic matches?

Daniel
 
D

Dimitre Novatchev

Hi Daniel,

I was mislead by the wording: "match a dynamic node, given as a parameter ",
which sounds as a good description what a higher-order xsl:function does --
with the exception that we should say "apply-templates to a dynamic node,
given as a parameter ".

Reading your post for a second time I understand what you want. One way to
achieve this is, supposing that the $tomatch parameter is defined as a
single node, is to have the following template:

<xsl:template match="node() | @*">

<xsl:choose>
<xsl:when test="generate-id() = generate-id($tomatch)">
Hallo
</xsl:when>
<xsl:eek:therwise>
<xsl:apply-imports/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>


You have to assure this template has the highest import-precedence and that
any other templates are part of stylesheets imported by the stylesheet that
contains the above template. Not very convenient, but possible to do.

In XSLT 2.0 one can use the <xsl:next-match> instruction -- designed to
eliminate the inconvenience of using <xsl:apply-imports> in a case like
this -- the overridden templates that are invoked can now be in the same
stylesheet.

Hope this helped.


Cheers,
Dimitre Novatchev
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,002
Messages
2,570,261
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top