如何测试 xsl 中的命名空间元素名称
我正在做一些 xforms 开发,并且有一个与所选项目匹配的模板。如果是单个选择而不是多个选择,我想在给定列表的顶部添加一个空值:
<xsl:template match="xforms:select1|xforms:select">
<xsl:apply-templates select="node()" />
<xsl:if test=".[name()='xf:select1'] and not (@appearance eq 'full')">
<xforms:item>
<xforms:label />
<xforms:value />
</xforms:item>
</xsl:if>
...
问题是这适用于我的一个具有 xf:select1
的表单(因为match
是命名空间感知的),但另一种表单中的 xforms:select1
控件已损坏,因为 name()
测试仅适用于字符串。
有没有办法让这个 if 语句起作用,无论我为 http://www.w3.org/2002/xforms
命名空间设置什么前缀?
I'm doing some xforms development and have a template that matches select items. If it's a single select instead of multiple, I want to add an empty value to the top of the given list:
<xsl:template match="xforms:select1|xforms:select">
<xsl:apply-templates select="node()" />
<xsl:if test=".[name()='xf:select1'] and not (@appearance eq 'full')">
<xforms:item>
<xforms:label />
<xforms:value />
</xforms:item>
</xsl:if>
...
The issue is that this worked for one of my forms that had an xf:select1
(since the match
is namespace aware), but the xforms:select1
controls in another form were broken since the name()
test is only for strings.
Is there a way I can make this if statement work, regardless of what prefix I make for the http://www.w3.org/2002/xforms
namespace?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您肯定希望避免任何依赖命名空间前缀的代码。我认为你想使用自轴:
You definitely want to avoid any code which relies on namespace prefixes. I think you want to use the self axis:
我所做的是将 if 分成不同的模板...我喜欢它,因为它是 xsl 的用途;我讨厌它,因为它是某些匹配逻辑的另一个副本,如果它有错误或改进,则必须在多个地方进行更改......
What I have done is split out the if into different templates... I like it because it's what xsl is geared for; I hate it because it's yet another copy of some match logic that would have to be changed in multiple places if it had a bug or an improvement...