使用内联 if 语句进行 XSLT 转换
我有一些 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>
,但我得到了所有段落的输出。
I have some XML data that I want to convert to HTML with XSLT and I have mostly got it right. The problem with my XML input is that it contains inline if statements / boolean expressions that I have no idea how to handle.
<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>
I have been thinking of adding xslt-vairables with each variable name (e.g. if_xVariable) to true/false and in some way check against them.
How would you go about to solve this problem?
Update This is what I've tried
<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>
Since I have neither of if_xVariable
or if_yVariable
specified, the output should probably be something like
<p>
Always show this text
</p>
but instead I get the output from all of the paragraphs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XSLT 没有内置的表达式动态求值。 此问题的答案中讨论了可能的解决方案。
XSLT has no built-in dynamic evaluation of expressions. Possible solutions are discussed in the answer to this question.
您可以使用以下
完整示例,位于 http://www.w3schools.com/xsl/xsl_if.asp
you can use following
full example is at http://www.w3schools.com/xsl/xsl_if.asp
xsl 文件中的代码
这是标签 person.xml 中
如下
this is the code in xsl file
in tag
person.xml is as follow