【问题标题】:XSLT transform with inline if statements带有内联 if 语句的 XSLT 转换
【发布时间】: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 &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>

但是我得到了所有段落的输出。

【问题讨论】:

  • 看看this
  • @H-Man2 你的回答似乎是我迄今为止遇到的最合适的。请添加常规答案,我会接受。

标签: xml xslt


【解决方案1】:

XSLT 没有内置的表达式动态求值。可能的解决方案在this question的回答中讨论。

【讨论】:

    【解决方案2】:

    你可以使用以下

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

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

    【讨论】:

    • 是的,这是真的,但是如何将 if_xVariable 转换为 $if_xVariable 以表明它是我要检查的变量?以及如何转换 &&至 &&?根据我的阅读,字符串操作并不是 xslt 的强项。
    • 您的 if_xVariable 应该是 xml 文件中的标签,以便您从 xml 文件中获取值,请参阅上面示例中的自行尝试,其中 xml 文件具有价格标签,示例使用
    • 我无法让它工作,根据this question 的说法,如果没有扩展,它甚至是不可能的。你有一个可行的解决方案还是我应该接受它不起作用?
    【解决方案3】:

    这是xsl文件中的代码

    在标签中

      <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如下

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

    【讨论】:

    • 我认为您没有正确理解这个问题。只要我可以自己设计它们,我就可以使用 if 语句。不幸的是,这次并非如此。我已经更新了原始问题,提供了到目前为止我尝试过的更多信息。
    猜你喜欢
    • 2013-06-16
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多