有人能告诉我我做错了什么吗?当<xsl:value-of select="string-length(add:Destinations/add:DestinationName)" />
=萨克拉门托,CA时我会期望变量区域位于西边,但我会向东。
<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 :(得分: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, ', ')" />