zyberjock
zyberjock

Reputation: 347

Get a substring from a specific pattern in an XML string

I have a string in XML that will look like below:

NNNANNNNN.NNNNNN

Where N is a numeric value (can be as long as 10 digits) and A is a single Alphabet.

What I need is to get the string after the Alphabet and before the decimal value and return the value with leading zeroes (10 characters max with the extracted value)

For Example I have

123A123456.789 

then the result will be

0000123456

Can anyone point me in the right direction using XSLT 1.0.

Upvotes: 0

Views: 1813

Answers (2)

Kevin Brown
Kevin Brown

Reputation: 8857

Pointing you to the right direction:

1) Use translate function to translate the alphabet character to some known thing:

translate($myvalue,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','**************************')

2) Wrap that in substring-before() using the "." to get everything before the "."

3) Wrap that in substring-after() using the character you chose above (" * " in this case) to get everything afer the " * "

Upvotes: 2

michael.hor257k
michael.hor257k

Reputation: 116959

I would do it this way:

<xsl:variable name="alpha" select="translate($yourstring, '0123456789.', '')" />
<xsl:variable name="int" select="substring-before(substring-after($yourstring, $alpha), '.')" />
<xsl:value-of select="format-number($int, '0000000000')" />

return the value with leading zeroes (10 characters max with the extracted value)

Not quite sure what you mean by that. If the extracted number can be longer than 10 characters, and you want to limit it to the 10 rightmost digits, then use:

<xsl:value-of select="substring(concat('0000000000', $int), 1 + string-length($int))" />

as the last step, instead of format-number().

Upvotes: 2

Related Questions