xml XSLT if - 属性等于字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21858620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XSLT if - attribute equals string
提问by lajarre
I'm using a xsl:ifto perform a small condition in a larger xsl:templateblock, and I'd like to test equality of an attribute of the current xsl:templatematched node.
我正在使用 axsl:if在较大的xsl:template块中执行一个小条件,并且我想测试当前xsl:template匹配节点的属性的相等性。
The following is not working:
以下不起作用:
<xsl:template match="sometag[@type='sometype']">
==Sometag==
<xsl:if test="@something!='hidden'">something</xsl:if>
<!--a lot of other stuff that I don't want to duplicate by multiplying the xsl:templates-->
<xsl:template>
This test seems to be always evaluating to false, maybe I don't have the good syntax?
这个测试似乎总是评估为假,也许我没有好的语法?
This XML:
这个 XML:
<sometag type="sometype" something="visible"/>
<sometag type="sometype" something="hidden"/>
<sometag type="sometype"/>
Should give
应该给
==Sometag==
something...
==Sometag==
==Sometag==
something...
回答by Mathias Müller
the 2nd tag should not get the "something" part printed.
第二个标签不应打印“某物”部分。
I am not entirely sure what you are trying to achieve, but I'll give it a try.
我不完全确定您要实现的目标,但我会尝试一下。
One of your sometagelements does not have a somethingattribute at all. Not having this attribute is entirely different from @something!='hidden'. So, the string "something" is not output if the somethingattribute is not present.
您的sometag元素之一根本没有something属性。没有这个属性与@something!='hidden'. 因此,如果something属性不存在,则不会输出字符串“something” 。
Because of this you need to test whether there isa somethingattribute before your xsl:ifcondition is evaluated.
正因为如此,你需要测试是否是一个something属性您之前xsl:if评估条件。
Input
输入
<?xml version="1.0" encoding="utf-8"?>
<root>
<sometag type="sometype" something="visible"/>
<sometag type="sometype" something="hidden"/>
<sometag type="sometype"/>
</root>
Stylesheet
样式表
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/root">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="sometag[@type='sometype']">
<xsl:text>==Sometag==</xsl:text>
<xsl:choose>
<xsl:when test="@something">
<xsl:if test="@something!='hidden'">
<xsl:text>something</xsl:text>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:text>something</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
EDIT@Tim C has suggested an even shorter version:
编辑@Tim C 提出了一个更短的版本:
<xsl:template match="sometag[@type='sometype']">
<xsl:text>==Sometag==</xsl:text>
<xsl:if test="@something!='hidden' or not(@something)">
<xsl:text>something</xsl:text>
</xsl:if>
</xsl:template>
Output
输出
==Sometag==something==Sometag====Sometag==something
回答by ikellenberger
Based on the solution above (of @Mathias Müller and as suggested by @Tim C) you can even speed up things a little bit using Short-Circuit Evaluationfor the or:
基于上面的解决方案(@Mathias Müller 和@Tim C 的建议),您甚至可以使用短路评估来加快速度or:
<xsl:template match="sometag[@type='sometype']">
<xsl:text>==Sometag==</xsl:text>
<xsl:if test="not(@something) or @something!='hidden'">
<xsl:text>something</xsl:text>
</xsl:if>
</xsl:template>
If there is no attribute somethingpresent, then the if-clause already evaluates to trueand the expression @something!='hidden'does not have to be evaluated.
如果不存在属性something,则 if 子句已计算为true并且@something!='hidden'不必计算表达式。

