Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>This transformation</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: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="product|*[starts-with(name(), 'item')]"&gt; &lt;action&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/action&gt; &lt;/xsl:template&gt; &lt;xsl:template match="product/@id"/&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on the provided XML document</strong>:</p> <pre><code>&lt;root&gt; &lt;item1&gt; &lt;name&gt;test&lt;/name&gt; &lt;price&gt;160&lt;/price&gt; &lt;stock&gt;4&lt;/stock&gt; &lt;country&gt;Belgium&lt;/country&gt; &lt;/item1&gt; &lt;item2&gt; &lt;name&gt;Alfa&lt;/name&gt; &lt;price&gt;140&lt;/price&gt; &lt;stock&gt;3&lt;/stock&gt; &lt;country&gt;Turkey&lt;/country&gt; &lt;/item2&gt; &lt;item3&gt; &lt;name&gt;Beta&lt;/name&gt; &lt;price&gt;110&lt;/price&gt; &lt;stock&gt;48&lt;/stock&gt; &lt;country&gt;Holland&lt;/country&gt; &lt;/item3&gt; &lt;product id="p1"&gt; &lt;name&gt;Delta&lt;/name&gt; &lt;price&gt;800&lt;/price&gt; &lt;stock&gt;4&lt;/stock&gt; &lt;country&gt;Denmark&lt;/country&gt; &lt;/product&gt; &lt;product id="p2"&gt; &lt;name&gt;Golf&lt;/name&gt; &lt;price&gt;1000&lt;/price&gt; &lt;stock&gt;5&lt;/stock&gt; &lt;country&gt;Germany&lt;/country&gt; &lt;/product&gt; &lt;product id="p3"&gt; &lt;name&gt;Alfa&lt;/name&gt; &lt;price&gt;1200&lt;/price&gt; &lt;stock&gt;19&lt;/stock&gt; &lt;country&gt;Germany&lt;/country&gt; &lt;/product&gt; &lt;product id="p4"&gt; &lt;name&gt;Foxtrot&lt;/name&gt; &lt;price&gt;1500&lt;/price&gt; &lt;stock&gt;5&lt;/stock&gt; &lt;country&gt;Australia&lt;/country&gt; &lt;/product&gt; &lt;product id="p5"&gt; &lt;name&gt;Tango&lt;/name&gt; &lt;price&gt;1225&lt;/price&gt; &lt;stock&gt;3&lt;/stock&gt; &lt;country&gt;Japan&lt;/country&gt; &lt;/product&gt; &lt;/root&gt; </code></pre> <p><strong>produces the wanted, correct result</strong>:</p> <pre><code>&lt;root&gt; &lt;action&gt; &lt;name&gt;test&lt;/name&gt; &lt;price&gt;160&lt;/price&gt; &lt;stock&gt;4&lt;/stock&gt; &lt;country&gt;Belgium&lt;/country&gt; &lt;/action&gt; &lt;action&gt; &lt;name&gt;Alfa&lt;/name&gt; &lt;price&gt;140&lt;/price&gt; &lt;stock&gt;3&lt;/stock&gt; &lt;country&gt;Turkey&lt;/country&gt; &lt;/action&gt; &lt;action&gt; &lt;name&gt;Beta&lt;/name&gt; &lt;price&gt;110&lt;/price&gt; &lt;stock&gt;48&lt;/stock&gt; &lt;country&gt;Holland&lt;/country&gt; &lt;/action&gt; &lt;action&gt; &lt;name&gt;Delta&lt;/name&gt; &lt;price&gt;800&lt;/price&gt; &lt;stock&gt;4&lt;/stock&gt; &lt;country&gt;Denmark&lt;/country&gt; &lt;/action&gt; &lt;action&gt; &lt;name&gt;Golf&lt;/name&gt; &lt;price&gt;1000&lt;/price&gt; &lt;stock&gt;5&lt;/stock&gt; &lt;country&gt;Germany&lt;/country&gt; &lt;/action&gt; &lt;action&gt; &lt;name&gt;Alfa&lt;/name&gt; &lt;price&gt;1200&lt;/price&gt; &lt;stock&gt;19&lt;/stock&gt; &lt;country&gt;Germany&lt;/country&gt; &lt;/action&gt; &lt;action&gt; &lt;name&gt;Foxtrot&lt;/name&gt; &lt;price&gt;1500&lt;/price&gt; &lt;stock&gt;5&lt;/stock&gt; &lt;country&gt;Australia&lt;/country&gt; &lt;/action&gt; &lt;action&gt; &lt;name&gt;Tango&lt;/name&gt; &lt;price&gt;1225&lt;/price&gt; &lt;stock&gt;3&lt;/stock&gt; &lt;country&gt;Japan&lt;/country&gt; &lt;/action&gt; &lt;/root&gt; </code></pre> <p><strong>Explanation</strong>:</p> <ol> <li><p>The identity rule copies every node "as-is".</p></li> <li><p>We have two templates overriding the identity rule. The first template matches any <code>product</code> or any element whose name starts with the string <code>item</code>. It effectively "renames" the matched element to <code>action</code> by creating and outputting an <code>action</code> element.</p></li> <li><p>The second overriding template matches any <code>id</code> attribute of any <code>product</code> element. This template has no body, which effectively "deletes" the matched attribute -- it is not copied/recreated in the output.</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