Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This transformation shows how to use a global parameter (modelled here with an inline element) to specify (possibly multiple) updates</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my" &gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;my:updates&gt; &lt;update place="Berlin" dt="11-Dec-2011"/&gt; &lt;/my:updates&gt; &lt;xsl:template match="node()|@*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match= "location [place = document('')/*/my:updates/update/@place] /dt/text() "&gt; &lt;xsl:value-of select= "document('')/*/my:updates/update [@place = current()/../../place] /@dt "/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When applied on the provided XML document</strong> (corrected to make it well-formed):</p> <pre><code>&lt;Locations&gt; &lt;location&gt; &lt;place&gt;Newyork&lt;/place&gt; &lt;dt&gt;01-Dec-2011&lt;/dt&gt; &lt;/location&gt; &lt;location&gt; &lt;place&gt;Berlin&lt;/place&gt; &lt;dt&gt;02-Dec-2011&lt;/dt&gt; &lt;/location&gt; &lt;location&gt; &lt;place&gt;Tokyo&lt;/place&gt; &lt;dt&gt;04-Dec-2011&lt;/dt&gt; &lt;/location&gt; &lt;/Locations&gt; </code></pre> <p><strong>the wanted, correct result is produced</strong>:</p> <pre><code>&lt;Locations&gt; &lt;location&gt; &lt;place&gt;Newyork&lt;/place&gt; &lt;dt&gt;01-Dec-2011&lt;/dt&gt; &lt;/location&gt; &lt;location&gt; &lt;place&gt;Berlin&lt;/place&gt; &lt;dt&gt;11-Dec-2011&lt;/dt&gt; &lt;/location&gt; &lt;location&gt; &lt;place&gt;Tokyo&lt;/place&gt; &lt;dt&gt;04-Dec-2011&lt;/dt&gt; &lt;/location&gt; &lt;/Locations&gt; </code></pre> <p><strong>Explanation</strong>:</p> <ol> <li><p><strong>The identity rule copies every node "as-is</strong>".</p></li> <li><p><strong>There is just one overriding template</strong> -- matching the text-node child of any <code>dt</code> whose <code>place</code> sibling's string value has a corresponding <code>my:updates/update</code> element. In this template we output the value of the <code>dt</code> attribute of this corresponding <code>my:updates/update</code> element.</p></li> </ol> <p><strong>Do note</strong>: In a realworld transformation the inline <code>my:updates</code> element will be better replaced by an external, global parameter. Read your XSLT processor's documentation how to pass an external parameter to the transformation -- this is implementation-dependent.</p> <p><strong>UPDATE</strong>: As the OP has found it difficult to convert this solution to one using global, externally passed <code>xsl:param</code>, here is this converted solution:</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:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:param name="pUpdates"&gt; &lt;update place="Berlin" dt="11-Dec-2011"/&gt; &lt;/xsl:param&gt; &lt;xsl:variable name="vUpdates" select= "ext:node-set($pUpdates)/*"/&gt; &lt;xsl:template match="node()|@*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="dt/text()"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="../../place=$vUpdates/@place"&gt; &lt;xsl:value-of select= "$vUpdates[@place = current()/../../place]/@dt"/&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the same XML document (above), the same correct and wanted result is produced</strong>:</p> <pre><code>&lt;Locations&gt; &lt;location&gt; &lt;place&gt;Newyork&lt;/place&gt; &lt;dt&gt;01-Dec-2011&lt;/dt&gt; &lt;/location&gt; &lt;location&gt; &lt;place&gt;Berlin&lt;/place&gt; &lt;dt&gt;11-Dec-2011&lt;/dt&gt; &lt;/location&gt; &lt;location&gt; &lt;place&gt;Tokyo&lt;/place&gt; &lt;dt&gt;04-Dec-2011&lt;/dt&gt; &lt;/location&gt; &lt;/Locations&gt; </code></pre> <p><strong>Do note</strong>: In this solution the <code>xsl:param</code> still has its value hardcoded and this is the only reason we are using the <code>ext:node-set()</code> extension function. If the parameter is really passed from outside, then this convertion from RTF to a regular tree isn't necessary and the parameter should be referenced directly.</p> <p><strong>Also, in XSLT 1.0 we have to match more inexactly</strong> and to use comparisons (the <code>xsl:choose</code>) inside the body of the template. This is so because in XSLT 1.0 it isn't allowed to reference variables/parameters inside the match-pattern.</p> <p><strong>In XSLT 2.0 this limitation has been eliminated</strong>, so we can just have a much simpler transformation:</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:strip-space elements="*"/&gt; &lt;xsl:param name="pUpdates"&gt; &lt;update place="Berlin" dt="11-Dec-2011"/&gt; &lt;/xsl:param&gt; &lt;xsl:template match="node()|@*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match= "location[place=$pUpdates/*/@place]/dt/text()"&gt; &lt;xsl:value-of select= "$pUpdates/*[@place = current()/../../place]/@dt"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      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