Reputation: 79
Is there any possibility when applying the following xsl code
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="fils1"><xsl:text> </xsl:text>
<xsl:choose>
<xsl:when test=".='C_RE'">
<fils><xsl:value-of select="." /></fils>
</xsl:when>
<xsl:otherwise>
<fils><xsl:value-of select="floor(.)*(. >= 0) + ceiling(.) * not(. >= 0)"/> </fils><xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
on this xml file:
<Racine>
<el1>
<fils1 >2.13</fils1>
<fils1>012</fils1>
<fils1>-31.45</fils1>
<fils1>C_RE</fils1>
</el1>
</Racine>
to get this output?
<?xml version="1.0" encoding="utf-8"?>
<fils>2</fils>
<fils>012</fils>
<fils>-31</fils>
<fils>C_RE</fils>
For the moment I can get on the second line of my result this: <fils>12</fils>
and I want to get the digit 0 in my result like this: <fils>012</fils>
.
Thanks
Upvotes: 1
Views: 97
Reputation: 243459
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="fils1[number() = number()]">
<fils>
<xsl:value-of select="substring-before(concat(.,'.'), '.')"/>
</fils>
</xsl:template>
<xsl:template match="fils1">
<fils><xsl:copy-of select="node()"/></fils>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<Racine>
<el1>
<fils1>2.13</fils1>
<fils1>012</fils1>
<fils1>-31.45</fils1>
<fils1>C_RE</fils1>
</el1>
</Racine>
the wanted, correct result is produced:
<fils>2</fils>
<fils>012</fils>
<fils>-31</fils>
<fils>C_RE</fils>
Explanation:
Using the fact that an expression to test if the string value of the current node is a correct representation of a number is: number() = number()
.
Proper use of sentinel programming.
Upvotes: 2
Reputation: 163262
You could count the number of leading zeros ( = string-length(x) - string-length(number(x)), perhaps) and add them back at the end, though of course this kind of string manipulation in XSLT 1.0 is very fiddly.
Upvotes: 0