Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to have two XSLT templates apply to same element
    primarykey
    data
    text
    <p>I have XML like this:</p> <pre><code>&lt;data&gt; &lt;admin id="abc"&gt; &lt;type&gt;one&lt;/type&gt; &lt;!-- &lt;detail/&gt; might appear here but the content is unrelated to &lt;detail/&gt; below --&gt; &lt;/admin&gt; &lt;detail id="def"&gt; &lt;name&gt;Bob&lt;/name&gt; &lt;/detail&gt; &lt;/data&gt; </code></pre> <p>I then have XSLT like this:</p> <pre><code>&lt;xsl:template match="/data/admin"&gt; Admin content &lt;/xsl:template&gt; &lt;xsl:template match="/data/detail"&gt; Some content to appear before ALL types. This id is &lt;xsl:value-of select="@id"/&gt; &lt;xsl:choose&gt; &lt;xsl:when test="/data/admin/type='one'&gt; Content for type One only. Your name is &lt;xsl:value-of select="name"/&gt; &lt;/xsl:when&gt; &lt;xsl:when test="/data/admin/type='two'&gt; Content for type Two only. Your name is still &lt;xsl:value-of select="name"/&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; Content if the type is not set or set to an unknown value &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; Some content to appear after ALL types. This id is &lt;xsl:value-of select="@id"/&gt; &lt;/xsl:template&gt; </code></pre> <p>Because the <code>&lt;xsl:choose/&gt;</code> is quite large, I want to split it into different files - one per condition - and <code>&lt;xsl:include/&gt;</code> them into the main document.</p> <p>The problem I have is that <code>&lt;xsl:include/&gt;</code> requires each included file to contain templates, I can't include raw data. So I am left with these choices:</p> <ul> <li>In each included file, use <code>&lt;xsl:template match="/data/detail"/&gt;</code> - this then overrides the parent template above, so I have to copy and paste the data "for ALL types" into each file. This is not good because of the data duplication.</li> <li>In each included file, use something like <code>&lt;xsl:template match="/data/admin/type='one'"/&gt;</code> and then put in a call to <code>&lt;xsl:apply-templates select="/data/admin/type"/&gt;</code>. This won't work because then in the "Admin content" template I will have an inadvertent template instance for the child <code>&lt;type/&gt;</code> node that I don't want.</li> <li>Define each block of content as a named template, then inside my <code>&lt;xsl:choose/&gt;</code> call each one by name. This seems kind of untidy when XSLT should be able to select the right template without me naming it explicitly.</li> </ul> <p>Is there a neater way this can be achieved?</p> <p>The output should look like this:</p> <pre><code>Admin content Some content to appear before ALL types. This id is def Content for type One only. Your name is Bob Some content to appear after ALL types. This id is def </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.
 

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