【发布时间】: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 中没有值
-
那么如果你想测试具有空字符串值的元素,为什么要与非空字符串(
' ')进行比较? -
您的问题有两个有效答案。请接受其中之一。