xsl:call-template中的条件,以限制在XSLT中考虑Null或WhiteSpace for Split Scenario

时间:2017-06-24 15:23:51

标签: xml xslt biztalk

我需要在调用语句xsl:call-template之前执行一个条件。我试图检查一个条件,在分割字符串后,如果value1或value2有一个Null或一个空值,整个记录及其中的元素不应该被打印。

以下是我需要的一个简短示例:

Value1 : Name1;;Name3

Value2 : Sam;Tsn;Doug

预期产出:

<Profile>
<Type>Name1</Type>
<Value>Sam</Value>
</Profile>
<Profile>
<Type>Name3</Type>
<Value>Doug</Value>
</Profile>

所以这里第二种类型和值没有打印,因为它在Value1中有一个空值 反之亦然,如果Value2有空值而Value1有一个值,那么它也应该限制它打印它。

我尝试过: 发生的问题总是包含一个值,所以我们无法检查那里

<xsl:template name="WritePropertyNodeTemplateName">
<xsl:param name="Name" />
<xsl:param name="Type" />
<xsl:if test="$Name != '' and $Type != ''"> // Had put condition here but didnt work
    <xsl:call-template name="StringSplitName">
        <xsl:with-param name="val1" select="$Name" />
        <xsl:with-param name="val2" select="$Type" />
    </xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="StringSplitName">
<xsl:param name="val1" />
<xsl:param name="val2" />
<xsl:choose>
    <xsl:when test="contains($val1, ';')">
        <xsl:if test="$val2 != '' and $val1 != ''">
            <xsl:value-of select="'1st'" />
            <ns1:OtherType xmlns:ns1="http://schemas.datacontract.org/2004/07/GEP.Cumulus.WebInterfaces.BusinessEntities">
                <ns1:Name xmlns:ns1="http://schemas.datacontract.org/2004/07/GEP.Cumulus.WebInterfaces.BusinessEntities">
                    <xsl:value-of select="substring-before($val1, ';')" />
                </ns1:Name>
                <ns1:Type xmlns:ns1="http://schemas.datacontract.org/2004/07/GEP.Cumulus.WebInterfaces.BusinessEntities">
                    <xsl:value-of select="substring-before($val2, ';')" />
                </ns1:Type>
            </ns1:OtherType>
            <xsl:call-template name="StringSplitName">
                 //Tried to put condition here also but didnt work
                <xsl:with-param name="val1" select="substring-after($val1, ';')" />
                <xsl:with-param name="val2" select="substring-after($val2, ';')" />
            </xsl:call-template>
        </xsl:if>
    </xsl:when>
</xsl:choose>
</xsl:template>

我在BizTalk Maps中使用此XSLT代码。

1 个答案:

答案 0 :(得分:0)

你为什么不这样做:

<xsl:template name="tokenize">
    <xsl:param name="types"/>
    <xsl:param name="values"/>
    <xsl:param name="delimiter" select="';'"/>
        <xsl:variable name="type" select="substring-before(concat($types, $delimiter), $delimiter)" />
        <xsl:variable name="value" select="substring-before(concat($values, $delimiter), $delimiter)" />
        <xsl:if test="normalize-space($type) and normalize-space($value)">
            <Profile>
                <Type>
                    <xsl:value-of select="$type"/>
                </Type>
                <Value>
                    <xsl:value-of select="$value"/>
                </Value>
            </Profile>
        </xsl:if>
        <xsl:if test="contains($types, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="types" select="substring-after($types, $delimiter)"/>
                <xsl:with-param name="values" select="substring-after($values, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

电话示例:

    <xsl:call-template name="tokenize">
        <xsl:with-param name="types">Name1;  ;Name3</xsl:with-param>
        <xsl:with-param name="values">Sam;Tsn;Doug;</xsl:with-param>
    </xsl:call-template>

结果:

  <Profile>
    <Type>Name1</Type>
    <Value>Sam</Value>
  </Profile>
  <Profile>
    <Type>Name3</Type>
    <Value>Doug</Value>
  </Profile>

演示:http://xsltransform.net/93dEHFW

相关问题