Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>This short and simple (no explicit conditional instructions) XSLT 1.0 transformation</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:template match="/*"&gt; &lt;fields&gt; &lt;field name="all"&gt; &lt;xsl:variable name="vfieldConcat"&gt; &lt;xsl:for-each select="field/value"&gt; &lt;xsl:value-of select="concat(., ' ')"/&gt; &lt;/xsl:for-each&gt; &lt;/xsl:variable&gt; &lt;value&gt;&lt;xsl:value-of select= "normalize-space($vfieldConcat)"/&gt;&lt;/value&gt; &lt;/field&gt; &lt;/fields&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on the provided XML document (corrected for well-formedness):</strong></p> <pre><code>&lt;fields&gt; &lt;field name="first"&gt; &lt;value&gt;example&lt;/value&gt; &lt;/field&gt; &lt;field name="last"&gt; &lt;value&gt;hello&lt;/value&gt; &lt;/field&gt; &lt;field name="age"&gt; &lt;value&gt;25&lt;/value&gt; &lt;/field&gt; &lt;field name="enable"&gt; &lt;value&gt;1&lt;/value&gt; &lt;/field&gt; &lt;/fields&gt; </code></pre> <p><strong>produces the wanted, correct result:</strong></p> <pre><code>&lt;fields&gt; &lt;field name="all"&gt; &lt;value&gt;example hello 25 1&lt;/value&gt; &lt;/field&gt; &lt;/fields&gt; </code></pre> <hr> <p><strong>II. 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:template match="/*"&gt; &lt;fields&gt; &lt;field name="all"&gt; &lt;value&gt;&lt;xsl:value-of select="field/value"/&gt;&lt;/value&gt; &lt;/field&gt; &lt;/fields&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;fields&gt; &lt;field name="all"&gt; &lt;value&gt;example hello 25 1&lt;/value&gt; &lt;/field&gt; &lt;/fields&gt; </code></pre> <p><strong>Explanation</strong>: Using the fact that the default value for the <strong><a href="http://www.w3.org/TR/2007/REC-xslt20-20070123/#value-of" rel="nofollow"><code>separator</code> attribute of <code>xsl:value-of</code></a></strong> is a single space.</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