简单的XSLT嵌套IF语句

时间:2017-06-20 15:16:34

标签: xslt xslt-1.0 ibm-datapower

以下是我的代码片段。输入现在应该包括2个可选字段。所以我想创建4个if语句,因为如果字段存在与否,则URL必须不同。选择/何时起作用,以及第一个IF语句有效。但是如果一个或两个字段都不在输入中,则最后3个会爆炸。 URL变量(DVURL)根本没有填充。我确信这很容易回答......我希望。

    <?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:func="http://exslt.org/functions" xmlns:date="http://exslt.org/dates-and-times" xmlns:string="http://symphony-cms.com/functions" xmlns:dpfunc="http://www.datapower.com/extensions/functions">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:variable name="datavalueagg" select="test1"/>
        <xsl:variable name="valueagg" select="test2"/>
        <xsl:variable name="variable1" select="Y"/>
        <xsl:choose>
            <xsl:when test="($variable1 = 'Y' or $variable1 = 'N')">
                <xsl:if test="(($datavalueagg != '') and ($valueagg != ''))">
                    <xsl:variable name="DVURL" select="concat('http://server.com/$valueagg/$datavalueagg')"/>
                </xsl:if>
                <xsl:if test="(($datavalueagg != '') and ($valueagg = ''))">
                    <xsl:variable name="DVURL" select="concat('http://server.com/$datavalueagg')"/>
                </xsl:if>
                <xsl:if test="$datavalueagg = '' and $valueagg != ''">
                    <xsl:variable name="DVURL" select="concat('http://server.com/$valueagg')"/>
                </xsl:if>
                <xsl:if test="$datavalueagg = '' and $valueagg = ''">
                    <xsl:variable name="DVURL" select="concat('http://server.com/none')"/>
                </xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="DVURL" select="concat('http://server.com/all')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:-2)

抱歉问题不好,但我使用了稍微修改过的代码。现在就工作。

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:func="http://exslt.org/functions" xmlns:date="http://exslt.org/dates-and-times" xmlns:string="http://symphony-cms.com/functions" xmlns:dpfunc="http://www.datapower.com/extensions/functions">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:variable name="datavalueagg" select="test1"/>
        <xsl:variable name="valueagg" select="test2"/>
        <xsl:variable name="variable1" select="Y"/>
        <xsl:choose>
            <xsl:when test="($variable1 = 'Y' or $variable1 = 'N')">
                <xsl:choose>
                    <xsl:when test="(string-length($datavalueagg) != 0 and string-length($valueagg) != 0)">
                        <xsl:variable name="DVURL" select="concat('http://server.com/$valueagg/$datavalueagg')"/>
                    </xsl:when>
                    <xsl:when test="(string-length($datavalueagg) != 0 and string-length($valueagg) != 0)">
                        <xsl:variable name="DVURL" select="concat('http://server.com/$datavalueagg')"/>
                    </xsl:when>
                    <xsl:when test="(string-length($datavalueagg) != 0 and string-length($valueagg) != 0)">
                        <xsl:variable name="DVURL" select="concat('http://server.com/$valueagg')"/>
                    </xsl:when>
                    <xsl:when test="(string-length($datavalueagg) != 0 and string-length($valueagg) != 0)">
                        <xsl:variable name="DVURL" select="concat('http://server.com/none')"/>
                    </xsl:when>
                    <xsl:otherwise/>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="DVURL" select="concat('http://server.com/all')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>