Reputation: 5093
I need to generate an XML structure for a fixed number of languages from an input that may or may not contain information for each language. If the information is missing, I need to generate empty elements. The problem is, that I need to iterate over the languages at many places in the output structure.
The easiest way would be to use something resembling
<xsl:variable name="languages" select="en,de,fr">
<xsl:for-each select="$languages">
...
</xsl:for-each>
with the loop appearing wherever I need the language list.
Of course this does not work, because select="en,de,fr"
does not define a node-list. With an extension I could use the node-set
function, but I am stuck with XSLT-1.0.
Is there any way to define a constant node-set to iterate over?
(This is somehow related to another question where the accepted answer kills many ideas of creating a constant node set, in particular everything that needs sub-element of <xsl:variable/>
)
Upvotes: 5
Views: 4115
Reputation: 122364
If you want a constant node set rather than one whose contents are calculated by xsl:
instructions, then you can do a trick with document('')
which gives you access to the XML tree of the stylesheet itself:
<xsl:variable name="languagesLiteral">
<lang>en</lang>
<lang>de</lang>
<lang>fr</lang>
</xsl:variable>
<xsl:variable name="languages"
select="document('')//xsl:variable[@name='languagesLiteral']/*" />
This only works for static values, if you had for example <xsl:variable name="foo"><xsl:for-each ...>
then the node set you get from the document('')
trick would be the xsl:for-each
element, not the result of evaluating it.
Upvotes: 3