0

I am pretty new to all of developing / using XSLT but for work we have to use this now. I am busy mapping messages from application A to B and i run into something I cannot find the answer to.

as input field i have <sys_external_id>201000077_G001_S20_H10</sys_external_id>

now have i created below:

<stopNumber>
    <xsl:value-of select="substring(sys_external_id, 16, 4)" /> 
</stopNumber>

<stopHandlingNumber>
    <xsl:value-of select="substring(sys_external_id, 20, 4)" /> 
</stopHandlingNumber>

this gives me at the moment the correct answer ( S20 and H10 ) now is the "problem" that the value of S20 and H10 can differ. for example the value can be S2100 or H110. also the G001 can also differ.

A substring-after does not work because i cannot determine where my end is ( as far as I know )

1
  • 1
    Which XSLT processor will you be using? Commented Feb 14, 2020 at 14:24

1 Answer 1

1

In XSLT 2.0 you could do:

<xsl:variable name="tokens" select="tokenize(sys_external_id, '_')" />

<stopNumber>
    <xsl:value-of select="$tokens[3]" /> 
</stopNumber>

<stopHandlingNumber>
    <xsl:value-of select="$tokens[4]" /> 
</stopHandlingNumber>

If you're limited to XSLT 1.0, then it could be:

<xsl:variable name="tail" select="substring-after(substring-after(sys_external_id, '_'), '_')" />

<stopNumber>
    <xsl:value-of select="substring-before($tail, '_')" /> 
</stopNumber>

<stopHandlingNumber>
    <xsl:value-of select="substring-after($tail, '_')" /> 
</stopHandlingNumber>

This is assuming that the input string is simply a sequence of tokens delimited by an underscore.


Note also that some XSLT 1.0 processors support the EXSLT str:tokenize() extension function.

Sign up to request clarification or add additional context in comments.

3 Comments

In XSLT 1.0 you can also use recursive template like Jeni Tennison implementation of EXSLT tokenize()
Hello, thank you so much for the quick respons. using the tokenize function did the trick. Just for my understanding of the working of tokenize function? Does this function check how much sections there are seperated with the "_" ? therefore my example has 4 different parts and after that I tell it to take part [3] and part [4]? again, thanks for help and quick answer.
@G-love Yes. For detailed explanation see: w3.org/TR/2010/REC-xpath-functions-20101214/#func-tokenize

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.