Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The currently accepted solution always displays the long days names in chronological order and in addition, it doesn't display repeating (with same code) days.</p> <p><strong>Suppose we have the following XML document</strong>:</p> <pre><code>&lt;days&gt;STMSU&lt;/days&gt; </code></pre> <p><strong>I. This XSLT 1.0 transformation</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my" exclude-result-prefixes="my" &gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;my:days&gt; &lt;M&gt;Monday&lt;/M&gt; &lt;T&gt;Tuesday&lt;/T&gt; &lt;W&gt;Wednesday&lt;/W&gt; &lt;R&gt;Thursday&lt;/R&gt; &lt;F&gt;Friday&lt;/F&gt; &lt;S&gt;Saturday&lt;/S&gt; &lt;U&gt;Sunday&lt;/U&gt; &lt;/my:days&gt; &lt;xsl:key name="kLongByShort" match="my:days/*" use="name()"/&gt; &lt;xsl:variable name="vstylesheet" select="document('')"/&gt; &lt;xsl:template match="days"&gt; &lt;long-days&gt; &lt;xsl:call-template name="expand"/&gt; &lt;/long-days&gt; &lt;/xsl:template&gt; &lt;xsl:template name="expand"&gt; &lt;xsl:param name="pcodeString" select="."/&gt; &lt;xsl:if test="$pcodeString"&gt; &lt;xsl:variable name="vchar" select= "substring($pcodeString,1,1)"/&gt; &lt;xsl:for-each select="$vstylesheet"&gt; &lt;xsl:value-of select= "concat(key('kLongByShort',$vchar), substring(',',1,string-length($pcodeString)-1) ) "/&gt; &lt;/xsl:for-each&gt; &lt;xsl:call-template name="expand"&gt; &lt;xsl:with-param name="pcodeString" select= "substring($pcodeString,2)"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on the above document, produces the wanted, correct result:</strong></p> <pre><code>&lt;long-days&gt;Saturday,Tuesday,Monday,Saturday,Sunday&lt;/long-days&gt; </code></pre> <p><strong>II. This XSLT 2.0 transformation</strong>:</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" exclude-result-prefixes="xs"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:variable name="vshortCodes" as="xs:integer+" select="string-to-codepoints('MTWRFSU')"/&gt; &lt;xsl:variable name="vlongDays" as="xs:string+" select="'Monday','Tuesday','Wenesday','Thursday', 'Friday','Saturday','Sunday' "/&gt; &lt;xsl:template match="days"&gt; &lt;long-days&gt; &lt;xsl:for-each select="string-to-codepoints(.)"&gt; &lt;xsl:value-of separator="" select= "for $pos in position() ne last() return ($vlongDays[index-of($vshortCodes,current())], ','[$pos]) "/&gt; &lt;/xsl:for-each&gt; &lt;/long-days&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on the same XML document</strong>:</p> <pre><code>&lt;days&gt;STMSU&lt;/days&gt; </code></pre> <p><strong>produce the wanted, correct result</strong>:</p> <pre><code>&lt;long-days&gt;Saturday,Tuesday,Monday,Saturday,Sunday&lt;/long-days&gt; </code></pre>
    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.
    1. CO+1 Mandatory XSLT 2.0 solution!
      singulars
      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