Select template for execution using a condition including variable on apply-templates - xslt

I have a template that looks like below
<xsl:template match="more-info" mode="docuSection">
html
</xsl:template>
and which is applied with the call
<xsl:apply-templates select="." mode="docuSection"/>
so the template is applied when the current node has more-info element, is there a way to make this template get applied with the same call and with the condition which includes a global variable e.g. match="$mode='edit' or more-info"
Best Regards,
Keshav

is there a way to make this template
get applied with the same call and
with the condition which includes a
global variable e.g.
match="$mode='edit' or more-info"
In XSLT 2.0 this is perfectly legal:
<xsl:template match="more-info[$mode = ('edit', 'more-info')]"
mode="docuSection">
In XSLT 1.0 it is forbidden to use variable or key references within a match pattern.
However, one can use either of the following techniques:
I. Within the <xsl:apply-templates> instruction specify the exact node-list of nodes to be processed.
<xsl:apply-templates mode="docuSection"
select="self::*[$mode = 'edit' or $mode='more-info']" />
||. Make the match pattern more general, but do any processing within the template only if the desired condition is fulfilled:
<xsl:template match="more-info" mode="docuSection">
<xsl:if test="$mode = 'edit' or $mode='more-info'">
html
</xsl:if>
</xsl:template>

Related

XSLT 2.0 / XPATH testing for parent element in specific position

In XSLT 2.0 and XPATH, within <xsl:template match="lb">, I am testing for a variety of different case where each case produces different HTML output (using xsl:choose/xsl:when).
I want to test for the following situation, where lb is the very first node of any sort inside seg element:
<seg><lb break="n"/>text</seg>
By contrast, these tests would fail:
<seg>text<lb break="n"/>text</seg>
<seg><foo/><lb break="n"/>text</seg>
I've tried combining parentand position() but it's not testing correctly.
Many thanks.
Having a template matching lb and then using xsl:choose/when in my view can be solved more elegantly and compact with precise match patterns e.g. xsl:template match="seg/node()[1][self::lb]" would match any first child node of a seg parent where the child is an lb element. For other conditions you would set up different templates with different match patterns.
But you can use . is parent::seg/node()[1] inside the xsl:template match="lb" to write it the other way around if needed.
/seg/child::node()[1]/name() = 'lb'
check if first child is named "lb"
You can simply test if there are no preceding siblings:
<xsl:when test="not(preceding-sibling::node())">
Do note that node() includes comments and processing instructions too, not just elements and text.
Alternatively, if you have a template matching seg where you do something like this...
<xsl:template match="seg">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
Then, because <xsl:apply-templates /> is short for <xsl:apply-templates select="node()" /> you could use position() in your template
<xsl:when test="position() = 1">
This would not work if the "seg" template did <xsl:apply-templates select="lb" /> though.
See http://xsltfiddle.liberty-development.net/nc4NzRd for an example of the tests in action.

XSLT static key declaration using sequence constructor instead of #use?

XSLT2.0 seems to allow declaring key inline, inside the <key> element.
All the examples I have seen declare an intermediate XML fragment and match on that, using #use. I think that is wasteful.
Can you please provide an example of a XSLT 2.0 key declaration using sequence constructor inside the key element rather than #use?
Usually the value that you want to index is a very simple function of the objects being indexed, so the #use attribute works perfectly well. You can use a contained sequence constructor for more complex cases if you need to, but I've very rarely seen it needed. For example you might want to index sections by their section number like this:
<xsl:key name="k" match="section">
<xsl:number level="multi" count="section" format="1.1.1"/>
</xsl:key>
I don't know what makes you think that using the #use attribute is "wasteful".
I don't think I have used that feature so far and I can't think of a good sample for an obvious use case but let's assume foo elements have some value child elements and we want to sort the value elements and only key on the first or last few in sort order so we could use e.g.
<xsl:key name="by-first-three-values" match="foo">
<xsl:for-each select="value/xs:decimal(.)">
<xsl:sort select="."/>
<xsl:if test="position() lt 4">
<xsl:sequence select="."/>
</xsl:if>
</xsl:for-each>
</xsl:key>
Of course you could avoid that use by writing a function that sorts with perform-sort and then call that function in use="mf:sort(value)[position() lt 4]" but I guess there is at least the flexibility to do it inline of the xsl:key.
What I am after is even more simple, something similar to:
<xsl:key name="AcronymKey" match="a:acronymItem" use="a:acronym"/>
<xsl:template name="AcronymnStandsFor">
<xsl:param name="acronym"/>
<!-- change context to current document so the key will work -->
<xsl:for-each select="document('')">
<xsl:value-of select="key('AcronymKey',$acronym)/a:standsFor"/>
</xsl:for-each>
</xsl:template>
<a:acronymList>
<a:acronymItem>
<a:acronym>Ant</a:acronym>
<a:standsFor>Another Neat Tool</a:standsFor>
</a:acronymItem>
</a:acronymList>
But where the actual key is inside the key element. Is that possible, given the syntax?

Is there a better way than xsl:variable to refer to an attribute value inside an XPath expression?

I am using XSLT 2.0 to transform some XML. The source XML looks similar to this:
<AnimalTest>
<AnimalTypes>
<AnimalType name="cat"/>
<AnimalType name="dog"/>
</AnimalTypes>
<Animals>
<Animal name="Sylvester" typeName="cat"/>
<Animal name="Fido" typeName="dog"/>
<Animal name="Tom" typeName="cat"/>
</Animals>
</AnimalTest>
Inside the XSL template to handle AnimalType tags, I want to use the name attribute of the AnimalType inside an XPath expression. The only way I have been able to achieve this, is by introducing a variable that holds the attribute #name and is referred from inside the XPath expression, like this:
<xsl:template match="AnimalType">
<xsl:variable name="typename" select="#name"/>
<xsl:apply-templates select="/AnimalTest/Animals/Animal[#typeName=$typename]"/>
</xsl:template>
This works, but I wonder whether I really have to use this temporary variable. Is there any better way to refer to that #name attribute? It looks like a detour to me.
If you really disliked using the variable, you could use the current() function to refer to the current context node (AnimalType in your case)
<xsl:apply-templates select="/AnimalTest/Animals/Animal[#typeName=current()/#name]"/>
If you had a more complex expression, using a variable can improve readability though, and you could potentially re-use in other places.
One thing to note is that his declaration
<xsl:variable name="typename" select="#name"/>
Is not quite the same as this declaration
<xsl:variable name="typename">
<xsl:value-of select="#name" />
</xsl:variable>
Although both variables will contain the same value. In the latter case (using xsl:value-of) you are creating a copy of the value of the name attribute. In the former case, you are referring to the attribute directly. Therefore using the latter format would be less efficient.
As a slight aside, you may consider using a key here to look up your Animal elements by their typeName
<xsl:key name="AnimalByType" match="Animal" use="#typeName" />
That way, your apply-templates expression can be simplified to just the following
<xsl:template match="AnimalType">
<xsl:apply-templates select="key('AnimalByType', #name)"/>
</xsl:template>

How to call an xslt template which has no name?

My xslt template looks like this:
<xsl:template match="text()">
<xsl:param name="precedingPStyle" select="preceding-sibling::aic:pstyle[position()=1]/#name"/>
</xsl:template>
Is above a valid xslt template? How/when can this template be called? it has no name, only a match and the match has a parameter.
It will be called by xsl:apply-templates when it is the most appropriate template for the node selected. In the absence of any other more specific templates such as match="text()[normalize-space(.)]" this template would be applied for all text nodes.
For parameters, apply-templates supports with-param in exactly the same way as call-template does.
<xsl:apply-templates select="*/text()">
<xsl:with-param name="precedingPStyle" select="'normal'"/>
</xsl:apply-templates>
The with-param select expression is evaluated in the context of the call, not the target node to which the template applies. As with call-template, any parameters that are not set with an explicit with-param will take the default value specified by the select expression on the xsl:param element in the template (which is evaluated in the context of the target, not the call)

How to use an XSL variable as a condition to evaluate XPath?

I have faced an issue when using a variable as a condition for XPath evaluation. I have the following template which works fine:
<xsl:template name="typeReasonDic">
<xsl:variable name="dic" select="$schema//xs:simpleType[#name = 'type_reason_et']"/>
<!-- do something with the variable -->
</xsl:template>
However, when I change it to look like this:
<xsl:template name="typeReasonDic">
<xsl:param name="choose_dic" select="#name = 'type_reason_et'"/>
<xsl:variable name="dic" select="$schema//xs:simpleType[$choose_dic]"/>
<!-- do something with the variable -->
</xsl:template>
it fails to find the desired node.
What I wish to get is a template with a default value for $choose_dic which can be overriden where necessary.
What am I missing here?
UPD: there is this link I found with the description of what I'm trying to do, but it doesn't seem to work for me.
You can't do this directly in XSLT 1.0 or 2.0 without an extension function. The problem is that with
<xsl:template name="typeReasonDic">
<xsl:param name="choose_dic" select="#name = 'type_reason_et'"/>
<xsl:variable name="dic" select="$schema//xs:simpleType[$choose_dic]"/>
<!-- do something with the variable -->
</xsl:template>
the <xsl:param> will evaluate its select expression a single time in the current context and store the true/false result of this evaluation in the $choose_dic variable. The <xsl:variable> will therefore select either all xs:simpleType elements under the $schema (if $choose_dic is true) or none of them (if $choose_dic) is false. This is very different from
<xsl:variable name="dic" select="$schema//xs:simpleType[#name = 'type_reason_et']"/>
which will evaluate #name = 'type_reason_et' repeatedly, in the context of each xsl:simpleType, and select those elements for which the expression evaluated to true.
If you store the XPath expression as a string you can use an extension function such as dyn:evaluate or the XSLT 3.0 xsl:evaluate element if you're using Saxon.
By doing
<xsl:param name="choose_dic" select="#name = 'type_reason_et'"/>
the XSL engine will try to evaluate "#name = 'type_reason_et'" as an XPath expression, and will assign the RESULT to your variable.
You should use the following variable declaration instead:
<xsl:param name="choose_dic">#name = 'type_reason_et'</xsl:param>
This is the default value, but you can override it when you call your template by using xsl:with-param.
XSLT is not a macro language where you might be able to concatenate your code at run-time from strings and then evaluate them dynamically. So in general for your purpose you would need an extension function to evaluate an XPath expression stored in a string or you need to look into a new XSLT 3.0 features like http://www.saxonica.com/documentation/xsl-elements/evaluate.xml.
What is possible in the scope of XSLT 1.0 or 2.0 is doing e.g.
<xsl:param name="p1" select="'foo'"/>
<xsl:variable name="v1" select="//bar[#att = $p1]"/>
where the param holds a value you compare to other value, for instance those in a node like an attribute or element node.