xsl:call-template 中的条件,以限制在 XSLT 中为拆分场景考虑 Null 或 WhiteSpace
Condition in xsl:call-template in order to restrict considering Null or WhiteSpace for Split Scenario in XSLT
我需要在调用语句 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 代码。
你为什么不简单地做:
<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>
我需要在调用语句 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 代码。
你为什么不简单地做:
<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>