Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple XML Restructure with XSLT
    primarykey
    data
    text
    <p>I'm new to XSLT and I'm trying to achieve a reasonably simple restructure. I have an xml document representing a person (for illustrative purposes). I have numerous elements under person. From these I want to extract the <code>first_name</code> and <code>last_name</code> (and crucially others) into a new <code>full_name</code> element. I've seen the question <a href="https://stackoverflow.com/questions/6156307/how-to-use-xslt-to-convert-a-simple-piece-of-xml/6156761#6156761">here</a>. The "renameWrapped" approach works. However it's not generic enough as I can't guarantee the ordering of the elements in the input document.</p> <p>I have the following XML:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;myname:Person xmlns:myname="http://www.myname.co.uk/def"&gt; &lt;myname:attr1&gt;some value&lt;/myname:attr1&gt; &lt;myname:first_name&gt;john&lt;/myname:first_name&gt; &lt;myname:last_name&gt;smith&lt;/myname:last_name&gt; &lt;myname:attr2&gt;some value&lt;/myname:attr2&gt; &lt;!-- other person stuff --&gt; &lt;/myname:Person&gt; </code></pre> <p>This is transformed by the following xsl:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myname="http://www.myname.co.uk/def"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:template match="@*|node()" name="identity"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="myname:Person"&gt; &lt;myname:Person&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()[local-name() != 'first_name' and local-name() != 'Person' and local-name() != 'last_name']"/&gt; &lt;/xsl:copy&gt; &lt;myname:FullName&gt; &lt;xsl:apply-templates select="myname:first_name | myname:last_name"/&gt; &lt;/myname:FullName&gt; &lt;/myname:Person&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>However this is giving me the following output with <code>myname:Person</code> being duplicated:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;myname:Person xmlns:myname="http://www.myname.co.uk/def"&gt; &lt;myname:Person&gt; &lt;myname:attr1&gt;some value&lt;/myname:attr1&gt; &lt;myname:attr2&gt;some value&lt;/myname:attr2&gt; &lt;/myname:Person&gt; &lt;myname:FullName&gt; &lt;myname:first_name&gt;john&lt;/myname:first_name&gt; &lt;myname:last_name&gt;smith&lt;/myname:last_name&gt; &lt;/myname:FullName&gt; &lt;/myname:Person&gt; </code></pre> <p>What am I doing wrong?</p> <p>Thanks in advance for your help. </p>
    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.
 

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