Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your answer couldn't be much simpler, so no need to worry on that front. When writing code, but especially XSLT, the clarity of the code tends to be worth a lot more than its ultimate efficiency.</p> <p>As for the hardcoded element name and copying attributes, here's a start:</p> <pre><code>&lt;xsl:template match="Report_Entry"&gt; &lt;xsl:variable name="parent-name" select="name()"/&gt; &lt;xsl:variable name="parent-attributes" select="@*"/&gt; &lt;xsl:for-each select="Event"&gt; &lt;xsl:element name="{$parent-name}"&gt; &lt;xsl:copy-of select="$parent-attributes"/&gt; &lt;xsl:copy-of select="../*[not(self::Event)]"/&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:element&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; </code></pre> <p><code>variable</code>s are used to stash some of the context as it exists outside the <code>for-each</code>. <code>element</code> makes an element that's a look-alike for your original, no matter what it's called, and the first <code>copy-of</code> makes it more convincing by copying in the original's attributes as well. Now, if your data suddenly takes on attributes, you'll be ready.</p> <p>The non-hardcodedness of the name doesn't mean much in this case, but it would if, say, you were to factor out that part to a separate template and call it from multiple places:</p> <pre><code>&lt;xsl:template name="collapse-the-thing"&gt; &lt;xsl:param name="context"/&gt; &lt;xsl:param name="sub-element-name" select="'Event'"/&gt; &lt;xsl:variable name="parent-name" select="name($context)"/&gt; &lt;xsl:variable name="parent-attributes" select="$context/@*"/&gt; &lt;xsl:for-each select="$context/*[name()=$sub-element-name]"&gt; &lt;xsl:element name="{$parent-name}"&gt; &lt;xsl:copy-of select="$parent-attributes"/&gt; &lt;xsl:copy-of select="../*[name()!=$sub-element-name]"/&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:element&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Report_Entry"&gt; &lt;xsl:call-template name="collapse-the-thing"&gt; &lt;xsl:with-param name="context" select="."/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Some_Other_Entry"&gt; &lt;xsl:call-template name="collapse-the-thing"&gt; &lt;xsl:with-param name="context" select="."/&gt; &lt;xsl:param name="sub-element-name" select="'Happening'"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:template&gt; </code></pre> <p>Hope that was enlightening. Enjoy!</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.
    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