Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>As pointed in the other answers, XPath cannot modify an XML document and produce new nodes</em>.</p> <p>Any node can participate in a node-set only once, due to the definition of <code>"set"</code>.</p> <p><strong>However, XPath 2.0 gives us the new <a href="http://www.w3.org/TR/xpath-datamodel/#sequences" rel="nofollow noreferrer">sequence type</a>,</strong> which allows items to be repeated.</p> <p>In order to have an element appearing twice in a sequence, one would just use <a href="http://www.w3.org/TR/xpath-functions/#func-concatenate" rel="nofollow noreferrer"><strong>the sequence concatenation operator <code>","</code></strong></a> as in the below expression:</p> <p><strong><code>/*/ElementYouWant, /*/ElementYouWant</code></strong></p> <p>Put this into an XSLT2.0 stylesheet as simple as this:</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" xmlns:f="http://fxsl.sf.net/" exclude-result-prefixes="f xs" &gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:template match="/"&gt; &lt;t&gt; &lt;xsl:sequence select= "/*/ElementYouWant, /*/ElementYouWant"/&gt; &lt;/t&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>And apply this transformation on this XML document:</p> <pre><code>&lt;toplevel&gt; &lt;ElementYouWant&gt; &lt;SomeSubElement1&gt;specific data&lt;/SomeSubElement1&gt; &lt;SomeSubElement2&gt;specific data 2&lt;/SomeSubElement2&gt; &lt;/ElementYouWant&gt; &lt;/toplevel&gt; </code></pre> <p>to produce the wanted result:</p> <pre><code>&lt;t&gt; &lt;ElementYouWant&gt; &lt;SomeSubElement1&gt;specific data&lt;/SomeSubElement1&gt; &lt;SomeSubElement2&gt;specific data 2&lt;/SomeSubElement2&gt; &lt;/ElementYouWant&gt; &lt;ElementYouWant&gt; &lt;SomeSubElement1&gt;specific data&lt;/SomeSubElement1&gt; &lt;SomeSubElement2&gt;specific data 2&lt;/SomeSubElement2&gt; &lt;/ElementYouWant&gt; &lt;/t&gt; </code></pre> <p>Do note, that if one uses the <a href="http://www.w3.org/TR/xslt20/#element-sequence" rel="nofollow noreferrer"><strong><code>&lt;xsl:sequence&gt;</code></strong></a> instruction, no new copy of the <code>&lt;ElementYouWant&gt;</code> element is created -- therefore in XSLT 2.0 it is recommended to use <code>&lt;xsl:sequence&gt;</code> and to avoid using <a href="http://www.w3.org/TR/xslt#element-copy-of" rel="nofollow noreferrer"><strong><code>&lt;xsl:copy-of&gt;</code></strong></a> which creates (unnecessary) copies of nodes.</p>
 

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