【发布时间】:2011-11-17 06:43:35
【问题描述】:
我有一些 XML 数据,我想用 XSLT 将其转换为 HTML,而且我大部分都做对了。我的 XML 输入的问题在于它包含 内联 if 语句 / 布尔表达式,我不知道如何处理。
<section>
<title>My Title</title>
<paragraph>
<phrase class="inline-if">if_xVariable || if_yVariable</phrase>
Show this text if if_xVariable or if_yVariable is true.
</paragraph>
<paragraph>
<phrase class="inline-if">if_xVariable && if_yVariable</phrase>
Show this text if if_xVariable and if_yVariable is true
</paragraph>
<paragraph>
Always show this text
</paragraph>
</section>
我一直在考虑将带有每个变量名称(例如 if_xVariable)的 xslt-vairables 添加到 true/false 并以某种方式检查它们。
你打算如何解决这个问题?
更新这是我尝试过的
<xsl:template match="section/paragraph">
<xsl:variable name="inlineif" select="phrase[@class='inline-if']"/>
<xsl:if test="$inlineif">
<p>
<xsl:value-of select="."/>
</p>
</xsl:if>
</xsl:template>
因为我没有指定if_xVariable 或if_yVariable,所以输出应该是这样的
<p>
Always show this text
</p>
但是我得到了所有段落的输出。
【问题讨论】:
-
看看this。
-
@H-Man2 你的回答似乎是我迄今为止遇到的最合适的。请添加常规答案,我会接受。