Otherwise-statement in XSLT-choose is not applied/working
Otherwise-statement in XSLT-choose is not applied/working
我有一个 XML 文档,其 类型 节点的值为“1”或“2”:
<MyDoc>
<foo>
<bar>
<type>2</type>
</bar>
</foo>
</MyDoc>
我想根据类型节点的值设置一个变量 typeBool,如果它是“1”,则应设置为 false,如果它是“2”到true。
使用 XSLT-choose-Element 应该可以测试当前值并根据结果设置 typeBool。
我正在尝试使用 XSLT 2.0 中的以下构造来执行此操作,但令我感到困惑的是 "otherwise"-路径未应用,我得到未创建 typeBool 的错误:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="type" select="/MyDoc/foo/bar/type/text()"/>
<xsl:choose>
<xsl:when test="$type = '2'">
<xsl:variable name="typeBool">true</xsl:variable>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="typeBool">false</xsl:variable>
</xsl:otherwise>
</xsl:choose>
<h1><b><xsl:value-of select="$typeBool"/></b></h1>
</xsl:transform>
这是我得到的转换错误:
error during xslt transformation:
Source location: line 0, col 0 Description:
No variable with name typeBool exists
与choose-element
必须在 variable-declaration 的 中定义 choose-clause:
<xsl:variable name="type">
<xsl:value-of select="/MyDoc/foo/bar/type/text()"/>
</xsl:variable>
<xsl:variable name="typeBool">
<xsl:choose>
<xsl:when test="$type = '2'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:variable>
这样条件语句看起来也更清晰。
使用 XSLT 2.0
@MichaelKay指出在XSLT 2.0中可以使用一个xpath-conditional,更简单:
<xsl:variable name="type">
<xsl:value-of select="/MyDoc/foo/bar/type/text()"/>
</xsl:variable>
<h1>
<b>
<xsl:value-of select="select="if($type=2) then 'true' else 'false'"/>
</b>
</h1>
正如您当前提出的问题,不需要 xsl:choose
并且它不必要地使您的 XSLT 代码复杂化。不过,您的实际问题可能更复杂。
您可以编写一个模板来匹配您感兴趣的元素(例如,type
元素),然后简单地 select 比较的值将评估为 true 或 false .
XSLT 样式表
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="type">
<h1>
<b>
<xsl:value-of select=". = '2'"/>
</b>
</h1>
</xsl:template>
<xsl:template match="text()"/>
</xsl:transform>
HTML输出
<h1><b>true</b></h1>
在线试用 here。
我有一个 XML 文档,其 类型 节点的值为“1”或“2”:
<MyDoc>
<foo>
<bar>
<type>2</type>
</bar>
</foo>
</MyDoc>
我想根据类型节点的值设置一个变量 typeBool,如果它是“1”,则应设置为 false,如果它是“2”到true。
使用 XSLT-choose-Element 应该可以测试当前值并根据结果设置 typeBool。
我正在尝试使用 XSLT 2.0 中的以下构造来执行此操作,但令我感到困惑的是 "otherwise"-路径未应用,我得到未创建 typeBool 的错误:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="type" select="/MyDoc/foo/bar/type/text()"/>
<xsl:choose>
<xsl:when test="$type = '2'">
<xsl:variable name="typeBool">true</xsl:variable>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="typeBool">false</xsl:variable>
</xsl:otherwise>
</xsl:choose>
<h1><b><xsl:value-of select="$typeBool"/></b></h1>
</xsl:transform>
这是我得到的转换错误:
error during xslt transformation:
Source location: line 0, col 0 Description:
No variable with name typeBool exists
与choose-element
必须在 variable-declaration 的 中定义 choose-clause:
<xsl:variable name="type">
<xsl:value-of select="/MyDoc/foo/bar/type/text()"/>
</xsl:variable>
<xsl:variable name="typeBool">
<xsl:choose>
<xsl:when test="$type = '2'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:variable>
这样条件语句看起来也更清晰。
使用 XSLT 2.0
@MichaelKay指出在XSLT 2.0中可以使用一个xpath-conditional,更简单:
<xsl:variable name="type">
<xsl:value-of select="/MyDoc/foo/bar/type/text()"/>
</xsl:variable>
<h1>
<b>
<xsl:value-of select="select="if($type=2) then 'true' else 'false'"/>
</b>
</h1>
正如您当前提出的问题,不需要 xsl:choose
并且它不必要地使您的 XSLT 代码复杂化。不过,您的实际问题可能更复杂。
您可以编写一个模板来匹配您感兴趣的元素(例如,type
元素),然后简单地 select 比较的值将评估为 true 或 false .
XSLT 样式表
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="type">
<h1>
<b>
<xsl:value-of select=". = '2'"/>
</b>
</h1>
</xsl:template>
<xsl:template match="text()"/>
</xsl:transform>
HTML输出
<h1><b>true</b></h1>
在线试用 here。