Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer is "yes"! Firstly, you would base your XSLT upon the <a href="http://en.wikipedia.org/wiki/Identity_transform" rel="nofollow">XSLT identity transform</a>. On its own, this copies all the nodes an attributes in your XML</p> <pre><code>&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; </code></pre> <p>By using this, you only then need to write templates to match the nodes you wish to transform. In your case, you say you want to match elements without children (although strictly speaking, the elements do have children. They have text nodes as children! But I guess you mean they don't have other elements as children). So, your template match would look this this</p> <pre><code>&lt;xsl:template match="*[not(*)]"&gt; </code></pre> <p>Where <code>*</code> is the symbol used to match elements.</p> <p>Then, within this template, you can copy the element, and turn the text into an attribute using <strong>xsl:attribute</strong></p> <pre><code> &lt;xsl:attribute name="V"&gt; &lt;xsl:value-of select="text()" /&gt; &lt;/xsl:attribute&gt; </code></pre> <p>Try this XSLT</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes"/&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="*[not(*)]"&gt; &lt;xsl:copy&gt; &lt;xsl:attribute name="V"&gt; &lt;xsl:value-of select="text()" /&gt; &lt;/xsl:attribute&gt; &lt;xsl:apply-templates select="@*" /&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>When applied to your XML, the following is output</p> <pre><code>&lt;DEV&gt; &lt;HDR&gt; &lt;ApID V="value1" /&gt; &lt;STAT V="value2" /&gt; &lt;/HDR&gt; &lt;DSC&gt; &lt;Cap V="value3" /&gt; &lt;/DSC&gt; &lt;/DEV&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