【问题标题】:Testing conditions in XSLT with xsl:when使用 xsl:when 在 XSLT 中测试条件
【发布时间】:2015-03-07 14:24:07
【问题描述】:

我有下面的 xsl 标记,我在其中提取 fpml:periodMultiplier 和 fpml:period 的值,如下所示... xml 中的标签是:-

<fpml:periodMultiplier>1</fpml:periodMultiplier>
<fpml:period>Y</fpml:period>

在xsl中解压如下图

<Payindextenor>
<xsl:value-of select="../fpml:calculationPeriodDates
                        /fpml:calculationPeriodFrequency
                        /fpml:periodMultiplier" />
<xsl:value-of select="../fpml:calculationPeriodDates
                        /fpml:calculationPeriodFrequency
                        /fpml:period" />
</Payindextenor>

所以 Payindextenor 的值为 1Y

现在我想在此标记中添加空检查,因为在即将到来的 xml 中可能会出现 fpml:periodMultiplier 和 fpml:period 也没有值。

所以我想出了下面的 xsl 实现,我在其中尝试过,如果任何一个值为 null,那么它应该打印 null 请告知它是否正确:-

<xsl:choose>
  <xsl:when test="../fpml:calculationPeriodDates
                    /fpml:calculationPeriodFrequency
                    /fpml:periodMultiplier
                  != ' ' 
                  and 
                  ../fpml:calculationPeriodDates
                    /fpml:calculationPeriodFrequency
                    /fpml:period 
                  != ' '">
    <xsl:value-of select="../fpml:calculationPeriodDates
                             /fpml:calculationPeriodFrequency
                            /fpml:periodMultiplier" />
   <xsl:value-of select="../fpml:calculationPeriodDates
                           /fpml:calculationPeriodFrequency
                           /fpml:period" />
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="'null'" />
  </xsl:otherwise>
</xsl:choose>

【问题讨论】:

  • “为空”到底是什么意思?此代码正在检查目标值是否等于包含单个空格的字符串,如果缺少元素或具有(完全)空值的元素,它将失败。
  • @IanRoberts 假设值以 xml 格式出现,例如
  • @IanRoberts 如上面的评论所示,如果这两个标签的输入 xml 中没有值
  • 那么如果你想测试具有空字符串值的元素,为什么要与非空字符串(' ')进行比较?
  • 您的问题有两个有效答案。请接受其中之一。

标签: xslt xslt-1.0


【解决方案1】:

这与your previous question 中的情况完全相同 - 您正在与包含单个空格的(非空)字符串' ' 进行比较,而您真正想要的是检查空字符串。您可以使用我为该问题建议的相同解决方案,并使用normalize-space 进行测试(它将空字符串和仅包含空格的字符串视为“假”,将其他任何内容视为“真”):

<xsl:choose>
  <xsl:when test="normalize-space(../fpml:calculationPeriodDates
                    /fpml:calculationPeriodFrequency
                    /fpml:periodMultiplier)
                  and 
                  normalize-space(../fpml:calculationPeriodDates
                    /fpml:calculationPeriodFrequency
                    /fpml:period)">
    <xsl:value-of select="../fpml:calculationPeriodDates
                             /fpml:calculationPeriodFrequency
                            /fpml:periodMultiplier" />
   <xsl:value-of select="../fpml:calculationPeriodDates
                           /fpml:calculationPeriodFrequency
                           /fpml:period" />
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="'null'" />
  </xsl:otherwise>
</xsl:choose>

这将处理fpml:periodMultiplierfpml:period 元素不存在以及它们存在但为空的情况。

【讨论】:

    【解决方案2】:

    正如 Ian Roberts 所说,将节点与单个空格进行比较与检查“null”非常不同,但假设您想在 periodMultiplierperiod 均为空白时显示“null”,您可以这样做这个:

    <xsl:variable name="freq" 
             select="../fpml:calculationPeriodDates/fpml:calculationPeriodFrequency" />
    <xsl:choose>
      <xsl:when test="$freq/fpml:periodMultiplier != '' or 
                      $freq/fpml:period != ''">
        <xsl:value-of select="$freq/fpml:periodMultiplier" />
        <xsl:value-of select="$freq/fpml:period" />
      </xsl:when>
      <xsl:otherwise>
          <xsl:text>null</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    

    【讨论】:

    • OP 的后续 cmets 建议他们想要检查 present-but-empty 而不是缺席。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    相关资源
    最近更新 更多