Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, you would start off by having a generic template that turned attributes into elements, like so:</p> <pre><code>&lt;xsl:template match="@*"&gt; &lt;xsl:element name="{local-name()}"&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; </code></pre> <p>But looking at your requirements, certain attributes need to be renamed as elements, and so you would then write specific templates for these. For example, the <strong>@id</strong> element looks like it is to be renamed as <strong>InstrumentId</strong></p> <pre><code>&lt;xsl:template match="@id"&gt; &lt;instrumentID&gt; &lt;xsl:value-of select="."/&gt; &lt;/instrumentID&gt; &lt;/xsl:template&gt; </code></pre> <p>The only little bit of extra work needed is with the template that matches the <strong>part</strong> element is that you need to output an <strong>instrumentType</strong> element whose value is equal to the parent attribute. Apart from that you would just need to select all the attribute elements to transform</p> <pre><code>&lt;xsl:template match="part"&gt; &lt;instrumentType&gt; &lt;xsl:value-of select="../@name"/&gt; &lt;/instrumentType&gt; &lt;xsl:apply-templates select="@*"/&gt; &lt;/xsl:template&gt; </code></pre> <p>For the second level <strong>type</strong> element, you can let XSLT's built-in templates handle it. </p> <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="list"&gt; &lt;instrument&gt; &lt;xsl:apply-templates/&gt; &lt;/instrument&gt; &lt;/xsl:template&gt; &lt;xsl:template match="part"&gt; &lt;instrumentType&gt; &lt;xsl:value-of select="../@name"/&gt; &lt;/instrumentType&gt; &lt;xsl:apply-templates select="@*"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@id"&gt; &lt;instrumentID&gt; &lt;xsl:value-of select="."/&gt; &lt;/instrumentID&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@name"&gt; &lt;instrumentBrand&gt; &lt;xsl:value-of select="."/&gt; &lt;/instrumentBrand&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@*"&gt; &lt;xsl:element name="{local-name()}"&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>When applied to your XML (assuming a closing <strong>list</strong> tag), the following is output</p> <pre><code>&lt;instrument&gt; &lt;instrumentType&gt;drum&lt;/instrumentType&gt; &lt;instrumentBrand&gt;pearl&lt;/instrumentBrand&gt; &lt;instrumentID&gt;0&lt;/instrumentID&gt; &lt;version&gt;0&lt;/version&gt; &lt;displayOrder&gt;0&lt;/displayOrder&gt; &lt;customizationLevel&gt;basic&lt;/customizationLevel&gt; &lt;instrumentType&gt;drum&lt;/instrumentType&gt; &lt;instrumentBrand&gt;dw&lt;/instrumentBrand&gt; &lt;instrumentID&gt;1&lt;/instrumentID&gt; &lt;version&gt;0&lt;/version&gt; &lt;displayOrder&gt;0&lt;/displayOrder&gt; &lt;customizationLevel&gt;basic&lt;/customizationLevel&gt; &lt;instrumentType&gt;guitar&lt;/instrumentType&gt; &lt;instrumentBrand&gt;fender&lt;/instrumentBrand&gt; &lt;instrumentID&gt;0&lt;/instrumentID&gt; &lt;version&gt;0&lt;/version&gt; &lt;displayOrder&gt;0&lt;/displayOrder&gt; &lt;customizationLevel&gt;basic&lt;/customizationLevel&gt; &lt;/instrument&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. 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