Note that there are some explanatory texts on larger screens.

plurals
  1. POHow the "as" attribute of xsl:template affects the result of xsl:apply-templates
    primarykey
    data
    text
    <p>Given this source document:</p> <pre><code>&lt;things&gt; &lt;thing&gt;&lt;duck&gt;Eider&lt;/duck&gt;&lt;/thing&gt; &lt;thing&gt;&lt;duck&gt;Mallard&lt;/duck&gt;&lt;/thing&gt; &lt;thing&gt;&lt;duck&gt;Muscovy&lt;/duck&gt;&lt;/thing&gt; &lt;/things&gt; </code></pre> <p>I require the following output</p> <pre><code>Fat Eider, Fat Mallard, Fat Muscovy </code></pre> <p>which I can indeed get with this XSL transform:</p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" &gt; &lt;xsl:output method="text"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:value-of separator=", "&gt; &lt;xsl:apply-templates select="//duck"/&gt; &lt;/xsl:value-of&gt; &lt;/xsl:template&gt; &lt;xsl:template match="duck" as="xs:string"&gt; &lt;xsl:value-of select="concat('Fat ', .)"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>However, I have three questions:</p> <p><strong>Question 1.</strong> (specific)</p> <p>If I remove <code>as="xs:string"</code> from the duck template, I get the following output:</p> <pre><code> Fat EiderFat MallardFat Muscovy </code></pre> <p>Why? My understanding is that in XSLT 2.0 the result of <code>xsl:apply-templates</code> is always a sequence, and that <code>xsl:value-of</code> inserts its <code>separator</code> between the items in the sequence. So why does the sequence seem to "collapse" when the template has no <code>as</code> attribute? Bonus points for pointing me towards appropriate pages of Michael Kay's excellent "XSLT 2.0 and XPath 2.0, 4th Edition" book.</p> <p><strong>Question 2.</strong> (vague!)</p> <p>As a novice user of XSLT, it seems to me that there are probably many ways to solve this problem. Can you put forward a good solution that takes a different approach? How do you choose between approaches?</p> <p><strong>Question 3.</strong></p> <p>Debugging. Can you recommend how to dump out intermediate results that would indicate the difference between the presence and the absence of the <code>as</code> attribute to the <code>template</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload