xslt-1.0 迭代固定的值列表 [英] xslt-1.0 iterate over fixed list of values

查看:26
本文介绍了xslt-1.0 迭代固定的值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从可能包含也可能不包含每种语言信息的输入中为固定数量的语言生成一个 XML 结构.如果信息丢失,我需要生成空元素.问题是,我需要在输出结构的许多地方迭代语言.

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.

当然这不起作用,因为 select="en,de,fr" 没有定义节点列表.通过扩展,我可以使用 node-set 函数,但我坚持使用 XSLT-1.0.

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?

(这与另一个问题 其中接受的答案扼杀了创建常量节点集的许多想法,特别是需要 <xsl:variable/>) 子元素的所有内容

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

推荐答案

如果你想要一个 常量 节点集,而不是一个内容由 xsl: 指令计算的节点集,然后你可以用 document('') 做一个技巧,让你访问样式表本身的 XML 树:

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']/*" />

这只适用于静态值,如果你有例如 <xsl:variable name="foo"><xsl:for-each ...> 那么节点集你从 document('') 技巧中得到的是 xsl:for-each 元素,而不是评估它的结果.

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.

这篇关于xslt-1.0 迭代固定的值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆