【问题标题】:Simple XSLT nested IF statement简单的 XSLT 嵌套 IF 语句
【发布时间】:2017-06-20 15:16:33
【问题描述】:

这是我的代码的 sn-p。输入现在应该包括 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>

【问题讨论】:

  • 提供一个实际的minimal reproducible example,而不是伪代码。特别是,变量定义中的一些 select 表达式不是有效的 XPath 表达式;真实表达式的细节与问题相关,输入的 XML 也可能是相关的。通常,我们希望能够重现您的(最小示例)问题。
  • 可以接受编辑吗?
  • 编辑改进了问题,但您仍然没有提供 MCVE。特别是需要示例输入和预期输出;按照我提供的链接了解更多信息。现在呈现的 XSL 中有一些明显的奇怪之处,但我不确定它们是否是您真正的 XML 的特征。

标签: xslt xslt-1.0 ibm-datapower


【解决方案1】:

抱歉,问题很糟糕,但我使用了稍微修改过的代码。现在工作。

<?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>

【讨论】:

  • 没有看到输入并理解您要完成的工作,这对任何人都没有帮助。
  • 所以现在你嵌套了 &lt;xsl:when&gt; 测试而不是嵌套 &lt;xsl:if&gt; ?这是您所做的唯一更改,还是您在我们看不到的地方编辑了其他任何内容?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
相关资源
最近更新 更多