Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old May 28th, 2004, 11:47 AM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grouping Nodes

Looking at the sample XML below ...

<Products>
     <ProductsItem>
          <Identifiers>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>12345</Value>
               </Identifier>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>43512</Value>
               </Identifier>
          </Identifiers>
     </ProductsItem>
     <ProductsItem>
          <Identifiers>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>54321</Value>
               </Identifier>
          </Identifiers>
     </ProductsItem>
     <ProductsItem>
          <Identifiers>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>12112</Value>
               </Identifier>
          </Identifiers>
     </ProductsItem>
</Products>

How can I select a group of ProductsItem nodes where the count of that group's Identifier nodes is less than say four? Looking at the above example I would want my result to be only the first two ProductsItem nodes because of count of the Identifier node within those first two ProductsItem nodes is equal to three.

I've been experimenting with ...

//ProductsItem[count(Identifiers/Identifier) &lt; 4]

Unfortunately, the avove expression brings back all ProductsItem nodes in the XML. I only want the first two ProductsItem node because the combined count of the first two ProductsItem/Identifiers/Identifier nodes is three which is essentially less than four.

Any help would be greatly appreciated.

Thanks,

Ben
 
Old May 28th, 2004, 12:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

Are you saying that you only want the first three ProductsItem elements, and not all ProductsItem with less than three Identifiers?

Brian
 
Old May 28th, 2004, 12:33 PM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brian,

Because the XML will be constantly changing in terms of the number of ProductsItem nodes and corresponding child Identifier nodes, I want to be able to select as many ProductsItem nodes as possible without exceeding a maximum count of three Identifier nodes in the selected ProductsItem nodes. Wheew ...

Here is another example ... If I have ...

<Products>
     <ProductsItem>
          <Identifiers>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>43512</Value>
               </Identifier>
          </Identifiers>
     </ProductsItem>
     <ProductsItem>
          <Identifiers>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>54321</Value>
               </Identifier>
          </Identifiers>
     </ProductsItem>
     <ProductsItem>
          <Identifiers>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>12112</Value>
               </Identifier>
          </Identifiers>
     </ProductsItem>
     <ProductsItem>
          <Identifiers>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>21213</Value>
               </Identifier>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>21334</Value>
               </Identifier>
               <Identifier>
                    <Type>ECCN Nbr</Type>
                    <Value>21456</Value>
               </Identifier>
          </Identifiers>
     </ProductsItem>
</Products>

I only want the first three ProductsItem nodes because the total count of Identifier nodes in the first three ProductsItem nodes is equal to 3 which is less than four. Then, I would wan the next (4th postion) ProductsItem node by itself because it has a total of three Identifier nodes which is less than four.

Thanks for the quick response,

Ben
 
Old May 28th, 2004, 10:34 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

You may be able to use the position() method to do this. Maybe try:

//ProductsItem[./Identifiers/Identifier/position() &lt; 4]

The way to think of what the position() method does is it groups all the results together and numbers them 1-n, and you can use this to limit the resultset in that manner. I'm a little rusty in my XML lately, so I may be a little off, but I hope this gets you in the right direction.

Brian





Similar Threads
Thread Thread Starter Forum Replies Last Post
Muenchian Grouping on individual nodes in xslt1.0 sudhish.sikhamani XSLT 9 July 1st, 2008 10:32 AM
Protect cells and allow grouping/un-grouping sfreuden Excel VBA 4 December 14th, 2006 08:01 AM
Get distinct values on *second* grouping of nodes. MissHenesy XSLT 4 October 23rd, 2006 10:34 AM
Grouping orphan nodes to paranet elements ROCXY XSLT 2 January 1st, 2006 07:11 AM
Grouping stand alone nodes inside an element Bala007 XSLT 2 December 30th, 2005 02:25 PM





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