 |
| 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
|
|
|
|

March 17th, 2008, 09:16 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Have you tried the code? For your input sample it gives you two group elements, one with two elements, the second with one element. If you want to process distinct elements you would only process the first element in each group.
You have already been pointed to http://www.jenitennison.com/xslt/grouping, it explains how Muenchian grouping works.
|
|

March 18th, 2008, 05:00 AM
|
|
Authorized User
|
|
Join Date: Feb 2008
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have created a key
<xsl:key name="prepackIndicatorUpcCodeKey" match="LineItem"
use="concat(VariableDataSet/VariableDataElement[@Code = '750']/@Value, '-', VariableDataSet/VariableDataElement[@Code = '790']/@Value)" />
and assigned it to a variable
<xsl:variable name="distinctPrepackIndicatorUpcCode"
select="$activeLineItems[generate-id(.) = generate-id(key('prepackIndicatorUpcCodeKey', concat(VariableDataSet/VariableDataElement[@Code = '750']/@Value, '-',
VariableDataSet/VariableDataElement[@Code = '790']/@Value))[1])]" />
After this i am looping through the above records returned from $distinctPrepackIndicatorUpcCode.
<table>
<xsl:for-each select="$distinctPrepackIndicatorUpcCode">
<tr>
<td>Prepack Item:</td>
<td><xsl:value-of select="./VariableDataElement[@Code='750']"/></td>
<td>Prepack UPC:</td>
<td><xsl:value-of select="1"/></td>
</tr>
</table>
Here i want to retrieve the VariableDataSet/VariableDataElement[@Code = '750']/@Value and VariableDataSet/VariableDataElement[@Code = '790']/@Value seperately and display above for Prepack Item and Prepack UPC.
I tried giving VariableDataSet/VariableDataElement[@Code = '750']/@Value but it does not get me the value.
|
|

March 18th, 2008, 07:17 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Your variable is a node-set of 'LineItem' elements. The 'VariableDataElement' elements are not children of 'LineItem' elements, rather they are grandchildren contained in a 'VariableDataSet' element. Furthermore the 'VariableDataElement' elements are empty and don't have any contents so I don't understand why you use <xsl:value-of select="./VariableDataElement[@Code='750']"/>.
|
|

March 18th, 2008, 07:31 AM
|
|
Authorized User
|
|
Join Date: Feb 2008
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I was able to retrive the data.Thanks
|
|

March 19th, 2008, 06:29 AM
|
|
Authorized User
|
|
Join Date: Feb 2008
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Based on the key i was able to get the distinct set of values.But these values can have multiple records(like multiple LineItems havign the same values).
I am able to loop through the distinct values returned by the key(concat(VariableDataSet/VariableDataElement[@Code = '750']/@Value, '-',
VariableDataSet/VariableDataElement[@Code = '790']/@Value)).
But i dont know how can i loop through the LineItems for each distinct value.
|
|

March 19th, 2008, 06:46 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
I had already posted an example that processes the first LineItem element with a distinct key value:
Code:
<xsl:apply-templates
select="LineItem[generate-id() = generate-id(key('group', concat(VariableDataSet/VariableDataElement[@Name = 'Prepack Indicator']/@Value,
'_',
VariableDataSet/VariableDataElement[@Name = 'UPC Code']/@Value))[1])]"/>
You have choosen a slightly different key but otherwise you can use the same approach.
|
|

March 19th, 2008, 07:08 AM
|
|
Authorized User
|
|
Join Date: Feb 2008
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So in this case should i need to use the for-each loop to loop through the LineItems.Should i be using the key value.
<xsl:template match="LineItem">
<group key="{concat(VariableDataSet/VariableDataElement[@Name = 'Prepack Indicator']/@Value,'_', VariableDataSet/VariableDataElement[@Name = 'UPC Code']/@Value)}"
item-count="{count(key('group', concat(VariableDataSet/VariableDataElement[@Name = 'Prepack Indicator']/@Value,
'_',
VariableDataSet/VariableDataElement[@Name = 'UPC Code']/@Value)))}"/>
</xsl:template>
|
|

March 19th, 2008, 07:45 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
You can use either xsl:for-each or xsl:apply-templates but if you use xsl:apply-templates as in my example then the predicate
Code:
LineItem[generate-id() = generate-id(key('key-name', keyvalue)[1])]
already makes sure that only the LineItem elements with a distinct key are processed.
|
|
 |