给出两个匹配变量的XSL子串

时间:2014-09-26 10:58:43

标签: xml xslt xslt-1.0 xslt-grouping

我正在尝试根据两个匹配字符串'Group -''Experts Choice - '返回一个子字符串值。一切都符合我的要求,但问题是,它应该返回{{1 }和'Group -'。但是我收到这条线不正确的错误。我查看了运算符,每当我使用'Experts Choice - '运算符时,我得到的输出是'OR'。但我不希望这是我的输出。所以一个例子是:

CASE1:

'true'

输出: <xsl:variable name="$own_name" select="Group - China sells apple products | $900 "/> <xsl:value-of select="substring-before(substring-after($own_name, 'Group -'),'|')"/>

CASE2:

China sells apple products

输出: <xsl:variable name="$own_name" select="Group - Experts Choice - China sells apple products | $900 "/> <xsl:value-of select="substring-before(substring-after($own_name, 'Group -'),'|') or substring-before(substring-after($own_name, 'Experts Choice - '),'|')"/>

我的目标:是获得相同的输出,true而不是真。在case2中我哪里出错了。

1 个答案:

答案 0 :(得分:1)

对于您可能提供的输入,我仍然感到有些困惑。我们采取以下示例输入

<input>
    <string>Group - China sells apple products | $900</string>
    <string>Experts Choice - China sells apple products | $900 </string>
    <string>Group - Experts Choice - China sells apple products | $900 </string>
    <string>Group - gobbledy gook Experts Choice - China sells apple products | $900 </string>
    <string>Experts Choice - Group - China sells apple products | $900 </string>
</input>

应用以下样式表:

XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <result>
        <xsl:for-each select="/input/string">
            <payload>
                <xsl:choose>
                    <xsl:when test="contains(., 'Experts Choice -')">
                        <xsl:value-of select="substring-before(substring-after(., 'Experts Choice -'),'|')"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="substring-before(substring-after(., 'Group -'),'|')"/>
                    </xsl:otherwise>
                </xsl:choose>
            </payload>
        </xsl:for-each>
    </result>
</xsl:template>

</xsl:stylesheet>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<result>
   <payload> China sells apple products </payload>
   <payload> China sells apple products </payload>
   <payload> China sells apple products </payload>
   <payload> China sells apple products </payload>
   <payload> Group - China sells apple products </payload>
</result>