Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>A not so verbose, pure "push style" XSLT 1.0 solution that doesn't use a hardcoded string for the name of the generated attribute</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="cars"&gt; &lt;records&gt; &lt;xsl:apply-templates/&gt; &lt;/records&gt; &lt;/xsl:template&gt; &lt;xsl:template match="car"&gt; &lt;record&gt; &lt;xsl:apply-templates/&gt; &lt;/record&gt; &lt;/xsl:template&gt; &lt;xsl:template match="entrydata"&gt; &lt;xsl:attribute name="{@name}"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:attribute&gt; &lt;/xsl:template&gt; &lt;xsl:template match="text"&gt; &lt;xsl:if test="position() &gt;1"&gt;, &lt;/xsl:if&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the provided XML document:</strong></p> <pre><code>&lt;cars&gt; &lt;car&gt; &lt;entrydata columnnumber="4" name="Colour"&gt; &lt;text&gt;Red&lt;/text&gt; &lt;/entrydata&gt; &lt;/car&gt; &lt;car&gt; &lt;entrydata columnnumber="4" name="Colour"&gt; &lt;textlist&gt; &lt;text&gt;Yellow&lt;/text&gt; &lt;text&gt;Blue&lt;/text&gt; &lt;/textlist&gt; &lt;/entrydata&gt; &lt;/car&gt; &lt;/cars&gt; </code></pre> <p><strong>the wanted, correct result is produced</strong>:</p> <pre><code>&lt;records&gt; &lt;record Colour="Red"/&gt; &lt;record Colour="Yellow, Blue"/&gt; &lt;/records&gt; </code></pre> <p><strong>Explanation</strong>:</p> <p>Proper use of templates, pattern matching, AVTs and the <code>position()</code> function.</p> <hr> <p><strong>II. A simpler, XSLT 2.0 solution</strong></p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="cars"&gt; &lt;records&gt; &lt;xsl:apply-templates/&gt; &lt;/records&gt; &lt;/xsl:template&gt; &lt;xsl:template match="car"&gt; &lt;record&gt; &lt;xsl:apply-templates/&gt; &lt;/record&gt; &lt;/xsl:template&gt; &lt;xsl:template match="entrydata"&gt; &lt;xsl:attribute name="{@name}"&gt; &lt;xsl:value-of select=".//text" separator=", "/&gt; &lt;/xsl:attribute&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the same XML document (above), the same correct result is produced:</strong></p> <pre><code>&lt;records&gt; &lt;record Colour="Red"/&gt; &lt;record Colour="Yellow, Blue"/&gt; &lt;/records&gt; </code></pre> <p><strong>Explanation</strong>:</p> <p>Proper use of templates, pattern matching, AVTs and the <code>separator</code> attribute of <code>xsl:value-of</code></p>
 

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