我正试图返回基于两个匹配字符串'Group -'
和'Experts Choice - '
的子字符串的值。一切都符合我的要求,但问题是,它应该返回'Group -'
和'Experts Choice - '
之后的所有内容。然而,我得到了一个错误,这行是不正确的。我看过运算符,每当我使用'OR'
运算符时,我得到的输出都是'true'
。但我不想把它作为我的输出。例如:
CASE1:
<xsl:variable name="$own_name" select="Group - China sells apple products | $900 "/>
<xsl:value-of select="substring-before(substring-after($own_name, 'Group -'),'|')"/>
输出:China sells apple products
CASE2:
<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
我的目标:是获得相同的输出,China sells apple products
而不是true。第二种情况下我哪里错了。
对于您可能拥有的输入,我仍然有点困惑。让我们以以下示例输入:
<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>