使用内联 if 语句进行 XSLT 转换

发布于 2024-12-16 14:14:47 字数 1278 浏览 2 评论 0原文

我有一些 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 &amp;&amp; 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_xVariableif_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋檐 2024-12-23 14:14:47

XSLT 没有内置的表达式动态求值。 此问题的答案中讨论了可能的解决方案。

XSLT has no built-in dynamic evaluation of expressions. Possible solutions are discussed in the answer to this question.

月野兔 2024-12-23 14:14:47

您可以使用以下

<xsl:if test="expression">
    ...some output if the expression is true...
 </xsl:if>

完整示例,位于 http://www.w3schools.com/xsl/xsl_if.asp

you can use following

<xsl:if test="expression">
    ...some output if the expression is true...
 </xsl:if>

full example is at http://www.w3schools.com/xsl/xsl_if.asp

断舍离 2024-12-23 14:14:47

xsl 文件中的代码

这是标签 person.xml 中

  <xsl:for-each select="persons/person">
  <xsl:if test="GenderElementType='combo'">
       <select name="gender">
      <option>
      <xsl:value-of select="Gender"/>
      </option>
      <xsl:if test="Gender='MALE'">
      <option>
       FEMALE
      </option>
      </xsl:if>
      <xsl:if test="Gender='FEMALE'">
      <option>
       MALE
      </option>
      </xsl:if>
      </select>
      </xsl:if>
 </xsl:for-each>

如下

 <persons>
 <person>
        <Gender>MALE</Gender>
    <GenderElementType>radio</GenderElementType>


</person>

this is the code in xsl file

in tag

  <xsl:for-each select="persons/person">
  <xsl:if test="GenderElementType='combo'">
       <select name="gender">
      <option>
      <xsl:value-of select="Gender"/>
      </option>
      <xsl:if test="Gender='MALE'">
      <option>
       FEMALE
      </option>
      </xsl:if>
      <xsl:if test="Gender='FEMALE'">
      <option>
       MALE
      </option>
      </xsl:if>
      </select>
      </xsl:if>
 </xsl:for-each>

person.xml is as follow

 <persons>
 <person>
        <Gender>MALE</Gender>
    <GenderElementType>radio</GenderElementType>


</person>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文