2017-10-19 70 views
-1

有人能告诉我我做错了什么吗?当<xsl:value-of select="string-length(add:Destinations/add:DestinationName)" /> =加利福尼亚州萨克拉门托我期望可变区域是西部,但我得到东部。XSLT选择不工作

<xsl:variable name="charCount"> 
    <xsl:value-of select="string-length(add:Destinations/add:DestinationName)" /> 
</xsl:variable> 
<xsl:variable name="amendedCharCount"> 
    <xsl:value-of select="$charCount - 2"/> 
</xsl:variable> 
<xsl:variable name="state"> 
    <xsl:value-of select="substring(add:Destinations/add:DestinationName,$amendedCharCount)"/> 
</xsl:variable> 
<xsl:variable name="region"> 
    <xsl:choose> 
    <xsl:when test="$state='CA'">west</xsl:when> 
    <xsl:otherwise>east</xsl:otherwise> 
    </xsl:choose> 
</xsl:variable>  
<xsl:element name="OperationsRegion"> 
    <xsl:value-of select="$region"/> 
</xsl:element>  
+0

你真的应该在问题中发布你的输入XML。谢谢。 –

回答

0

在XSLT/XPath中,字符串中的字符的索引是基于1而不是0为主。所以,与其这样做(这实际上将获得最后三个字符)

<xsl:variable name="amendedCharCount"><xsl:value-of select="$charCount - 2"/></xsl:variable> 

做到这一点....

<xsl:variable name="amendedCharCount"><xsl:value-of select="$charCount - 1"/></xsl:variable> 

或者更好的是,这样做....

<xsl:variable name="amendedCharCount" select="$charCount - 1"/> 

也许,如果你所有的目的地都有相同的格式,你也可以将这三个表达式简化为一个?如果目的地名称中只有一个逗号,这将起作用。

<xsl:variable name="state" select="substring-after(add:Destinations/add:DestinationName, ', ')" /> 
+0

完美地工作。比我所做的更简单的解决方案。非常感谢! – Bob

+0

而且 - 谢谢你为什么这不起作用的解释。我现在看到我的错误。 – Bob