Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with using XSLT to extract data

Status
Not open for further replies.

jba6511

Programmer
Jun 15, 2007
23
US
Before anyone asks, this is not homework, I am trying to teach myself xml using SAMS textbook. I am working with an xml file that has several dates listed for each room. I am trying to list the room, followed by all of the dates that it has been booked for. I can sucessfully list each room, however, I can only get one date to show up per room. What can I do in order to list all of the dates the room was booked for? Do I manually need to add each one or is there a way to do a list all?

Here is what I have now:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:eek:utput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="bookings">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="room">
<p>
<xsl:value-of select="@name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="date/month"/>
<xsl:value-of select="date/day"/>
<xsl:value-of select="date/year"/>
</p>
</xsl:template>
</xsl:stylesheet>

And here is a sample of one of the rooms and dates from the xml I am working with:

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="D:\ISM 660\Assignment 3\Solutions\Question 1\Question 1.xslt?>
<!-- fictional today's date: 2000/06/23 -->
<bookings>
<lastUpdated date='20000623' time='1317'/>
<room name='Red'>
<date>
<year>2000</year>
<month>June</month>
<day>23</day>
<meeting>
<period>
<from>1300</from>
<to>1330</to>
</period>
<bookedBy>John Smith</bookedBy>
<attendees>
<attendee><individual>Jennifer Platt</individual></attendee>
<attendee><group>NewBieNetGroup</group></attendee>
<attendee><individual>Peter Shanahan</individual></attendee>
<attendee><individual>Susan Zak</individual></attendee>
</attendees>
<meetingTitle>Project Status</meetingTitle>
<purpose>The client is not very happy about the pace of
development. This meeting is booked to find out the actual
status of the project before we get back to the
client.</purpose>
<bookedAt date='20000413' time='1004'/>
<reminder><email>jsmith@peacockpaint.com</email></reminder>
</meeting>
<meeting>
<period>
<from>1430</from>
<to>1530</to>
</period>
<bookedBy>Jon Khan</bookedBy>
<attendees>
<attendee><group>Pheasants</group></attendee>
</attendees>
<meetingTitle>Javascript Code Review</meetingTitle>
<purpose>We will review the Javascript of
the "Golden Pheasant Pub" web site.</purpose>
<bookedAt date='20000324' time='1534'/>
</meeting>
</date>
<date>
<year>2000</year>
<month>July</month>
<day>10</day>
<meeting>
<period>
<from>0730</from>
<to>0900</to>
</period>
<bookedBy>Joan Leclair</bookedBy>
<attendees>
<attendee><group>Project Managers</group></attendee>
</attendees>
<meetingTitle>Breakfast Meeting for Future
Projects</meetingTitle>
<purpose>We will start development for five projects in
August. We must meet to dicuss coordination among ourselves on how
these projects can be handled properly.</purpose>
<bookedAt date='20000328' time='1657'/>
</meeting>
</date>
</room>
 
The missing piece of the puzzle is the xsl:for-each element. If the Sam's book doesn't have that, or you find its explanation confusing, feel free to come back with additional questions.

Also, a development environment with XSLT debugging can be quite helpful. Two that I have used are Stylus Studio and XML Spy. I think each has a try-before-buy evaluation. With these you can step through each XSLT element as it 'executes' and therefore better understand the source of your symptoms.

And, since you are learning, can you determine why you were seeing only one date (which should be the first date in document order)?

Tom Morrison
 
Thanks for the reply. I got it working now using the xsl:for-each select=" ". What is I wanted to just pull out some pieces of data. Say I only wanted to get the names of the individuals who were in a meeting in the Red room and the Green room. I can do this by listing all the rooms and all of the individuals, but how do I only select to work with those 2 rooms or is this even possible?
 
<xsl:apply-templates/>
[tt]<xsl:apply-templates select="room[@name='Red' or @name='Green']" />[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top