RE: [xsl] go from double to integer OR show just 1 or 0 decimals

Subject: RE: [xsl] go from double to integer OR show just 1 or 0 decimals
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 28 Jul 2005 10:20:51 +0100
> Thanks for explanation - however, I cannot make it work, and I'm
> struggling to understand this line:
> 
> <xsl:variable name="pic" select="$formats/format[unitCode =
> current()/../@unitCode]/@picture"/>
> 
> To me, it seems that here are too many backward-steps: /../ shouldn't
> it be ../ instead?

It should be 

<xsl:variable name="pic" select="xx:node-set($formats)/format[@unitCode =
  current()/../@unitCode]/@picture"/>

I think the ".." is correct. current() is the InputQuantity element,
current()/.. is the ThroughputStructure element, and current()/../@unitCode
is the unitCode attribute of the ThroughputStructure element.

Michael Kay
http://www.saxonica.com/


> 
> I'm using the following sample XML, and it spits out 6.00 no matter
> what the unitCode is:
> 
> XML:
> ====
> <?xml version="1.0" encoding="UTF-8"?>
> <members>
> 	  <ThroughputStructure unitCode="kg">
> 		<InputQuantity>6.00</InputQuantity>
> 		<OutputQuantity>28.00</OutputQuantity>
> 	  </ThroughputStructure>
> </members>
> 
> XSL:
> ====
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:fo="http://www.w3.org/1999/XSL/Format";>
> <xsl:variable name="formats">
>  <format unitCode="months" picture="#0"/>
>  <format unitCode="kg" picture="0.###"/>
> </xsl:variable>
> 
> 
> <xsl:template match="members">
> members-template is matched!
> <xsl:apply-templates 
> select="ThroughputStructure"></xsl:apply-templates>
> </xsl:template>
> 
> <xsl:template match="members/ThroughputStructure">
> 	<xsl:apply-templates 
> select="InputQuantity"></xsl:apply-templates>
> 	Throughput-template is matched!
> </xsl:template>
> 
> <xsl:template match="members/ThrougputStructure/InputQuantity">
>  <xsl:variable name="pic" select="$formats/format[unitCode
> =current()/../@unitCode]/@picture"/>
>  <xsl:value-of select="format-number(., $pic)"/>
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> 
> On beforehand, thanx for helping :-)
> /Christian
> 
> 
> 
> 
> 
> On 7/28/05, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> > The neat way to do this is
> > 
> > <xsl:variable name="formats">
> >   <format unitCode="months" picture="#0"/>
> >   <format unitCode="kg" picture="0.###"/>
> > </xsl:variable>
> > 
> > <xsl:template match="gr:InputQuantity">
> >   <xsl:variable name="pic" select="$formats/format[unitCode =
> > current()/../@unitCode]/@picture"/>
> >   <xsl:value-of select="format-number(., $pic)"/>
> > </xsl:template>
> > 
> > Michael Kay
> > http://www.saxonica.com/
> > 
> > > -----Original Message-----
> > > From: Christian Rasmussen [mailto:byggemandbob@xxxxxxxxx]
> > > Sent: 27 July 2005 22:34
> > > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > > Subject: [xsl] go from double to integer OR show just 1 
> or 0 decimals
> > >
> > > Hi Experts,
> > >
> > > I have a som xml like this:
> > >
> > >
> > >           <gr:ThroughputStructure unitCode="months">
> > >             <gr:InputQuantity>6.00</gr:InputQuantity>
> > >             <gr:OutputQuantity>28.00</gr:OutputQuantity>
> > >           </gr:ThroughputStructure>
> > >
> > > The unitCode attribute of ThroughputStructure is 
> enummerations and can
> > > hold one of following values:
> > > - days
> > > - months
> > > - kg
> > > - numbers
> > >
> > > The inputQuantity and OutputQuantity is double types, 
> mainly because
> > > of situations where the unitCode is numbers or kg, but if 
> the unitCode
> > > is days or months, there is really no need for two decimals.
> > >
> > > What I would like to do is:
> > >
> > > make a template that matches InputQuantity and 
> OutputQuantity, and if
> > > the ../ThroughputStructure/@unitCode is months or days, then the
> > > format for the inputQuantity OutputQuantity need just to 
> be integer or
> > > double without decimals (then there should be som round up...new
> > > problem, how is that done?).
> > >
> > > Is that possible? Is it possible to round up the decimals and just
> > > show one, or strip them totally, or convert to integer?
> > >
> > > Help is very much appreciated :-)
> > >
> > > on beforehand, thank you!
> > >
> > > /Christian Rasmussen

Current Thread