xslt-1.0 iterate over fixed list of values

假如想象 提交于 2021-01-27 14:22:17

问题


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/>)


回答1:


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.



来源:https://stackoverflow.com/questions/22839803/xslt-1-0-iterate-over-fixed-list-of-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!