domueni
domueni

Reputation: 304

apostrophe in xsl:format-number

I parse an xml with my xslt and get the result as a xml.

i need to format numbers with apostrophe as delimiter for a tousand, million, etc...

eg: 1234567 = 1'234'567

now the problem is how do i get these apostrophes in there?

<xsl:value-of select="format-number(/path/to/number, '###'###'###'###')" />

this doesn't work because the apostrophe itself is already delimiting the start of the format.

is there a simple solution to that (maybe escaping the apostrophe like in c#?

Upvotes: 3

Views: 1771

Answers (2)

domueni
domueni

Reputation: 304

After some research we came up with this solution:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:decimal-format name='ch' grouping-separator="'" />
     <xsl:template match="/">
     <xsl:value-of select='format-number(/the/path/of/the/number, "###&apos;###&apos;###", "ch")'/>
     ...

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163625

The answer depends on whether you are using 1.0 or 2.0.

In 2.0, you can escape the string delimiter by doubling it (for example 'it''s dark'), and you can escape the attribute delimiter by using an XML entity such as &quot;. So you could write:

<xsl:value-of select="format-number(/path/to/number, '###''###''###''###')" />

In 1.0, you can escape the attribute delimiter by using an XML entity, but there is no way of escaping the string delimiter. So you could switch your delimiters and use

<xsl:value-of select='format-number(/path/to/number, "###&apos;###&apos;###&apos;###")' />

The other way - probably easier - is to put the string in a variable:

<xsl:variable name="picture">###'###'###'###</xsl:variable>
<xsl:value-of select="format-number(/path/to/number, $picture)" />

Upvotes: 2

Related Questions