Search code examples
xmlxsltxslt-1.0xslt-grouping

XSL substring given two match variables


I'm trying to return a substring'ed value based on two matching strings 'Group -' and 'Experts Choice - ' Everything works accordingly to my requirements but the issue is that, it should return everything after 'Group -' and 'Experts Choice - '. However I'm getting the error that this line is incorrect. I've looked at the operators and whenever I used the 'OR' operator the output I got was 'true'. But I don't want that as my output. So an example would be:

CASE1:

<xsl:variable name="$own_name" select="Group - China sells apple products | $900 "/>

<xsl:value-of select="substring-before(substring-after($own_name, 'Group -'),'|')"/>

OUTPUT: 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 - '),'|')"/>

OUTPUT: true

My goal: Is to get the same ouput, China sells apple products instead of true. Where am I going wrong in case2.


Solution

  • I am still a bit confused regarding the possible inputs you might have. Let us take the following example input:

    <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>
    

    Applying the following stylesheet:

    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>
    

    results in:

    <?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>