Frage

I'm attempting to create an XSLT mapping that properly converts a fairly large integer value coming through in a text field into the appropriate integer value. The problem is that since 1.0 only supports converting to type number, I get a value like 1.234567890E9 back for input of "1234567890"

I'm using Altova MapForce with XSLT1.0 as the coding platform. XSLT2.0 doesn't appear to be an option, as the XSLT has to be processed using a pre-existing routine that only supports XSLT1.0

By default Mapforce generates <xsl:value-of select="string(floor(number(string(.))))"/> and I've tried every combination of functions I can think of, but always get a float for large values.

Further testing shows the problem lies in Mapforce, which insists on using the number() function when mapping from text to int.

War es hilfreich?

Lösung 3

The problem lies within Mapforce, so I've decided to let mapforce generate it's code, then overwrite it for this one field that's causing all the trouble.

@Tobias @Michael Thanks to you both for your help. I've +1'ed both your answers and a few comments since your help led to the answer.

Andere Tipps

Let me try and move this forward by answering a question that you did not ask, but perhaps should have. Suppose you have the following input:

XML

<input>
    <value>1234567890000000.9</value>
    <value>9876543210000000</value>
</input>

and you want to make sure that the input values (which are all numbers, but some of them are not integers) are converted to integers at the output, you could apply the following transformation:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <output>    
        <xsl:for-each select="input/value">
            <value><xsl:value-of select="format-number(., '#')"/></value>
        </xsl:for-each>
    </output>
</xsl:template>  

</xsl:stylesheet>

to obtain the following output:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <value>1234567890000001</value>
   <value>9876543210000000</value> 
</output>

Note that the results here are rounded, not floored.

Are you sure that mapforce isn't using xslt-2.0?

If I do in XSLT-1.0 (with either saxon or Altova's processor):

<xsl:value-of select="number('1234567890')"/>

I get -> 1234567890

If I use XSLT-2.0 I get -> 1.23456789E9

So I think it is very strange that an XSLT 1 transformation supposedly returns you the floating point representation of the number.

Formatting the number with format-number(1.23456789E9,'#') will always give you 1234567890 in both XSLT-1.0 and 2.0. Edit: saxon will not convert 1.23456789E9 to number in xslt-1.0, altova's processor however will.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top