【问题标题】:XSLT how to create output based on the values of different nodesXSLT 如何根据不同节点的值创建输出
【发布时间】:2013-05-23 00:20:00
【问题描述】:

我有这个(伪代码)xml

<data>
<ref>one</ref>
<val>20</val>
<ref>two</ref>
<val>200</val>
<ref>three</ref>
<val>2000</val>
</data>

然后,假设我不知道“val”节点中有哪些值。因此,我想要 xhtml 输出

-> 如果有任何“val”低于 100

<div class="low">values 20,200,2000 in one,two,three</div>

-> 如果有任何“val”低于 1000 但不低于 100

<div class="medium">values 20,200,2000 in one,two,three</div>

-> 如果有任何“val”低于 10000 但不低于 1000

<div class="high">values 20,200,2000 in one,two,three</div>

*最后2种情况,初始值应该不同

有什么想法吗?谢谢:)

【问题讨论】:

  • 你的意思是:“*在最后两种情况下,初始值应该不同”?请解释一下——“初始值”是什么意思?这些应该是什么?
  • 那么,按照规定的要求,必须输出所有三个&lt;div&gt; 元素吗?对吗?
  • 只需说明在“中等”情况下,值应该是例如 120、200、2000,而不是原来的 20、200、2000。与“高”情况相同。否则,它将始终找到低于 100 的值。

标签: xml xslt xhtml


【解决方案1】:

要在“1.0”XSLT 版本中获得最大值,我们需要扩展函数。我已经为你创建了它:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:math="http://exslt.org/math" exclude-result-prefixes="math">

  <xsl:param name="checkValue">
    <xsl:variable name="value" select="math:max(//val)"/>
    <xsl:choose>
      <xsl:when test="$value &lt; 100">
        <xsl:text>low</xsl:text>
      </xsl:when>
      <xsl:when test="$value &lt; 1000 and $value &gt; 100">
        <xsl:text>medium</xsl:text>
      </xsl:when>
      <xsl:when test="$value &lt; 10000 and $value &gt; 1000">
        <xsl:text>high</xsl:text>
      </xsl:when>
    </xsl:choose>
  </xsl:param>

  <xsl:template match="data">
    <div class="{$checkValue}">
      <xsl:text>values </xsl:text>
      <xsl:for-each select="val">
        <xsl:value-of select="."/>
        <xsl:if test="position()!=last()">, </xsl:if>
      </xsl:for-each>
      <xsl:text> in </xsl:text>
      <xsl:for-each select="val">
        <xsl:value-of select="preceding-sibling::ref[1]"/>
        <xsl:if test="position()!=last()">, </xsl:if>
      </xsl:for-each>
    </div>
  </xsl:template>
</xsl:stylesheet>

输出:

<div class="high">values 20, 200, 2000 in one, two, three</div>

【讨论】:

  • 太棒了 :) 这就是我想要的
猜你喜欢
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多