Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 9th, 2006, 04:23 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question...

As per the advice of Micheal, I've tried to group records using "preceding" keyword.

Input XML:

<Orders>
<Order>
        <Header>
            <BillId>12345</BillId>
            <Category>Commercial</Category>
            <State>TX</State>
        </Header>
        <Line>
        </Line>
</Order>
<Order>
        <Header>
            <BillId>98765</BillId>
            <Category>Educational</Category>
            <State>CA</State>
        </Header>
        <Line>
        </Line>
</Order>
</Orders>

Desired Output XML::

<Orders>
    <TaxableCA>
        <CategoryType>
            <ProcessingUnit>
                <Category>Educational</Category>
            </ProcessingUnit>

                        <Order>
                              <Header>
                                   <BillId>98765</BillId>
                                   <Category>Educational</Category>
                                   <State>CA</State>
                              </Header>
                              <Line>
                              </Line>
                         </Order>
                 </CategoryType>
    </TaxableCA>

    <Taxable>
        <CategoryType>
            <ProcessingUnit>
                <Category>Commercial</Category>
            </ProcessingUnit>

                        <Order>
                              <Header>
                                   <BillId>12345</BillId>
                                   <Category>Commercial</Category>
                                   <State>TX</State>
                              </Header>
                              <Line>
                              </Line>
                         </Order>
                 </CategoryType>
    </Taxable>
</Orders>


My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>


    <xsl:variable name="isCA1" select="/*/Order/Header[State[.='CA'] and Category[not(.=preceding::Category)]]"></xsl:variable>
    <xsl:variable name="notCA1" select="/*/Order/Header[State[.='TX'] and Category[not(.=preceding::Category)]]"></xsl:variable>

    <xsl:template match="/*">
        <xsl:copy>


        <TaxableCA>



                    <xsl:for-each select="$isCA1">
                    <CategoryType>
                        <xsl:variable name="categoryname" select="Category"></xsl:variable>
                        <ProcessingUnit>
                            <xsl:copy-of select="$categoryname"></xsl:copy-of>
                        </ProcessingUnit>

                           <xsl:for-each select="/*/Order[(Header/Category=$categoryname) and (Header/State='CA')]">

                                <xsl:copy-of select="."></xsl:copy-of>

                        </xsl:for-each>
                    </CategoryType>
            </xsl:for-each>

        </TaxableCA>

        <Taxable>



                    <xsl:for-each select="$notCA1">
                    <CategoryType>
                        <xsl:variable name="categoryname" select="Category"></xsl:variable>
                        <ProcessingUnit>
                            <xsl:copy-of select="$categoryname"></xsl:copy-of>
                        </ProcessingUnit>

                          <xsl:for-each select="/*/Order[(Header/Category=$categoryname) and (Header/State='TX')]">

                                <xsl:copy-of select="."></xsl:copy-of>

                        </xsl:for-each>
                    </CategoryType>
            </xsl:for-each>

        </Taxable>

        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>



Basically, I am trying to deepen the hierarchy in two levels. 1st Level is according to state (All CA orders to stay in one "TaxableCA" tag and all other states to go under a different "Taxable" tag. Then, underneath each tag, I would like to create another hierarchy node grouped according to the Category type. I.e. all CA commerical orders groups under one tag etc....

When I perform the transformatino, only the first (TX) order is being being up and the resulting XML doesn't have the CA order at all..

Any help would be grealy appreciated..

Thanks

  #2 (permalink)  
Old April 10th, 2006, 03:05 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This code:

    <xsl:variable name="isCA1" select="/*/Order/Header[State[.='CA'] and Category[not(.=preceding::Category)]]">

is selecting orders from California provided their category is different from all previous categories. I imagine that what you really want is orders from California provided that their category is different from the categories of all previous California orders. Which would be

select="/*/Order/Header[State[.='CA'] and Category[not(.=preceding::Category[../State='CA'])]]">

Not really an XSLT problem, just an error in your logic...

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old April 10th, 2006, 09:09 AM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeepers... Can't believe I overlooked such a stupid and silly mistake..

Having said so, do you think there is any other better (efficiency) way of executing this data transformation?

Thanks for all your help.



  #4 (permalink)  
Old April 10th, 2006, 09:44 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, it's a standard grouping problem, so the choices are

* xsl:for-each-group in XSLT 2.0

* Muenchian grouping in XSLT 1.0
  - see http://www.jenitennison.com/xslt/grouping



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


Similar Threads
Thread Thread Starter Forum Replies Last Post
question maheshraju ASP.NET 2.0 Basics 3 March 13th, 2008 08:54 AM
Question Ashwini Classic ASP Databases 3 January 10th, 2006 07:20 AM
Question? Calibus Classic ASP Databases 8 August 6th, 2004 08:25 AM
a question gorji C++ Programming 2 August 11th, 2003 07:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.