Unit conversion problem.

P

plopez

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
 
J

Joris Gillis

Tempore 22:50:17 said:
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,
 
P

plopez

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 said:
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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top