Wrox Programmer Forums
|
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 November 14th, 2003, 11:37 AM
Authorized User
 
Join Date: Oct 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default If Else

I posted this to the end of my previous message but I'm concerned that it might get over looked.
http://p2p.wrox.com/topic.asp?TOPIC_ID=6484


Running in to a slight problem, temp is not taking a value now. I think it is due to the fact that I am comparing it before I assign a value. When I try to assign it a blank string I receive the error that the variable is already in use.

Any help is appreciated
Pete



<xsl:for-each select="//Event">
<xsl:sort select="EventDate"/>
<xsl:sort select="ArriveTime"/>

<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<xsl:if test="not (EventDate = temp)" >
<td width="75"><xsl:value-of select="AirDateTime"/></td></xsl:if>

<td width="75"><xsl:value-of select="BeginTime"/></td>
<td><xsl:value-of select="Location"/></td>


<xsl:variable name="temp">
<xsl:value-of select="EventDate"/>
</xsl:variable>



</tr>
</table>

</xsl:for-each>


 
Old November 14th, 2003, 12:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

When you use a variable you must prefix a $ to its name. However, that alone won't fix your problem.

It looks like you need some preceding-sibling type syntax as you appear to be trying to compare EventDate across Event nodes. Or maybe some form of grouping would solve your problem better.

Can you elaborate on what exactly you are trying to achieve, and maybe give some sample XML and expected output. Maybe we can suggest something...

rgds
Phil
 
Old November 14th, 2003, 12:24 PM
Authorized User
 
Join Date: Oct 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok. Here is the idea:

I would like to initial set temp to NULL or some other blank value. As my code run through one cycle it will then assign a real value to temp called EventDate.

<xsl:variable name="temp">
<xsl:value-of select="EventDate"/>
</xsl:variable>

Once the loop starts again I would like to compare temp to the new EventDate. If the dates are not the same, Do <xsl:value-of select="AirDateTime"/> if they are the same do nothing.

Example XML (This is not refined) | Event is a child.

    <Event>
        <AirDateTime>08/19/2003</AirDateTime>
        <DayOfWeek>1</DayOfWeek>
        <EventDate>20030819</EventDate>
        <EndTime>11:00 AM</EndTime>
        <Note> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</Note>
        <State>FL</State>
        <Street>18 Penbroon Dr.</Street>
        <TimeZone>EST</TimeZone>
    </Event>

Again Thank you for your help
Pete

 
Old November 14th, 2003, 01:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

As I suspected, this is essentially a grouping problem. You want to group the <Event>'s by <EventDate> and for each distinct <EventDate> output the <AirDateTime> of the first Event. Is that right?

The standard technique is "Muenchian Grouping".

I don't have any refernce material or examples to hand, but it goes something like this:
1. define a key at stylesheet level which will be used to group the <Event>'s
<xsl:key name="EventsByDate" use="Event" match="EventDate"/>
2. apply a template to only the first item in each group which outputs the AirDateTime
<xsl:apply-templates select="Event[generate-id() =
        generate-id(key('EventsByDate', EventDate)[1])]"/>

<xsl:template match="Event">
  <xsl:value-of select="AirDateTime"/>
</xsl:template>

hope that's right ;)
rgds
Phil
 
Old November 14th, 2003, 01:50 PM
Authorized User
 
Join Date: Oct 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There might be a grouping problem but I do not see it. As far as my grouping is now when I transform, it is fine. The issue basically boils down to When the dates are the same Do not show the AirDate. That is the goal, Dates do not match show AirDate.

How that clears things up a little.
Pete

 
Old November 14th, 2003, 02:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Pete, variables in XSLT don't work the way you want them to - they're more akin to constants.

With just the snippets you've shown, the grouping technique will do what you want. Maybe there are other things in your stylesheet which are relevant to know in order to give you a solution that works for you.

Can you post the whole stylesheet?

rgds
Phil
 
Old November 14th, 2003, 03:04 PM
Authorized User
 
Join Date: Oct 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes I think that is the key to the problem, I'm use to programming in way different than this works.

Unfortunately I can not go into detail about the project, the circumstances do not allow it.

Maybe if I state the problem I'm trying to solve we can come to a solution.

We have an Event (parent); with child elements; EventDate, AirDate, Notes,EventName, city, etc.

EventDate is a TimeStamp with format 20031114
AirDate is a string representation on EventDate with format 11/14/2003
Notes is a blob

My output looks similiar to this (for this section)

11/14/2003 - Car Show - Miami
11/14/2003 - Dinner and Movie - Miami
11/15/2003 - Boat Show - Miami

Now the issue is for the first two events I need to remove the date from the second one because it is the same as the previous. So my resultant output should be:

11/14/2003 - Car Show - Miami
                       Dinner and Movie - Miami
11/15/2003 - Boat Show - Miami

This is why I was storing the EventDate to a temp and when the loop cycled, compare it to the new date but apparently things do not function as so.

I am sorry I could not post the other parts of this project but it is not possible.

Again any help is appreciated.Thank you
Pete

 
Old November 14th, 2003, 04:15 PM
Authorized User
 
Join Date: Oct 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Been reading this book I have in front of me. Any one know if the answer to my question deals with using preceding Axis calls?

Thank you
Pete

 
Old November 14th, 2003, 04:47 PM
Authorized User
 
Join Date: Oct 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For Future reference this is the way to solve my problem.

     <xsl:if test="( not(preceding::EventDate = EventDate) )" >










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