Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Just using <code>contains()</code> may generally produce the wrong results</strong> (see the test XML document).</p> <p>What is necessary is a <a href="http://www.w3.org/TR/xpath-functions/#func-ends-with" rel="nofollow noreferrer"><strong><code>ends-with()</code></strong></a> function, which is standard in XPath 2.0 and can be implemented in XSLT 1.0 as in the following transformation:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="text"/&gt; &lt;xsl:template match="URLFilePath"&gt; &lt;xsl:variable name="visDoc"&gt; &lt;xsl:call-template name="ends-with"&gt; &lt;xsl:with-param name="pEnding" select="'.doc'"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:variable&gt; &lt;xsl:variable name="visXls"&gt; &lt;xsl:call-template name="ends-with"&gt; &lt;xsl:with-param name="pEnding" select="'.xls'"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:variable&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$visDoc=1"&gt;word_small.gif&lt;/xsl:when&gt; &lt;xsl:when test="$visXls=1"&gt;xls_small.gif&lt;/xsl:when&gt; &lt;xsl:otherwise&gt;unknown_small.gif&lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;xsl:template name="ends-with"&gt; &lt;xsl:param name="pEnding"/&gt; &lt;xsl:value-of select= "number(substring(., string-length() -string-length($pEnding) +1 ) = $pEnding ) "/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the following test XML document</strong>:</p> <pre><code>&lt;files&gt; &lt;URLFilePath&gt;myFile.doc&lt;/URLFilePath&gt; &lt;URLFilePath&gt;myFile.xls&lt;/URLFilePath&gt; &lt;URLFilePath&gt;myFile.xls.doc&lt;/URLFilePath&gt; &lt;URLFilePath&gt;myFile.doc.xls&lt;/URLFilePath&gt; &lt;/files&gt; </code></pre> <p><strong>the correct result is produced</strong>:</p> <pre><code> word_small.gif xls_small.gif word_small.gif xls_small.gif </code></pre> <p><strong>Do note</strong> that just using <code>contains()</code> produces incorrect results.</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