Unit conversion problem.

Discussion in 'XML' started by plopez, Feb 9, 2005.

  1. plopez

    plopez Guest

    Hi

    I'm a bit of a noob to xslt. I have a data.xml document like this..

    input.xml
    <energy>
    ..lots and lots of other data.....
    <units>(Watts)<\units>
    <value>1000.0<\value>
    </energy>

    Now I want to have an xslt stylesheet that will convert it from watts to
    kilowatts.

    output.xml
    <energy>
    ..lots and lots of other data.....
    <units>(KiloWatts)<\units>
    <value>1.0<\value>
    </energy>

    Any ideas?

    Thanks
    plopez, Feb 9, 2005
    #1
    1. Advertising

  2. plopez

    Joris Gillis Guest

    Tempore 22:50:17, die Wednesday 09 February 2005 AD, hinc in foro {comp.text.xml} scripsit plopez <plopez@spam_removethis crazy remove.nrcan.gc.ca>:

    > Hi
    >
    > I'm a bit of a noob to xslt. I have a data.xml document like this..
    >
    > input.xml
    > <energy>
    > ..lots and lots of other data....
    > <units>(Watts)<\units>
    > <value>1000.0<\value>
    > </energy>
    >
    > Now I want to have an xslt stylesheet that will convert it from watts to
    > kilowatts.
    >
    > output.xml
    > <energy>
    > ..lots and lots of other data.....
    > <units>(KiloWatts)<\units>
    > <value>1.0<\value>
    > </energy>
    >
    > Any ideas?

    Hi,

    You'll need a conversion table in XML.
    Now if only Google would output XHTML...
    http://www.google.com/search?q=1000.0 (watts) in kilowatt


    If the watt->kilowatt is really the only conversion, you can use:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="node()|@*">
    <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="units[.='(Watts)']">
    <units>(KiloWatts)</units>
    </xsl:template>

    <xsl:template match="value[preceding-sibling::*[1]='(Watts)']">
    <value><xsl:value-of select="format-number(. div 1000,'#.0')"/></value>
    </xsl:template>

    </xsl:stylesheet>


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Vincit omnia simplicitas
    Keep it simple
    Joris Gillis, Feb 10, 2005
    #2
    1. Advertising

  3. plopez

    plopez Guest

    Thanks!!

    It my real world problem was a little different.

    <name>Cooling</name>
    <units>(W)</units>
    <binned_data type="monthly">
    <index>0</index>
    <steps>744</steps>
    <active_steps>744</active_steps>
    <sum>0.000000</sum>
    </binned_data>


    But you pointed me in the right direction!

    It now my template looks like..


    <xsl:template match="//sum[parent::*/preceding-sibling::units='(W)']">
    <sum><xsl:value-of select="format-number(. div 1000,'#.0')"/></sum>
    </xsl:template>


    It probably would be better if I made the "units" value an attribute of
    "sum". That way I could use a conversion table for all units and all
    entities.

    Thanks again!

    PL



    "Joris Gillis" <> wrote in message
    news:eek:...
    > Tempore 22:50:17, die Wednesday 09 February 2005 AD, hinc in foro
    > {comp.text.xml} scripsit plopez <plopez@spam_removethis crazy
    > remove.nrcan.gc.ca>:
    >
    >> Hi
    >>
    >> I'm a bit of a noob to xslt. I have a data.xml document like this..
    >>
    >> input.xml
    >> <energy>
    >> ..lots and lots of other data....
    >> <units>(Watts)<\units>
    >> <value>1000.0<\value>
    >> </energy>
    >>
    >> Now I want to have an xslt stylesheet that will convert it from watts to
    >> kilowatts.
    >>
    >> output.xml
    >> <energy>
    >> ..lots and lots of other data.....
    >> <units>(KiloWatts)<\units>
    >> <value>1.0<\value>
    >> </energy>
    >>
    >> Any ideas?

    > Hi,
    >
    > You'll need a conversion table in XML.
    > Now if only Google would output XHTML...
    > http://www.google.com/search?q=1000.0 (watts) in kilowatt
    >
    >
    > If the watt->kilowatt is really the only conversion, you can use:
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >
    > <xsl:template match="node()|@*">
    > <xsl:copy>
    > <xsl:apply-templates select="node()|@*"/>
    > </xsl:copy>
    > </xsl:template>
    >
    > <xsl:template match="units[.='(Watts)']">
    > <units>(KiloWatts)</units>
    > </xsl:template>
    >
    > <xsl:template match="value[preceding-sibling::*[1]='(Watts)']">
    > <value><xsl:value-of select="format-number(. div 1000,'#.0')"/></value>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    >
    > regards,
    > --
    > Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    > Vincit omnia simplicitas
    > Keep it simple
    plopez, Feb 10, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VvanN
    Replies:
    5
    Views:
    479
    Phlip
    Apr 28, 2006
  2. Bill David
    Replies:
    2
    Views:
    262
    Arne Vajhøj
    Jun 18, 2008
  3. Ulrich Eckhardt

    unit-profiling, similar to unit-testing

    Ulrich Eckhardt, Nov 16, 2011, in forum: Python
    Replies:
    6
    Views:
    319
    Roy Smith
    Nov 18, 2011
  4. Bill Mosteller
    Replies:
    0
    Views:
    210
    Bill Mosteller
    Oct 22, 2009
  5. timr
    Replies:
    2
    Views:
    154
Loading...

Share This Page