Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's the full stylesheet you need (since the namespaces are important):</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:z="foo"&gt; &lt;xsl:template match="root"&gt; &lt;root&gt; &lt;xsl:apply-templates /&gt; &lt;/root&gt; &lt;/xsl:template&gt; &lt;xsl:template match="z:row"&gt; &lt;xsl:variable name="A" select="@A" /&gt; &lt;xsl:for-each select="@*[local-name() != 'A']"&gt; &lt;z:row A="{$A}"&gt; &lt;xsl:attribute name="{local-name()}"&gt; &lt;xsl:value-of select="." /&gt; &lt;/xsl:attribute&gt; &lt;/z:row&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>I much prefer using literal result elements (eg <code>&lt;z:row&gt;</code>) rather than <code>&lt;xsl:element&gt;</code> and attribute value templates (those <code>{}</code>s in attribute values) rather than <code>&lt;xsl:attribute&gt;</code> where possible as it makes the code shorter and makes it easier to see the structure of the result document that you're generating. Others prefer <code>&lt;xsl:element&gt;</code> and <code>&lt;xsl:attribute&gt;</code> because then everything is an XSLT instruction.</p> <p>If you're using XSLT 2.0, there are a couple of syntactic niceties that help, namely the <code>except</code> operator in XPath and the ability to use a <code>select</code> attribute directly on <code>&lt;xsl:attribute&gt;</code>:</p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" xmlns:z="foo"&gt; &lt;xsl:template match="root"&gt; &lt;root&gt; &lt;xsl:apply-templates /&gt; &lt;/root&gt; &lt;/xsl:template&gt; &lt;xsl:template match="z:row"&gt; &lt;xsl:variable name="A" as="xs:string" select="@A" /&gt; &lt;xsl:for-each select="@* except @A"&gt; &lt;z:row A="{$A}"&gt; &lt;xsl:attribute name="{local-name()}" select="." /&gt; &lt;/z:row&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </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