我有一个 XSLT,它需要一个 . 分隔字符串并将其拆分为 SQL 语句的两个字段:
<xsl:for-each select="tokenize(Path,'\.')">
<xsl:choose>
<xsl:when test="position() = 1 and position() = last()">SITE = '<xsl:value-of select="."/>' AND PATH = ''</xsl:when>
<xsl:when test="position() = 1 and position() != last()">SITE = '<xsl:value-of select="."/>' </xsl:when>
<xsl:when test="position() = 2 and position() = last()">AND PATH = '<xsl:value-of select="."/>' </xsl:when>
<xsl:when test="position() = 2">AND PATH = '<xsl:value-of select="."/></xsl:when>
<xsl:when test="position() > 2 and position() != last()">.<xsl:value-of select="."/></xsl:when>
<xsl:when test="position() > 2 and position() = last()">.<xsl:value-of select="."/>' </xsl:when>
<xsl:otherwise>zxyarglfaux</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
结果如下:
INPUT: North OUTPUT: SITE = 'North' AND PATH = ''
INPUT: North.A OUTPUT: SITE = 'North' AND PATH = 'A'
INPUT: North.A.B OUTPUT: SITE = 'North' AND PATH = 'A.B'
INPUT: North.A.B.C OUTPUT: SITE = 'North' AND PATH = 'A.B.C'
这有效,但非常冗长。谁能看到更有效的方法?
谢谢!