Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>The expired <code>&lt;item&gt;</code> elements can be selected by a single XPath expression</strong>, provided the current date is provided -- either in a variable, or as a literal string.</p> <p><strong>The solution below uses XSLT 1.0 as the hosting language for XPath 1.0</strong>. </p> <p><strong>For convenience, the current date is specified</strong> as a global <code>&lt;xsl:param&gt;</code> parameter, named <code>pToday</code>.</p> <p><strong>Another convenience is that the values for the current year, month and day are defined</strong> in the variables <code>$vthisYear</code>, <code>$vthisMonth</code>, and <code>$vthisDay</code>. If necessary, al these variable references can be substituted in the XPath expression with the right-hand-side of their definitions.</p> <p>The wanted single XPath expression is wrapped-up in the following XSLT transformation:</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" cdata-section-elements="item"/&gt; &lt;xsl:param name="pToday" select="'04/23/2009'"/&gt; &lt;xsl:variable name="vthisDay" select= "substring($pToday,4,2)"/&gt; &lt;xsl:variable name="vthisMonth" select= "substring($pToday,1,2)"/&gt; &lt;xsl:variable name="vthisYear" select= "substring($pToday,7)"/&gt; &lt;xsl:template match="/"&gt; &lt;nonExpired&gt; &lt;xsl:copy-of select= "/*/item [$vthisYear &amp;lt; substring(@expires,7) or ($vthisYear = substring(@expires,7) and $vthisMonth &amp;lt; substring(@expires,1,2) ) or ($vthisYear = substring(@expires,7) and $vthisMonth = substring(@expires,1,2) and $vthisDay &amp;lt;= substring(@expires,4,2) ) ] "/&gt; &lt;/nonExpired&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When the above transformation is applied on the provided XML document</strong>:</p> <pre><code>&lt;content dataType="XML"&gt; &lt;item type="Promotion" name="Sample Promotion" expires="04/01/2009"&gt;&lt;![CDATA[ &lt;p&gt;Details here.&lt;/p&gt; ]]&gt;&lt;/item&gt; &lt;item type="Promotion" name="Sample Promotion 2" expires="05/01/2009"&gt;&lt;![CDATA[ &lt;p&gt;Details here.&lt;/p&gt; ]]&gt;&lt;/item&gt; &lt;/content&gt; </code></pre> <p><strong>the wanted result is produced</strong>:</p> <pre><code>&lt;nonExpired&gt; &lt;item type="Promotion" name="Sample Promotion 2" expires="05/01/2009"&gt;&lt;![CDATA[ &lt;p&gt;Details here.&lt;/p&gt; ]]&gt;&lt;/item&gt; &lt;/nonExpired&gt; </code></pre> <p><strong>Note</strong>: The solution by <em>oneporter</em> will also work, provided some small errors in it are corrected. Also, it needs rework because of its unrealistic expectation that the current date will be provided in a different format than the dates in the <code>@expires</code> attributes.</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