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

RE: [xsl] XSL Trnasfromation - Is it possible to do a bottom up transformation?


Subject: RE: [xsl] XSL Trnasfromation - Is it possible to do a bottom up transformation?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 19 Nov 2007 10:47:22 -0000

>When the template matches line with LineNumber 1, the UnitPrice for
LineNumber 1.1, 2, 3 are not yet know.

You're thinking about this in terms of order-of-evaluation, which is
undefined. Use of time-related words like "when" and "not yet" is therefore
best avoided. 

Functionally, you are saying that the UnitPrice of LineNumber 1 has a
functional dependency on the UnitPrice of LineNumber 1.1, 1.2, 1.3 etc. 

You stated:

1. If the itemTypeCode of the current Item is '1' then unitPrice is netPrice
2. If the itemTypeCode of the current Item is '2' then unitPrice is sum of
all the (unitPrice/quantity) of all the child lines 3. If the itemTypeCode
of the current Item is '3' then unitPrice is Sum of unitPrice of only the
first level child elements

The above defn should be applied at all levels where unitPrice is
calculated.

That looks to me like a recursive function:

<xsl:function name="f:unitPrice" as="xs:decimal">
  <xsl:param name="item" as="element(orderline)">
  <xsl:choose>
    <xsl:when test="$item/itemTypeCode = 1">
      <xsl:sequence select="$item/netPrice"/>
    </xsl:when>
    <xsl:when test="$item/itemTypeCode = 2">
      <xsl:sequence select="sum(for $c in $item/child::orderline 
                                return f:unitPrice($c) * $c/quantity)"/>
    </xsl:when>
    <xsl:when test="$item/itemTypeCode = 3">
      <xsl:sequence select="??"/>
      <!-- I don't understand the difference between this and the previous
case -->
    </xsl:when>
  </xsl:choose>
</xsl:function>

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

> -----Original Message-----
> From: Raghu Narayan Koratagere [mailto:raghu.k.n@xxxxxxxxx] 
> Sent: 19 November 2007 10:32
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] XSL Trnasfromation - Is it possible to do 
> a bottom up transformation?
> 
> Micheal,
> 
> To clarify, here are the source and example of transformed xmls:
> 
> Source Tree:
> =========
> <orderline>
>       <linenum>1</linenum>
>       <productName>Computer</productName>
>       <netPrice>100</netPrice>
>       <itemTypeCode>3</itemTypeCode>
>       <quantity>1</quantity>
>       <orderline>
>           <linenum>1.1</linenum>
>           <productName>CPU</productName>
>           <netPrice>100</netPrice>
>           <itemTypeCode>3</itemTypeCode>
>           <quantity>2</quantity>
>           <orderline>
>               <linenum>1.1.1</linenum>
>               <productName>Mother Board</productName>
>               <netPrice>100</netPrice>
>               <itemTypeCode>2</itemTypeCode>
>               <quantity>2</quantity>
>           </orderline>
>           <orderline>
>               <linenum>1.1.2</linenum>
>               <productName>Processor</productName>
>               <netPrice>200</netPrice>
>               <itemTypeCode>2</itemTypeCode>
>               <quantity>2</quantity>
>           </orderline>
>     </orderline>
>     <orderline>
>         <linenum>2</linenum>
>         <productName>Table</productName>
>         <netPrice>100</netPrice>
>         <itemTypeCode>2</itemTypeCode>
>         <quantity>1</quantity>
>     </orderline>
>     <orderline>
>         <linenum>3</linenum>
>         <productName>UPS</productName>
>         <netPrice>100</netPrice>
>         <itemTypeCode>1</itemTypeCode>
>         <quantity>1</quantity>
>     </orderline>
> </orderline>
> 
> 
> Transformed Tree:
> =============
> <orderline>
>       <linenum>1</linenum>
>       <productName>Computer</productName>
>       <netPrice>100</netPrice>
>       <itemTypeCode>3</itemTypeCode>
>       <unitPrice>350</unitPrice> <!-- 150+100+100=350 -->
>       <quantity>1</quantity>
>       <orderline>
>           <linenum>1.1</linenum>
>           <productName>CPU</productName>
>           <netPrice>100</netPrice>
>           <itemTypeCode>3</itemTypeCode>
>           <unitPrice>150</unitPrice> <!-- 50+100=150 -->
>           <quantity>2</quantity>
>           <orderline>
>               <linenum>1.1.1</linenum>
>               <productName>Mother Board</productName>
>               <netPrice>100</netPrice>
>               <itemTypeCode>2</itemTypeCode>
>               <unitPrice>50</unitPrice> <!-- 100/2=50 -->
>               <quantity>2</quantity>
>           </orderline>
>           <orderline>
>               <linenum>1.1.2</linenum>
>               <productName>Processor</productName>
>               <netPrice>200</netPrice>
>               <itemTypeCode>2</itemTypeCode>
>               <unitPrice>100</unitPrice> <!-- 200/2=100 -->
>               <quantity>2</quantity>
>           </orderline>
>     </orderline>
>     <orderline>
>         <linenum>2</linenum>
>         <productName>Table</productName>
>         <netPrice>100</netPrice>
>         <itemTypeCode>2</itemTypeCode>
>         <unitPrice>100</unitPrice> <!-- 100/1=100 -->
>         <quantity>1</quantity>
>     </orderline>
>     <orderline>
>         <linenum>3</linenum>
>         <productName>UPS</productName>
>         <netPrice>100</netPrice>
>         <itemTypeCode>1</itemTypeCode>
>         <unitPrice>100</unitPrice> <!-- 100/1=100 -->
>         <quantity>1</quantity>
>     </orderline>
> </orderline>
> 
> 
> Issue:
> ====
> When the template matches line with LineNumber 1, the 
> UnitPrice for LineNumber 1.1, 2, 3 are not yet know. So what 
> values will the function take to calculate?
> 
> I have not tried using the below rule, will try :
> <xsl:template match="unitPrice[../itemTypeCode = '3']">
>    <xsl:copy><xsl:value-of select="sum(../unitPrice[1] div 
> ../quantity[1])" /></xsl:copy> </xsl:template>
> 
> Thanks,
> Raghu
> 
> 
> On 11/19/07, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> >
> > > The problem is here you are transforming top down.
> >
> > I don't understand your fixation with these terms bottom-up 
> and top-down.
> > Think functionally: x is a function of y. And it really 
> doesn't matter 
> > whether a transformation is top-down, bottom-up, outside-in, or 
> > inside-out, so long as it produces the right answer.
> >
> > If Abel's solution produces the wrong answer then you need 
> to explain 
> > the requirement more clearly. Sample input and output is 
> often useful 
> > if it's difficult to explain it in clear English.
> >
> > Michael Kay
> > http://www.saxonica.com/


Current Thread
Keywords
xsl