以下条件在 xsl 中不起作用 [英] Below condition is not working in xsl

查看:29
本文介绍了以下条件在 xsl 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的 xsl 中有以下条件..

I have the below condition that I have wriiten in xsl below..

因为我在 fpml:tradeId 的 xml 中获得了一个值,现在它可以在 xml 中,也可以不在那里,例如如下所示..

as i have getting an value in xml of fpml:tradeId now it can be there in xml or it can not be there for example as shown below..

<fpml:tradeId tradeIdScheme=""></fpml:tradeId>
or it can contain value also
<fpml:tradeId tradeIdScheme="">10381159400</fpml:tradeId>

作为建议,我已将实施更改为但仍然无法正常工作..

as advise i have change the implementation to but still not working ..

                <xsl:if test = "fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId = ' '">
                 <xsl:value-of select="'null'" />
                </xsl:if>                   
            </ContractID>

所以为了解决这个问题,我在 xsl 下提出了这个问题,现在问题来了,对于 xml 中没有 tradeId 的情况,它没有放置空字符串,请告知我下面的实现xsl 是正确的

so to deal with this problem i have come up with this below xsl , now the problem comes is that for the cases where tradeId is not there in xml it is not putting null string please advise is my below implementation of xsl is correct

                <xsl:if test = "fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId != ' '">
                <xsl:choose>
                    <xsl:when test="contains(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId,'-')">
                        <xsl:value-of select="substring-before(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId,'-')" />
                </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId" />
                    </xsl:otherwise>
                </xsl:choose>
                </xsl:if>

                <xsl:if test = "fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId = ' '">
                 <xsl:value-of select="'null'" />
                </xsl:if>                   
            </ContractID>

建议新的实现已完成,但仍然无法正常工作..

as advise new implemetaion done but it is still not working..

<xsl:if test="normalize-space(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId)">
                <xsl:choose>
                    <xsl:when test="contains(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId,'-')">
                        <xsl:value-of select="substring-before(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId,'-')" />
                </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId" />
                    </xsl:otherwise>
                </xsl:choose>
                </xsl:if>

                <xsl:if test = "fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId = ' '">
                 <xsl:value-of select="'null'" />
                </xsl:if>                   
            </ContractID>

推荐答案

您正在测试

fpml:tradeId = ' '

它在您的示例中没有 - 您的 fpml:tradeId 的值是空字符串,而不是包含空格的字符串.对于第一种情况,您可以使用

which it doesn't in your example - the value of your fpml:tradeId is the empty string, not a string containing a space. Instead of comparing the value against ' ' for the first case you could use a test of

<xsl:if test="normalize-space(
    fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId)">

在 XPath 中,如果字符串为空则将字符串视为 false,否则将字符串强制为布尔值,否则为 true,因此如果 tradeId 值包含至少一个非值,则测试将通过-whitespace 字符,如果完全为空或只包含空格则失败.

In XPath a string coerces to a boolean by treating it as false if the string is empty and true if it isn't, so the test will pass if the tradeId value contains at least one non-whitespace character, and fail if it is completely empty or contains only whitespace.

对于第二种情况

<xsl:if test="not(normalize-space(
    fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId))">

会给你反向测试 - 如果字符串全是空格或空,则为真,否则为假(或仅使用带有 otherwise 子句的 choose而不是两个相反的 if 测试).

would give you the reverse test - true if the string is all whitespace or empty, false if it isn't (or just use a choose with an otherwise clause instead of two opposing if tests).

这篇关于以下条件在 xsl 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆