Xml XSLT选择不工作
有人能告诉我我做错了什么吗?当Xml XSLT选择不工作,xml,xslt,Xml,Xslt,有人能告诉我我做错了什么吗?当=加利福尼亚州萨克拉门托时,我希望可变区域位于西部,但我位于东部 <xsl:variable name="charCount"> <xsl:value-of select="string-length(add:Destinations/add:DestinationName)" /> </xsl:variable> <xsl:variable name="amendedCharCount"> <xsl
=加利福尼亚州萨克拉门托时,我希望可变区域位于西部,但我位于东部
<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>
西
东
在XSLT/XPath中,字符串中字符的索引是基于1的,而不是基于0的。因此,不要这样做(实际上会得到最后三个字符)
这样做
<xsl:variable name="amendedCharCount"><xsl:value-of select="$charCount - 1"/></xsl:variable>
<xsl:variable name="amendedCharCount" select="$charCount - 1"/>
或者更好的是,这样做
<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, ', ')" />
您也应该在问题中发布您的输入XML。谢谢工作完美。比我做的更简单的解决方案。非常感谢!还有,谢谢你解释为什么这不起作用。我现在明白我的错误了。