XSLT 2.0 / XPATH testing for parent element in specific position - xslt

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.

Related

xsl 3.0: How to process certain child elements first in xsl:apply-templates, then the remainder (overriding document order)

Assume my xml input is a MFMATR element with a few child elements, such as: TRLIST, INTRO, and SBLIST -- in that document order. I am converting to HTML.
I have a template that matches on the MFMATR element, and wants to run xsl:apply-templates on the 3 child elements, but I want INTRO to be processed first (listed first in the HTML). The other two (TRLIST and SBLIST) should keep their relative document order, as long as INTRO comes before both of them.
So I'd like to run <xsl:apply-templates select="INTRO, *"> but not have INTRO matched twice. (Using this syntax with xsl 3.0 causes dupes for me.) I also don't want to explicitly list every tag in the select expression, so unknown tags will still be processed.
A 2nd real life example is this: <xsl:apply-templates select="TITLE, CHGDESC, *"/>. Again, right now that is causing dupes I don't want.
I am using Saxon.
So I'd like to run <xsl:apply-templates select="INTRO, *"> but not have INTRO matched twice
Try:
<xsl:apply-templates select="INTRO, * except INTRO">
This seems to work. If someone has a better answer, let me know and I will change it.
There is no DRY violation here -- no repeated element names or variable names. I want it to look clean at all the call sites I will have.
It seems idiomatic to me since the function was pulled from w3's own website!
<xsl:template match="MFMATR">
<!-- Process INTRO first, no matter where it appears -->
<xsl:variable name="nodes" select="INTRO, *"/>
<xsl:apply-templates select="kp:distinct_nodes_stable($nodes)"/>
</xsl:template>
<xsl:template match="INTRO">
<xsl:variable name="nodes" select="TITLE, CHGDESC, *"/>
<xsl:apply-templates select="kp:distinct_nodes_stable($nodes)"/>
</xsl:template>
<!-- Discard duplicate elements in $seq, but keep their ordering -->
<!-- Adapted from https://www.w3.org/TR/xpath-functions/#func-distinct-nodes-stable -->
<xsl:function name="kp:distinct_nodes_stable" as="node()*">
<xsl:param name="seq" as="node()*"/>
<xsl:sequence select="fold-left($seq, (),
function($foundSoFar as node()*, $this as node()) as node()* {
if ($foundSoFar intersect $this)
then $foundSoFar
else ($foundSoFar, $this)
}) "/>
</xsl:function>

Debug possible escaped-text issue in XSLT?

I have an XSL template that gives different results when used in two different contexts.
The template manifesting the defect is:
<xsl:template match="*" mode="blah">
<!-- snip irrelevant stuff -->
<xsl:if test="see">
<xsl:message>Contains a cross-ref. <xsl:value-of select="."/></xsl:message>
</xsl:if>
<xsl:apply-templates select="."/>
</xsl:template>
Given:
<el>This is a '<see cref="foo"/>' cross-referenced element.</el>
In one situation, I get the desired result:
Contains a cross-ref. This is a ' ' cross-referenced element.
(the <see/> is being dealt with as an XML element and is ultimately matched by another template.)
But in another situation, the xsl:if doesn't trigger and if I output the contents with <xsl:message><xsl:value-of select="."/>, I get:
This is a '<see cref="foo"/>' cross-referenced element.
It seems to me that in the latter improperly-behaving scenario, it's acting like it's been output-escaped. Does that make sense? Am I barking up the wrong tree? This is a typically complex XSL situation and trying to trace the call-stack is difficult; is there a particular XSLT processing command I should be looking for?

XSLT xsl:sequence. What is it good for..?

I know the following question is a little bit of beginners but I need your help to understand a basic concept.
I would like to say first that I'm a XSLT programmer for 3 years and yet there are some new and quite basics things I've been learning here I never knew (In my job anyone learns how to program alone, there is no course involved).
My question is:
What is the usage of xsl:sequence?
I have been using xsl:copy-of in order to copy node as is, xsl:apply-templates in order to modifiy nodes I selected and value-of for simple text.
I never had the necessity using xsl:sequence. I would appreciate if someone can show me an example of xsl:sequence usage which is preferred or cannot be achieved without the ones I noted above.
One more thing, I have read about the xsl:sequence definition of course, but I couldn't infer how it is useful.
<xsl:sequence> on an atomic value (or sequence of atomic values) is the same as <xsl:copy-of> both just return a copy of their input. The difference comes when you consider nodes.
If $n is a single element node, eg as defined by something like
<xsl:variable name="n" select="/html"/>
Then
<xsl:copy-of select="$n"/>
Returns a copy of the node, it has the same name and child structure but it is a new node with a new identity (and no parent).
<xsl:sequence select="$n"/>
Returns the node $n, The node returned has the same parent as $n and is equal to it by the is Xpath operator.
The difference is almost entirely masked in traditional (XSLT 1 style) template usage as you never get access to the result of either operation the result of the constructor is implicitly copied to the output tree so the fact that xsl:sequence doesn't make a copy is masked.
<xsl:template match="a">
<x>
<xsl:sequence select="$n"/>
</x>
</xsl:template>
is the same as
<xsl:template match="a">
<x>
<xsl:copy-of select="$n"/>
</x>
</xsl:template>
Both make a new element node and copy the result of the content as children of the new node x.
However the difference is quickly seen if you use functions.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="data:,f">
<xsl:variable name="s">
<x>hello</x>
</xsl:variable>
<xsl:template name="main">
::
:: <xsl:value-of select="$s/x is f:s($s/x)"/>
:: <xsl:value-of select="$s/x is f:c($s/x)"/>
::
:: <xsl:value-of select="count(f:s($s/x)/..)"/>
:: <xsl:value-of select="count(f:c($s/x)/..)"/>
::
</xsl:template>
<xsl:function name="f:s">
<xsl:param name="x"/>
<xsl:sequence select="$x"/>
</xsl:function>
<xsl:function name="f:c">
<xsl:param name="x"/>
<xsl:copy-of select="$x"/>
</xsl:function>
</xsl:stylesheet>
Produces
$ saxon9 -it main seq.xsl
<?xml version="1.0" encoding="UTF-8"?>
::
:: true
:: false
::
:: 1
:: 0
::
Here the results of xsl:sequence and xsl:copy-of are radically different.
The most common use case for xsl:sequence is to return a result from xsl:function.
<xsl:function name="f:get-customers">
<xsl:sequence select="$input-doc//customer"/>
</xsl:function>
But it can also be handy in other contexts, for example
<xsl:variable name="x" as="element()*">
<xsl:choose>
<xsl:when test="$something">
<xsl:sequence select="//customer"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="//supplier"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
The key thing here is that it returns references to the original nodes, it doesn't make new copies.
Well to return a value of a certain type you use xsl:sequence as xsl:value-of despite its name always creates a text node (since XSLT 1.0).
So in a function body you use
<xsl:sequence select="42"/>
to return an xs:integer value, you would use
<xsl:sequence select="'foo'"/>
to return an xs:string value and
<xsl:sequence select="xs:date('2013-01-16')"/>
to return an xs:date value and so on. Of course you can also return sequences with e.g. <xsl:sequence select="1, 2, 3"/>.
You wouldn't want to create a text node or even an element node in these cases in my view as it is inefficient.
So that is my take, with the new schema based type system of XSLT and XPath 2.0 a way is needed to return or pass around values of these types and a new construct was needed.
[edit]Michael Kay says in his "XSLT 2.0 and XPath 2.0 programmer's reference" about xsl:sequence: "This innocent looking instruction introduced in XSLT 2.0 has far reaching effects on the capability of the XSLT language, because it means that XSLT instructions and sequence constructors (and hence functions and templates) become capable of returning any value allowed by the XPath data model. Without it, XSLT instructions could only be used to create new nodes in a result tree, but with it, they can also return atomic values and references to existing nodes.".
Another use is to create a tag only if it has a child. An example is required :
<a>
<b>node b</b>
<c>node c</c>
</a>
Somewhere in your XSLT :
<xsl:variable name="foo">
<xsl:if select="b"><d>Got a "b" node</d></xsl:if>
<xsl:if select="c"><d>Got a "c" node</d></xsl:if>
</xsl:variable>
<xsl:if test="$foo/node()">
<wrapper><xsl:sequence select="$foo"/></wrapper>
</xsl:if>
You may see the demo here : http://xsltransform.net/eiZQaFz
It is way better than testing each tag like this :
<xsl:if test="a|b">...</xsl:if>
Because you would end up editing it in two places. Also the processing speed would depend on which tags are in your imput. If it is the last one from your test, the engine will test the presence of everyone before. As $foo/node() is an idioms for "is there a child element ?", the engine can optimize it. Doing so, you ease the life of everyone.

XSLT find whether sibling exist or not

Sample XML is given below.
<mapNode>
<mapNode>...</mapNode>
<mapNode>...</mapNode>-----I am here at 2
<mapNode>...</mapNode>
<mapNode>...</mapNode>
</mapNode>
<mapNode>
<mapNode>...</mapNode>
<mapNode>...</mapNode>
</mapNode>
I want to know whether position 3 exist or not.
Please help me.
Thanks in advance.
If you want to test if an element has a sibling following it, you can use the sensibly named "following-sibling" xpath expression:
<xsl:if test="following-sibling::*" />
Note that this will test if there is any following-sibling. If you only wanted to test for mapNode elements, you could do this
<xsl:if test="following-sibling::mapNode" />
However, this would also be true also in the following case, because following-sibling will look at all following siblings:
<mapNode>
<mapNode>...</mapNode>
<mapNode>...</mapNode>-----I am here at 2
<differentNode>...</differentNode>
<mapNode>...</mapNode>
</mapNode>
If you therefore want to check the most immediately following sibling was a mapNode element, you would do this:
<xsl:if test="following-sibling::*[1][self::mapNode]" />
In addition to #rene's answer you could also use the following-sibling axis from within any mapNode:
<xsl:template match="mapNode">
<xsl:if test="count(following-sibling::mapNode)>0">
<!-- has a successor -->
</xsl:if>
</xsl:template>
Not knowing what you already have but assuming you have a template to select a toplevel mapNode you can use count to findout how many mapNodes there are under thecurrent node:
<xsl:template match="/root/mapNode">
<xsl:if test="count(mapNode)>2">
more than two mapNodes
</xsl:if>
</xsl:template>

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

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>