Below condition is not working in xsl

老子叫甜甜 提交于 2019-12-25 03:44:29

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/28882502/below-condition-is-not-working-in-xsl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!