Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Unit conversion problem.

Reply
Thread Tools

Unit conversion problem.

 
 
plopez
Guest
Posts: n/a
 
      02-09-2005
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


 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      02-10-2005
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....29+in+kilowatt


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

<xsl:stylesheet version="1.0"
xmlnssl="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-v...i?userid=38041)
Vincit omnia simplicitas
Keep it simple
 
Reply With Quote
 
 
 
 
plopez
Guest
Posts: n/a
 
      02-10-2005
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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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....29+in+kilowatt
>
>
> If the watt->kilowatt is really the only conversion, you can use:
>
> <xsl:stylesheet version="1.0"
> xmlnssl="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-v...i?userid=38041)
> Vincit omnia simplicitas
> Keep it simple



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Friendly Easy To Install Unit For Voip Like Vonage Unit SuggestionsPlease? Heidy UK VOIP 1 06-22-2007 09:53 AM
unit--, a unit test framework for C++ VvanN C++ 5 04-28-2006 10:01 AM
connect a source unit with an S-Video connection to a receiving unit that has an RCA composite video connection ? worth it ? OCZ Guy DVD Video 6 08-01-2004 05:44 PM
Connect a source unit DVD with an S-Video connection to a receiving unit TV that has an RCA composite video connection Worth it. OCZ Guy Computer Information 0 07-31-2004 12:29 PM



Advertisments