【发布时间】: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