简体   繁体   中英

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?

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM