(已根据评论编辑并成功解决;TL;DR:忘记了 for-each 标签的“xsl”命名空间——真的很尴尬。有时你生活中唯一的目的就是为他人提供警告,所以我我正在用答案修补我的问题)
试图迭代静态值。我在搜索中能找到的最好的类似问题是xslt-1.0 迭代固定值列表。但我就是不能得到同样的行为。
在我的机器上使用 Firefox 82.0.3(与 nginx 服务器结合)或 Saxon 9.9.1.5 我得到了相同的结果。我已经隔离了行为不符合预期的 for-each 代码。当前文档似乎永远不会设置为 for-each 的选择所指示的新临时文档。
我在几个网站上检查了 for-each 的使用情况,我确定我错过了一些简单的东西。我只是没看到。其他人可以清楚地得到这个工作,出于某种原因我不能。我不认为我明确需要在 XSL 3 中使用节点集,尽管尝试并没有得到更好的结果。我尝试了链接问答中指出的 XSL 1.0 方法和其他一些相邻技术。
XML
<?xml-stylesheet type="text/xsl" href="for-each-test.xsl" ?>
<Character xmlns:math="http://exslt.org/math" xmlns:xi="http://www.w3.org/2001/XInclude">
<abc>xyz</abc>
</Character>
XSL
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:math="http://exslt.org/math"
extension-element-prefixes="exsl" >
<xsl:output method="html" version="4.01" indent="yes"/>
<xsl:output doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3C//DTD HTML 4.01//EN"/>
<xsl:variable name='var'>
<type>enhancement</type>
<type>inherent</type>
<type>enlargement</type>
</xsl:variable>
<xsl:template match='/'>
<html><head></head><body>
<!-- this was my original technique, which omitted 'xsl:'
<for-each select="document('')//xsl:variable[@name='var']/*">
name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
</for-each>
-->
<!-- these techniques work -->
<xsl:for-each select="document('')//xsl:variable[@name='var']/*">
name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
</xsl:for-each>
<xsl:for-each select='exsl:node-set($var)//type'>
name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
</xsl:for-each>
<!-- other incorrect techniques i tried;
they don't work even with tags corrected;
didn't really expect these to work;
was grasping at straws with these, really -->
<!--
<xsl:for-each select='exsl:node-set($var)'>
name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
</xsl:for-each>
<xsl:for-each select='$var//type'>
name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
</xsl:for-each>
<xsl:for-each select='$var/*'>
name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
</xsl:for-each>
<xsl:for-each select='$var'>
name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
</xsl:for-each>
-->
</body></html>
</xsl:template>
</xsl:stylesheet>
修复前For-Each 循环的示例输出
name[] = [ xyz ]
修复后For-Each 循环的期望输出和样本输出
name[type] = [enhancement]
name[type] = [inherent]
name[type] = [enlargement]