0

I think I have this working but I need advice. I'd like to know if this is a good setup for the requirement that I have. I've got a requirement to apply different transformation rules to an element based on what is contained in that element. I've tried to search for 'xsl' and 'choose', 'contains', 'substring'. I can't seem to find a solution applicable to this situation.

Here are the various scenarios for this element:

  1. If it begins with U, I need everything before the '/'

    • Original Value: UJXXXXX/001

    • Transformed : UJXXXXX

  2. If it begins with ECG_001 I need everything after ECG_001

    • Original Value: ECG_0012345678

    • Transformed : 12345678

  3. If it does not meet the above criteria and contains a '/' take everyting after the '/'

    • Original Value: F5M/12345678

    • Transformed : 12345678

  4. If it does not meet 1,2, or 3 just give me the value

    • Original Value : 12345678

    • Transformed : 12345678

Here is what I have so far:

<xsl:variable name="CustomerPO">
        <xsl:choose>
            <xsl:when test="contains(substring(rma/header/CustomerPO,1,1), 'U')">
                <xsl:value-of select="substring-before(rma/header/CustomerPO,'/')"/>
            </xsl:when>
            <xsl:when test="contains(rma/header/CustomerPO, 'ECG_001')">
                <xsl:value-of select="substring-after(rma/header/CustomerPO,'ECG_001')"/>
            </xsl:when>
            <xsl:when test="contains(rma/header/CustomerPO, '/')">
                <xsl:value-of select="substring-after(rma/header/CustomerPO, '/')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="rma/header/CustomerPO"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

Any feedback on potential loopholes or a more efficient way to accomplish this is appreciated.

Thanks.

dmittakarin8
  • 143
  • 1
  • 8
  • I think this is good enough. I can't find nothing wrong on it, and I think there is no better solution. You could do it in a different way only if you used javascript or an external library, but I think it wouldn't improve the performance. – JotaBe Mar 22 '12 at 00:36

2 Answers2

1

Your XSLT looks fine. You might consider using the starts-with function rather than substring, I find it easier to read, but I am not sure it is any faster.

Chris Summers
  • 10,153
  • 1
  • 21
  • 46
  • Yes that is what I was looking for. I was trying to find a begins-with function. Not sure how I missed starts-with. Thank you. – dmittakarin8 Mar 22 '12 at 08:33
0

My own style would be to use template rules.

<xsl:template match="CustomerPO[starts-with(., 'U')]">
  <xsl:value-of select="substring-before(., '/')"/>
</xsl:template>

<xsl:template match="CustomerPO[starts-with(., 'ECG_001')]">
  <xsl:value-of select="substring-after(., 'ECG_001')"/>
</xsl:template>

etc.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164