-2

Is it possible to write out xml based on an "if" "Like" statement or the equivalent of in xslt?

I have an element named "cust_code" If the element starts with a "HE" then I want to write it out, otherwise jump to the next.

Is it possible?

  • Duplicate of [How to implement if-else statement in XSLT?](https://stackoverflow.com/questions/13622338/how-to-implement-if-else-statement-in-xslt) – kjhughes Aug 14 '17 at 15:23
  • Your question is not clear. Please provide a [mcve]. XSLT does not have a "*jump to the next*" mechanism. Judging from your description, it would be much more elegant to apply templates only to the nodes you want to "*write out*" to begin with. – michael.hor257k Aug 14 '17 at 16:07

1 Answers1

1

If statements exist in XSLT.

<xsl:if test="...">
    ...
</xsl:if>

But this is a simple if, with no alternative.

If you want an equivalent to if ... else ... or switch ... case ..., you need to use the following:

<xsl:choose>
    <xsl:when test="...">
    </xsl:when>
    <xsl:otherwise>
    </xsl:otherwise>
</xsl:choose>

You can have as many when cases as necessary.

Links: w3school - if and w3school - choose.

As to having an element starting with a specific string, look at the function starts-with. You can find a good example in this SO answer (just omit the not from the main answer, as their tests was to find strings not starting with a particular string). You can also look at this answer for more information.

AntonH
  • 6,359
  • 2
  • 30
  • 40
  • thank you. I have done an if statement before in xslt and that works fine. In this instance though I want to see if the element value has the first 2 characters "HE" , the full element value will be something like "He2345567" I only want to write if it begins with "HE" – Nathan Fuller Aug 14 '17 at 15:32
  • @NathanFuller The other SO answers I linked show how to use the `starts-with` function. So for example, you could do ` ... `. – AntonH Aug 14 '17 at 15:35
  • Thank you Anton that is exactly what I needed. Works fantastically. – Nathan Fuller Aug 15 '17 at 10:49