Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>I. In XSLT 2.x / XPath 2.x one can use the functions <a href="http://www.w3.org/TR/xpath-functions/#func-tokenize" rel="nofollow noreferrer">tokenize()</a> and <a href="http://www.w3.org/TR/xpath-functions/#func-index-of" rel="nofollow noreferrer">index-of()</a> to produce the desired results with one-liner XPath expressions</strong>:</p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="text"/&gt; &lt;xsl:param name="pWord" select="'three'"/&gt; &lt;xsl:template match="text()"&gt; &lt;xsl:sequence select= "tokenize(., ',\s*') [index-of(tokenize(current(), ',\s*'), $pWord) -1]"/&gt; &lt;xsl:sequence select= "tokenize(., ',\s*') [index-of(tokenize(current(), ',\s*'), $pWord) +1]"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the following XML document</strong>:</p> <pre><code>&lt;t&gt;One, two, three, four&lt;/t&gt; </code></pre> <p><strong>the wanted, correct result is produced</strong>:</p> <pre><code>two four </code></pre> <p><strong>II. XSLT 1.0 solution</strong></p> <p>It is possible to solve the same task in XSLT 1.0 using the <code>strSplit-to-Words</code> template of <strong><a href="http://fxsl.sf.net" rel="nofollow noreferrer">FXSL</a></strong>.</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common" &gt; &lt;xsl:import href="strSplit-to-Words.xsl"/&gt; &lt;xsl:output method="text"/&gt; &lt;xsl:param name="pWord" select="'three'"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:variable name="vrtfwordNodes"&gt; &lt;xsl:call-template name="str-split-to-words"&gt; &lt;xsl:with-param name="pStr" select="/"/&gt; &lt;xsl:with-param name="pDelimiters" select="', &amp;#9;&amp;#10;&amp;#13;'"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:variable&gt; &lt;xsl:variable name="vwordNodes" select="ext:node-set($vrtfwordNodes)/*"/&gt; &lt;xsl:variable name="vserchWordPos" select= "count($vwordNodes [. = $pWord]/preceding-sibling::* ) +1"/&gt; &lt;xsl:value-of select= "concat($vwordNodes[$vserchWordPos -1], ' ', $vwordNodes[$vserchWordPos +1] ) "/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the same XML document</strong>:</p> <pre><code>&lt;t&gt;One, two, three, four&lt;/t&gt; </code></pre> <p><strong>the wanted, correct result is produced</strong>:</p> <pre><code>two four </code></pre>
 

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