user3715286
user3715286

Reputation: 1

Formatting phone number to in XSLT2.0

I am using XSLT 2.0. The output data has to be a csv file. For the phone number column, it is a simple select from XML in the following way:

<xsl:value-of select="ns:phone[ns:is_primary = 'true']/ns:phone_number"/>

When I view the output in a csv file,a few numbers come as "9.12234E+11". I tried changing this into a string like so:

<xsl:value-of select="ns:phone[ns:is_primary = 'true']/format-number(ns:phone_number,'############')"/>

This is, however, not helping matters. How can I make this work?

Upvotes: 0

Views: 958

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

My guess, in the absence of specific information, is that you are doing a schema-aware transformation, and the schema defines the phone_number field as numeric.

Now, numeric is a bad choice for a phone number field. It may be fine for validating a phone number, but the semantics are wrong: for example defining it as numeric makes leading zeroes (or leading "+" signs) insignificant. And it leads to incorrect formatting, as you have seen.

A phone number should be defined in the schema as a string with a pattern something like \+?[0-9]+ (you can vary this to be more permissive or more restrictive if you know your data).

If you can't change the schema, you may (depending on the processor) be able to recover the original lexical representation using ns:phone-number/text().

Upvotes: 0

Related Questions