0
<xsl:key name="item" match="SHOPITEM" use="substring-before(PRODUCT, ' ')" />

Hello, i'm using this part of code to verify elements by first word but this is not enough. i have to add some conditions like:

if contains(PRODUCT, "SIZE.") then substring-before(PRODUCT, 'SIZE.') else get all value of PRODUCT

is it possible to put "if" inside "use" or any other way to do that?

Szczepan
  • 47
  • 5

1 Answers1

2

In XSLT 1.0, you can use a technique called Oliver Becker's method, which means writing the use in this form

concat(
  substring(Str1,1 div Cond),
  substring(Str2,1 div not(Cond))
) 

So, in your case, you could write your xsl:key like this:

<xsl:key name="item2" match="SHOPITEM" use="concat(
        substring(substring-before(PRODUCT, ' SIZE.'), 1 div (contains(PRODUCT, ' SIZE.'))),
        substring(PRODUCT, 1 div (not(contains(PRODUCT, ' SIZE.'))))
    )" />

Whether you consider this readable is another matter.

However, in this particular case, for the logic you are trying to implement, you could also write the key like this:

 <xsl:key name="item" match="SHOPITEM" use="substring-before(concat(PRODUCT, ' SIZE.'), ' SIZE.')" />

So, if the PRODUCT contains SIZE. it will find the text before the first occurrence. If not, it will the concatenated SIZE. which effectively get the whole string

See examples in action at http://xsltfiddle.liberty-development.net/jyRYYij

Tim C
  • 70,053
  • 14
  • 74
  • 93