开发者

How do I get XSLT to stop using scientific notation when outputting the results of numeric operations?

开发者 https://www.devze.com 2022-12-16 03:09 出处:网络
I have a bit of XSLT which is performing some magic on integer database identifiers.The relevant code snippit is

I have a bit of XSLT which is performing some magic on integer database identifiers. The relevant code snippit is

      <xsl:variable name="releaseId" select="@ID + $contentOffsetId"/>
      <xsl:attribute name="OLD_ID"> <xsl:value-of select="@ID" /> </xsl:attribute>
      <xsl:attribute name="RELEASE_ID"> <xsl:value-of select="$releaseId" />      

Unfortunately the output from this looks like this:

<ALBUM.RELEASE_LOCALE LOCALE_CODE="en_US" OLD_ID="6597512" RELEASE_ID="6.597513E6">开发者_StackOverflow

This result is useless to me since I won't be able to use the scientific notation entry as input to my DB import utility. I've tried a number of functions on the variable declaration, such as string(@ID + $contentOffsetId) but nothing seems to achieve the desired results.

The XSL is being executed using Saxon 8.7.


Use format-number (for detailed reference: here)


I suspect that @ID and $contentOffsetId are both integers masquerading as doubles, and the best strategy would be to use integer arithmetic to add them rather than floating-point arithmetic. The conversion to string will then use the rules for integers rather than the rules for doubles, which will avoid the use of scientific notation.

You haven't shown us where $contentOffsetId comes from, but if my guess is correct, declare it as an integer using as="xs:integer" on the variable declaration.

As for @ID, my guess is that your stylesheet isn't schema aware and therefore @ID is untyped atomic; you can convert it to integer using xs:integer(@ID). The default when untyped atomic values are used in arithmetic expressions is to convert to xs:double, but this is largely a legacy from XSLT 1.0 and is often inappropriate.

Incidentally in XSLT 2.0 you can replace

<xsl:attribute name="x"><xsl:value-of select="Y"/></xsl:attribute>

by

<xsl:attribute name="x" select="Y"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消