Below condition is not working in xsl

2019-09-02 09:50发布

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

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>

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>

标签: xslt xslt-1.0
1条回答
Summer. ? 凉城
2楼-- · 2019-09-02 10:08

You're testing for

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)">

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.

For the second case

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

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).

查看更多
登录 后发表回答