[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] XSLT: Taking Value of One Attribute and Smearing it Across multiple times


Subject: Re: [xsl] XSLT: Taking Value of One Attribute and Smearing it Across multiple times
From: George Cristian Bina <george@xxxxxxxxxxxxx>
Date: Fri, 04 May 2007 15:46:16 +0300

Hi Rex,

See below an XSLT 1.0 solution.
The core is the putValue template that can do the job by itself.
If we have enough nodes in the document then we can use a for-each instruction.



<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="offers"> <xsl:variable name="n" select="fcst/@end_hr - fcst/@start_hr"/> <xsl:choose> <xsl:when test="count(//node())>=$n"> <xsl:variable name="val" select="fcst/@value"/> <xsl:for-each select="//node()[position()&lt;=$n]"> <xsl:value-of select="$val"/> <xsl:if test="position()!=last()">, </xsl:if> </xsl:for-each> </xsl:when> <xsl:otherwise> <xsl:call-template name="putValue"> <xsl:with-param name="value" select="fcst/@value"/> <xsl:with-param name="n" select="$n"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template>

<xsl:template name="putValue">
<xsl:param name="n"/>
<xsl:param name="value"/>
<xsl:if test="$n>0">
<xsl:variable name="result">
<xsl:call-template name="putValue">
<xsl:with-param name="value" select="$value"/>
<xsl:with-param name="n" select="($n - $n mod 2) div 2"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$result"/>
<xsl:if test="$result!=''">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="$result"/>
<xsl:if test="$n mod 2 =1">
<xsl:if test="$result!=''">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="$value"/>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>


Regards,
George
---------------------------------------------------------------------
George Cristian Bina - http://aboutxml.blogspot.com/
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


Rex Rex wrote:
Thanks, George. Unfortunately, I am using XSLT in Oracle, which is
only XSLT 1.0. So, I am wondering if I could achieve this within XSLT
1.0. I tried playing with <xsl:variable> but then soon discovered that
it is only declarative and that I cannot keep modifying it like a
"counter" that would run from "start_hr" to "end_hr" to print out the
extra values needed. So, basically I am stuck.

Is it NOT possible to achieve what I need within XSLT 1.0? Kindly opine.

Thanks,
Rex

On 5/4/07, George Cristian Bina <george@xxxxxxxxxxxxx> wrote:
Hi Rex,

If XSLT 2.0 is ok for you then all you need is as simple as:

<xsl:template match="offers">
  <xsl:value-of select="for $i in fcst/@start_hr to fcst/@end_hr return
(fcst/@value)" separator=","/>
</xsl:template>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina - http://aboutxml.blogspot.com/
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


Rex Rex wrote: > I am new to XSLT. I have this problem to resolve. > > For a given XML tree like this, where a single value is specified for > a range of hours -- from hour 1 to end hour 10, I would like to > explode this single value ten times. > > i.e., for a source XML like this, > > <?xml version="1.0"?> > <offers> > <fcst start_hr="1" end_hr="10" value="100"/> > </offers> > > I need an output like this... (explode the value of 100 from hr=1 to > hr=10, ten times.) > > 100,100,100,100,100,100,100,100,100,100


Current Thread