0

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.

Arty
  • 819
  • 3
  • 13
  • 24
  • 1
    `or` is a Boolean operator and including in an expression causes the expression to return a Boolean result. The real problem here is to define the **logic** by which you'll get the same result in both examples. Of that we know very little. Perhaps you should start by looking for 'Experts Choice - ' first and - if it's not found withing the given string - proceed with 'Group -'? – michael.hor257k Sep 26 '14 at 11:12
  • @michael.hor257k I could wrap around with the `if contains(stringToSearchWithin, stringToSearchFor)...` and apply the logic as you suggested. But how would I deal with a string that contains both `Group - ` and `Experts Choice - `? How would the `substring-after` deal with substring'ing a string after a string that contains both `Group -` and `Experts Choice - `? – Arty Sep 26 '14 at 11:28
  • "*But how would I deal with a string that contains both*" That's for you to say. Do we know in advance that 'Group -' always comes **before** 'Experts Choice -' ? – michael.hor257k Sep 26 '14 at 11:32
  • @michael.hor257k Yes 'Group - ' always comes before. Forgive my less explained question. I guess what I meant to ask is how does a substring-after with two matching strings substring after those two strings. As in case2, the format of 'Group - Experts Choice- ' Will always come in that fashion. But I'm trying to substring after those two strings. If that makes sense to you. – Arty Sep 26 '14 at 11:38

1 Answers1

1

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>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • thanks! my goal is the third input. And in regards to the input, Group always comes before Experts choice(it's automated that way and not done manually). But given the different states of input you've shown I appreciate it. and it's much more clearer to me now. Thanks ! – Arty Sep 26 '14 at 11:51