Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>I. You may do something like this in XSLT 2.0</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="s"&gt; &lt;xsl:variable name="vWords" select= "tokenize(lower-case(string(.)), '[\s.?!,;—:\-]+' ) [.] "/&gt; &lt;xsl:sequence select= " for $current in ., $i in 1 to count($vWords) return if($vWords[$i] eq 'blood' and $vWords[$i+1] eq 'pressure' ) then . else () "/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="text()"/&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this XSLT 2.0 transformation is applied to the following XML document</strong> (no such document provided in the question!!!):</p> <pre><code>&lt;t&gt; &lt;s&gt;He has high blood pressure.&lt;/s&gt; &lt;s&gt;He has high Blood Pressure.&lt;/s&gt; &lt;s&gt;He has high Blood Pressure.&lt;/s&gt; &lt;s&gt;He was coldblood Pressured.&lt;/s&gt; &lt;/t&gt; </code></pre> <p><strong>the wanted, correct result</strong> (only elements containing `"blood" and "pressure" (case-insensitive and as two adjacent words) <strong>is produced</strong>:</p> <pre><code>&lt;s&gt;He has high blood pressure.&lt;/s&gt; &lt;s&gt;He has high Blood Pressure.&lt;/s&gt; &lt;s&gt;He has high Blood Pressure.&lt;/s&gt; </code></pre> <p><strong>Explanation</strong>:</p> <ol> <li><p>Using the <code>tokenize()</code> function to split on strings of nn-letter characters, with flags for case-insensitivity and multi-line mode.</p></li> <li><p>Iterating through the result of <code>tokenize()</code> to find a <code>"blood"</code> word followed immediately by a <code>"pressure"</code> word.</p></li> </ol> <hr> <p><strong>II. An XSLT 1.0 solution</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:variable name="vUpper" select= "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/&gt; &lt;xsl:variable name="vLower" select= "'abcdefghijklmnopqrstuvwxyz'"/&gt; &lt;xsl:variable name="vSpaaaceeees" select= "' ' "/&gt; &lt;xsl:variable name="vAlpha" select="concat($vLower, $vUpper)"/&gt; &lt;xsl:template match="s"&gt; &lt;xsl:variable name="vallLower" select="translate(., $vUpper, $vLower)"/&gt; &lt;xsl:copy-of select= "self::* [contains (concat (' ', normalize-space (translate($vallLower, translate($vallLower, $vAlpha, ''), $vSpaaaceeees)), ' ' ), ' blood pressure ' ) ] "/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="text()"/&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when this transformation is applied on the same XML document (above), the same correst result is produced</strong>:</p> <pre><code>&lt;s&gt;He has high blood pressure.&lt;/s&gt; &lt;s&gt;He has high Blood Pressure.&lt;/s&gt; &lt;s&gt;He has high Blood Pressure.&lt;/s&gt; </code></pre> <p><strong>Explanation</strong>:</p> <ol> <li><p>Converting to lowercase. </p></li> <li><p>Using the <em>double-translate</em> method to replace any non-alpha character to a space.</p></li> <li><p>Then using <code>normalize-space()</code> to replace any group of adjacent spaces with a single space.</p></li> <li><p>Then surrounding this result with spaces.</p></li> <li><p>Finally, verifying if the current result contains the string <code>" blood pressure "</code>.</p></li> </ol>
 

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