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 October 30th, 2007, 05:27 AM
Registered User
 
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default newbie - grouping from different areas?

I am having problems finding the answer to this mainly because i don't know what to search for. i have the following xml

 
Quote:
quote:
Quote:
<event>
    <tag>123456</tag>
    <details>detail 1</detail>
    <price>2.00</detail>
</event>
<event>
    <tag>123456</tag>
    <details>detail a</detail>
    <price>1.50</detail>
</event>
<event>
    <tag>456789</tag>
    <details>detail again</detail>
    <price>0.99</detail>
</event>
<event>
    <tag>123456</tag>
    <details>detail 1</detail>
    <price>3.25</detail>
</event>
<event>
    <tag>456789</tag>
    <details>detail 1</detail>
    <price>10000.00</detail>
</event>
<event>
    <tag>123456</tag>
    <details>more detail</detail>
    <price>23.00</detail>
</event>
<product>
    <tag>123456</tag>
    <description>product number 1</description>
</product>
<product>
    <tag>456789</tag>
    <description>product number 1</description>
</product>
and i need to gather information from all over the file to produce this
 
Quote:
quote:123456 - product number 1
Quote:
detail 1 2.00
detail a 1.50
detail 1 3.25
more detail 23.00

456789 - product number 1
detail again 0.99
detail 1 100000.00
i have tried using
<xsl:if test=""> but cannot get it to accept a variable of another tag.

can anyone point me in the right direction.






 
Old October 30th, 2007, 05:55 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Looks like a classic example of grouping to me. I don't suppose you are using XSLT 2.0? In which case <xsl:for-each-group> is your friend.

Otherwise its back to http://www.jenitennison.com/xslt/gro...muenchian.html for the 1.0 version...

/- Sam Judson : Wrox Technical Editor -/
 
Old October 30th, 2007, 05:56 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I assume your XML has a root element that you haven't shown and is well-formed with matching tags rather than the mish-mash posted?
You basically need a template that matches product and within there process all the event element with the matching tag. So assuming this XML:
Code:
<data>
  <event>
      <tag>123456</tag>
      <details>detail 1</details>
      <price>2.00</price>
    </event>
    <event>
      <tag>123456</tag>
      <details>detail a</details>
      <price>1.50</price>
    </event>
    <event>
      <tag>456789</tag>
      <details>detail again</details>
      <price>0.99</price>
    </event>
    <event>
      <tag>123456</tag>
      <details>detail 1</details>
      <price>3.25</price>
  </event>
  <event>
      <tag>456789</tag>
      <details>detail 1</details>
      <price>10000.00</price>
    </event>
    <event>
      <tag>123456</tag>
      <details>more detail</details>
      <price>23.00</price>
  </event>
  <product>
      <tag>123456</tag>
      <description>product number 1</description>
  </product>
  <product>
      <tag>456789</tag>
      <description>product number 1</description>
  </product>
</data>
you need something like:
Code:
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
<xsl:template match="/">
  <xsl:apply-templates select="*/product"/>    
</xsl:template>

  <xsl:template match="product">
    <xsl:value-of select="tag"/>
    <xsl:text> - </xsl:text>
    <xsl:value-of select="description"/>
    <xsl:text>
</xsl:text>
    <xsl:apply-templates select="/*/event[tag = current()/tag]"/>
    <xsl:text>
</xsl:text>
  </xsl:template>

  <xsl:template match="event">
    <xsl:apply-templates select="details"/>
  </xsl:template>

  <xsl:template match="details">
    <xsl:value-of select="."/><xsl:text>
</xsl:text>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old October 30th, 2007, 07:13 AM
Registered User
 
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by joefawcett
 I assume your XML has a root element that you haven't shown and is well-formed with matching tags rather than the mish-mash posted?
You basically need a template that matches product and within there process all the event element with the matching tag. So assuming this
Joe (Microsoft MVP - XML)
yes, i have a root element and the tags do match. the xml i have been given to work with has 5 millions lines, and not very obvious tags so i was trying to simplify matters. sorry.

thanks for your help.

i am using xslt 1.0, as i have been working through the tutorials on www.w3schools.com, i have also just ordered "Learning XSLT"
M Fitzgerald. am i learning old technology? should i be jumping straight into XSLT 2.0?

again thanks for your help.



 
Old October 30th, 2007, 08:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>should i be jumping straight into XSLT 2.0?

Only you can judge. For many stylesheets, 2.0 will double your productivity and it will enable you to tackle problems that simply couldn't be done with 1.0. On the other hand, at present there are fewer processors available and they work in a smaller number of environments.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 30th, 2007, 09:46 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The other improvement to make is to declare a key that retrieves event elements via tag. Then use that instead of the construct I showed. As Michael says, if you are sure you can use a version 2.0 processor, Saxon for example, there's hardly any reason not to use it.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking to specific areas in a new page CathyM BOOK: Beginning ASP 3.0 2 August 2nd, 2005 09:23 AM
1:M sorting or grouping cant explain :: newbie Q auser99 XSLT 4 April 29th, 2005 11:57 AM
Section Grouping (XSL Newbie!) vjlomas XSLT 1 February 4th, 2005 04:00 AM
Formatting Areas happyslug BOOK: Professional Crystal Reports for VS.NET 2 January 20th, 2005 01:22 PM
Login Page going to 3 different management areas stacy Classic ASP Databases 1 January 21st, 2004 09:18 AM





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