XML & Decimal Numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Bowman

    XML & Decimal Numbers

    Hi,

    A couple Q's , so I've come to the experts<g>.

    1) I need a definitive answer to the following debate. I've got a couple
    developers who disagree on the following question. We've got an XML file of
    numerical data results where it could contain values such as:
    <RESULT>0.12345 </RESULT> when created in English, but it could contain:
    <RESULT>0,12345 </RESULT> when created in another language... such as German
    or French. One developer claims this should be perfrectly allowed, the other
    claims you can never use commas in XML files for content and that he read it
    somewhere, but can't find the reference. Who's correct?

    2) Related to the above debate, is the question of using an XSL file to
    generate the HTML from the above. How does one properly use the
    format-number function and the decimal-format xsl command to allow only 1
    XSL file to be used across multiple languages so that the selection and
    display of the above RESULT value is correct for the current culture. Be
    aware that an <xsl: choose> command operates on the RESULT value as well.
    Does this require separate XSL files for each language?

    TIA,

    --
    John C. Bowman
    Software Engineer
    Thermo Electron Scientific Instruments Div.
    <Remove this before reply> john.bowman@the rmo.com


  • Martin Honnen

    #2
    Re: XML &amp; Decimal Numbers



    John Bowman < wrote:

    [color=blue]
    > 1) I need a definitive answer to the following debate. I've got a couple
    > developers who disagree on the following question. We've got an XML file of
    > numerical data results where it could contain values such as:
    > <RESULT>0.12345 </RESULT> when created in English, but it could contain:
    > <RESULT>0,12345 </RESULT> when created in another language... such as German
    > or French. One developer claims this should be perfrectly allowed, the other
    > claims you can never use commas in XML files for content and that he read it
    > somewhere, but can't find the reference. Who's correct?[/color]

    Of course you can put a comma in there but if you want to store
    numerical values then use the dot, you can then when you output the
    stored data format the numbers as needed in the locale desired.


    --

    Martin Honnen

    Comment

    • John Bowman

      #3
      Re: XML &amp; Decimal Numbers

      Martin,

      Thanks for the info.

      That said, then if I have a simple example XML file as below containing the
      data values:

      <?xml-stylesheet type="text/xsl" href="format.xs l"?>
      <DOCUMENT>
      <RESULT>29.8220 </RESULT>
      <RESULT>6.123 4</RESULT>
      <RESULT>-0.0678</RESULT>
      </DOCUMENT>

      How do I get the value of each of the RESULT's into the format-number
      function so that it transforms them using the proper decimal-format? That
      is, it should display it using the "," specified in the <xsl:decimal-format
      name="fra" decimal-separator=","/> element? Basically, I can't get
      format-number to accept anything for the value retrieved. It seems that
      format-number only allows specific literal constants such as
      format-number(1.2345, "#.#"), is that correct?

      Below is a starting XSL file produced using Stylus Studio that displays the
      values with "."'s. How can I modifiy it so the output shows: 29,8220 6,1234
      0,0678 using the specified comma?

      <?xml version='1.0' encoding='utf-8'?>
      <xsl:styleshe et version="1.0"
      xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html"/>
      <xsl:decimal-format name="fra" decimal-separator=","/>
      <xsl:template match="/">
      <html><head/>
      <body>
      <xsl:for-each select="DOCUMEN T/RESULT">
      <xsl:value-of select="."/>
      </xsl:for-each>
      </body>
      </html>
      </xsl:template>
      </xsl:stylesheet>

      Thanks!

      John



      "Martin Honnen" <mahotrash@yaho o.de> wrote in message
      news:%23mtWWXN% 23EHA.1292@TK2M SFTNGP10.phx.gb l...[color=blue]
      >
      >
      > John Bowman < wrote:
      >
      >[color=green]
      >> 1) I need a definitive answer to the following debate. I've got a couple
      >> developers who disagree on the following question. We've got an XML file
      >> of numerical data results where it could contain values such as:
      >> <RESULT>0.12345 </RESULT> when created in English, but it could contain:
      >> <RESULT>0,12345 </RESULT> when created in another language... such as
      >> German or French. One developer claims this should be perfrectly allowed,
      >> the other claims you can never use commas in XML files for content and
      >> that he read it somewhere, but can't find the reference. Who's correct?[/color]
      >
      > Of course you can put a comma in there but if you want to store numerical
      > values then use the dot, you can then when you output the stored data
      > format the numbers as needed in the locale desired.
      >
      >
      > --
      >
      > Martin Honnen
      > http://JavaScript.FAQTs.com/[/color]


      Comment

      • Martin Honnen

        #4
        Re: XML &amp; Decimal Numbers



        John Bowman < wrote:

        [color=blue]
        > <DOCUMENT>
        > <RESULT>29.8220 </RESULT>
        > <RESULT>6.123 4</RESULT>
        > <RESULT>-0.0678</RESULT>
        > </DOCUMENT>
        >
        > How do I get the value of each of the RESULT's into the format-number
        > function so that it transforms them using the proper decimal-format?[/color]

        Here is an example stylesheet that uses two different formats to format
        your example input:

        <?xml version="1.0" encoding="UTF-8"?>
        <xsl:styleshe et
        xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
        version="1.0">

        <xsl:output method="xml" encoding="UTF-8" indent="yes" />

        <xsl:decimal-format
        name="de-DE"
        decimal-separator=","
        grouping-separator="." />

        <xsl:decimal-format
        decimal-separator="."
        grouping-separator="," />

        <xsl:template match="/">
        <results>
        <culture name="en-US">
        <xsl:for-each select="DOCUMEN T/RESULT">
        <result>
        <xsl:value-of select="format-number(., '0.0000')" />
        </result>
        </xsl:for-each>
        </culture>
        <culture name="de-DE">
        <xsl:for-each select="DOCUMEN T/RESULT">
        <result>
        <xsl:value-of select="format-number(., '0,0000', 'de-DE')" />
        </result>
        </xsl:for-each>
        </culture>
        </results>
        </xsl:template>

        </xsl:stylesheet>

        The result of the transformation should look alike

        <?xml version="1.0" encoding="UTF-8"?>
        <results>
        <culture name="en-US">
        <result>29.8220 </result>
        <result>6.123 4</result>
        <result>-0.0678</result>
        </culture>
        <culture name="de-DE">
        <result>29,8220 </result>
        <result>6,123 4</result>
        <result>-0,0678</result>
        </culture>
        </results>

        The documentation of xsl:format and format-number is here:
        <http://www.w3.org/TR/xslt#format-number>


        --

        Martin Honnen

        Comment

        • John Bowman

          #5
          Re: XML &amp; Decimal Numbers

          Martin,

          Thanks! The syntax of using just the period in the format-number function
          was the trick.

          John

          "Martin Honnen" <mahotrash@yaho o.de> wrote in message
          news:%23O77ROX% 23EHA.3820@TK2M SFTNGP11.phx.gb l...[color=blue]
          >
          >
          > John Bowman < wrote:
          >
          >[color=green]
          >> <DOCUMENT>
          >> <RESULT>29.8220 </RESULT>
          >> <RESULT>6.123 4</RESULT>
          >> <RESULT>-0.0678</RESULT>
          >> </DOCUMENT>
          >>
          >> How do I get the value of each of the RESULT's into the format-number
          >> function so that it transforms them using the proper decimal-format?[/color]
          >
          > Here is an example stylesheet that uses two different formats to format
          > your example input:
          >
          > <?xml version="1.0" encoding="UTF-8"?>
          > <xsl:styleshe et
          > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
          > version="1.0">
          >
          > <xsl:output method="xml" encoding="UTF-8" indent="yes" />
          >
          > <xsl:decimal-format
          > name="de-DE"
          > decimal-separator=","
          > grouping-separator="." />
          >
          > <xsl:decimal-format
          > decimal-separator="."
          > grouping-separator="," />
          >
          > <xsl:template match="/">
          > <results>
          > <culture name="en-US">
          > <xsl:for-each select="DOCUMEN T/RESULT">
          > <result>
          > <xsl:value-of select="format-number(., '0.0000')" />
          > </result>
          > </xsl:for-each>
          > </culture>
          > <culture name="de-DE">
          > <xsl:for-each select="DOCUMEN T/RESULT">
          > <result>
          > <xsl:value-of select="format-number(., '0,0000', 'de-DE')" />
          > </result>
          > </xsl:for-each>
          > </culture>
          > </results>
          > </xsl:template>
          >
          > </xsl:stylesheet>
          >
          > The result of the transformation should look alike
          >
          > <?xml version="1.0" encoding="UTF-8"?>
          > <results>
          > <culture name="en-US">
          > <result>29.8220 </result>
          > <result>6.123 4</result>
          > <result>-0.0678</result>
          > </culture>
          > <culture name="de-DE">
          > <result>29,8220 </result>
          > <result>6,123 4</result>
          > <result>-0,0678</result>
          > </culture>
          > </results>
          >
          > The documentation of xsl:format and format-number is here:
          > <http://www.w3.org/TR/xslt#format-number>
          >
          >
          > --
          >
          > Martin Honnen
          > http://JavaScript.FAQTs.com/[/color]


          Comment

          Working...