XSLT add number to substring - xslt

Could you please help how to add or sub integer from substring? Here below my substring, and want to add this value to 5
<xsl:value-of select="substring($values/smt1/smt2[position()=1]/smt3 , 12, 2)"/>
Lets say above value is 3, how to make it 8? And can i use also mod opetator?
Thanks

If $string contains "abc03de", then:
<xsl:value-of select="substring($string, 4, 2)"/>
returns "03".
In XSLT 1.0 you can do:
<xsl:value-of select="substring($string, 4, 2) + 5"/>
to get a result of 8.
In XSLT 2.0+ you need to expressly convert the resulting substring to a number before it can be used in a numerical operation, e.g.:
<xsl:value-of select="number(substring($string, 4, 2)) + 5"/>

Related

XSLT How to substring after 10 characters

I use xslt to substring before:
<xsl:element name="mapcat">
<xsl:value-of select="substring-before(categories,'/')"/>
</xsl:element>
Original:
17845/288323/8844
Result:
17845
Working correct
Now I need substring after 10 characters.
I try:
<xsl:element name="mapcat">
<xsl:value-of select="substring(categories,'1,10')"/>
</xsl:element>
But something went wrong and no working. Can anyone help me correct this code?
I need substring after 10 characters.
I am guessing you want to do:
<xsl:value-of select="substring(original-string, 11)"/>
Your explanation of the problem wasn't very clear (difficulties with your English perhaps?) but from the example supplied as a comment you want the first 11 characters of the string, which is substring(xx, 1, 11). Supplying 0 (or anything less than 1) as the second argument has the same effect: the actual semantics of substring(x, a, b) are to include all characters in x whose position p (counting from 1) satisfies p>=a and p<(a+b) -- with additional rules for rounding if the values are not integers.

XSLT code to select string between first underscore until third underscore(will be cut after 10 digits)

current Output
<wd:GradeCode>CH_Service_Fly_test worker</wd:GradeCode>
<wd:GradeCode>CN_Dips_12 Engineer depart</wd:GradeCode>
Output needed
<wd:GradeCode>Service_Fl</wd:GradeCode>
<wd:GradeCode>Dips_12 En</wd:GradeCode>
Unfortunately little input. Which could probably work, would be the following function (depending on what you have in mind):
<!-- build a string after the first underline -->
<xsl:variable name="firstString">
<xsl:value-of select="substring-after(., '_')"/>
</xsl:variable>
<!-- limit the firstString to 10 digits -->
<xsl:value-of select="substring($firstString/text(),1,10)"/>
The pending to use for each item.

How would I substring my value 6 decimal places

Is there a way to substring my value 6 places after decimal?
So if I have
<xsl:value-of select="100.1234567890" />
Is there a way to make it
"100.123456"
Your value is a number, not a string. The easy way to trim it to 6 decimal places is to floor it, not substring it:
<xsl:value-of select="floor(100.1234567890 * 1000000) div 1000000"/>
returns:
100.123456
To achieve the same thing through string manipulation, you could do:
<xsl:variable name="n" select="'100.1234567890'" />
<xsl:value-of select="substring($n, 1, string-length(substring-before($n, '.')) + 7)"/>

Determine position of row in xslt is every 10th position

here is my current xslt:
<xsl:when test="position()=10">
This works, but i have 100 records so it only produces my neccessary html on the 10th record, and nothing more.
what i need now is a dynamic way of saying when is every ten positions. i imagine you could use modulus but im having difficulty getting that to work.
Ive tried:
<xsl:when test="position() mod 10">
Not sure what the solution is.
I think you want <xsl:when test="position() mod 10 = 0">.

Extract decimal part into variable in XSL template

I need to store the 2 places after decimal (currency amount, so need only upto 2 places) in an XSL variable before I do a ceil function.
<xsl:element name="BaseFare"><xsl:value-of select="ceiling(BaseAmount/Amount * (1 - ($promoDisc div 100)))"/></xsl:element>
For eg. if the result of the Amount is 499 and promoDisc = 8%, then the discounted Amount would be 459.08 - I need to store "08" (with the zero) in a variable for use later, while I return the ceiling amount (460) in the output XML. Thought I could just do a string function and read 2 chars after the decimal into a variable rather than do any math?
There are different ways to do this extraction:
I. Using the string representation of the number:
concat('.',substring(substring-after($x, '.'), 1, 2))
II. Using standard math functions:
$x - floor($x)
This evaluates to the decimal part of any positive number $x.
Use one of the functions: format-number(), round(), round-half-to-even() (the last function available only XPath 2.0 / XSLT 2.0) to round it to two decimal places.
In XSLT 1.0 one way to get from a positive number exactly the digits in the two decimal places after the decimal point (truncate without rounding), is:
format-number(
floor(100* $x) div 100
-
floor(floor(100* $x) div 100),
'.00'
)
Here is a complete example of the described methods:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:value-of select=
"concat('.',substring(substring-after(0.12543, '.'), 1, 2))"/>
=========
<xsl:value-of select=
"format-number(0.12543, '.00')"/>
=========
<xsl:value-of select=
"format-number(
floor(100* 999.12543) div 100
-
floor(floor(100* 999.12543) div 100),
'.00'
)
"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on any XML document (not used), the result is:
.12
=========
.13
=========
.12