3 users online. Create an account or sign in to join them.Users
Is there a way to template-match a variable or parameter?
This is an open discussion with 8 replies, filed under XSLT.
Search
I haven't tested it but shouldn't this work?
<xsl:param name="foo" select="'bar'" />
<xsl:template match="data">
<xsl:apply-templates select="*[local-name() = $foo]" />
</xsl:template>
<xsl:template match="*[local-name() = $foo]">
...
</xsl:template>
But it's not the best way as you need to check all elements using *.
What's the purpose of this setup?
XSLTProcessor::importStylesheet(): Failed to compile predicate
I'm fetching the names and handles of some of my pages from the database. In many places I have something like:
<xsl:value-of select="/data/navigation/page[ @handle = '...' ]/name" />
where navigation is the handle of my navigation datasource. I wanted to set a parameter so I can change the name of the DS with ease.
My top navigation on page is something like:
<xsl:apply-templates select="/data/*[ local-name() = $foo ]" mode"header" /> <xsl:template match="navigation"> ...
I'm trying to match this param.
Question: is the XSLT processing a large overhead or just a minor thing?
So you have different navigation data sources, e. g. navigation, subnavigation, anothernavigation, and would like to apply the same template to all of these?
In this case you could do something like this:
<xsl:apply-templates select="/data//page/name" />
and match name in your template.
@vladG - would you mind posting your xml and xslt?
I'm not sure I'm following along here, but it may be of some help to remember that you can have a template match more than one element. You do however, need to know the name of those elements before hand.
<xsl:template match="one | two | three" />
You do however, need to know the name of those elements before hand.
Just to second Lewis, you can't use variables in a match value (I remember something about that being a compile-time value, but can't recall the specifics). See #13 here for a little more info.
I have made a post on this a long time ago but I can't seem to find it anymore.
You might be wondering why you cannot use variables insidetemplate match, yet you can use it in an apply-template select instruction. Andrew pointed out the underlying technical reason.
You can take a look at this from a layer above; at the specification level:
Take a look at the XSLT specification, specifically around the concept of patterns. Note that a pattern is different to an XPath Expression.
Here is the spec for the template element:
<xsl:template match = pattern name = qname priority = number mode = qname> <!-- Content: (xsl:param*, template) --> </xsl:template>
Notice how the match attribute requires a pattern.
And here is the spec for the apply-templates instruction:
<xsl:apply-templates select = node-set-expression mode = qname> <!-- Content: (xsl:sort | xsl:with-param)* --> </xsl:apply-templates>
Whereas the select attribute here looks for a node-set-expression.
Hope that clarifies at least the question why.
Yes it does clarify the "why". Thank you, Allen.
I'll look a bit to what XSLT can't do. Good stuff there.
Create an account or sign in to comment.
This code triggers the error in attachment:
My idea is to set a parameter for the handle of my navigation Datasource, for example.