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 December 21st, 2009, 09:34 AM
Registered User
 
Join Date: Dec 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Red face Beginner help: <xsl:apply-template> spits out everything then does the correct match

Hello all , I am using windows Vista & stylus 09, and saxon 9.1

I'm taking a course on xml at my uni and have gone through alot of material but am lacking in experience

The xml has root element "restaruantguide" and many "restaurant" children, each with random bits of data bout restaurant (cuisin, opening time,etc) i want to output a html file (with css) spitting out info about the restaurant sorted in different ways, the first is wether or not it has seasonal offers (seasonal is a child element of restaurant)

in my first template i match the root element and add all the html tages and the head and all the css required. i know it works but is it the best way to do so.

in my 2nd template:

<xsl:template match="RESTAURANT[SEASONAL]">
<tr><td>
<xsl:value-of select="NAME"/>
</td>
<td>
<xsl:value-of select="ACCEPTEDCARDS"/>
</td>
<td>
<xsl:value-of select="ADDRESS"/>
</td>
</tr>

</xsl:template>

from this i undestand that it will match restaurant elements that have the SEASONAL element which is 2/4 retaurants, but what happens is just after the table is started in the first template it spits out all information about all restaurants and then does the matching and adds it into the table.

Thanks for bearing with me with this and i thank you for reading









My XML=============================================== ==
<?xml version="1.0"?>
<!DOCTYPE RESTAURANTGUIDE SYSTEM "Ass2.dtd">
<?xml-stylesheet type="text/css" href="style.css"?>
<RESTAURANTGUIDE>
<RESTAURANT STATUS="SELECTED" BOOKONLINE="TRUE" CHILDRENWELCOME="TRUE" GROUPSALLOWED="TRUE" AIRCONDITIONING="TRUE" OUTSIDESEATING="FALSE" RESERVATIONS="TRUE" COVERCHARGE="FALSE">
<NAME>Roast</NAME>
<ADDRESS>
<STREETNUMBERORNAME>The Flora Hall, Borough Market</STREETNUMBERORNAME>
<STREETNAME>Stoney Street</STREETNAME>
<CITY>London</CITY>
<POSTCODE>SE1 1TL</POSTCODE>
<COUNTRY>United Kingdom</COUNTRY>
</ADDRESS>
<CUISINE>Modern British</CUISINE>
<AVGLUNCH>35</AVGLUNCH>
<AVGDINNER>55</AVGDINNER>
<CONTACT>
<TEL>+44(0)8445672354</TEL>
<EMAIL>[email protected]</EMAIL>
<WEBSITE>http://www.roast-restaurant.com/</WEBSITE>
</CONTACT>

<OPENINGTIMES>

<MON-FRI>07:00 - 11:00,12:00 - 14:45, 15:30 - 22:30</MON-FRI>


<SAT> 08:00 - 11:30, 12:30- 15:45</SAT>
<SUN>11:30-18:00 </SUN>
</OPENINGTIMES>

<ACCEPTEDCARDS>American express, Delta, Mastercard, Meatro, Visa</ACCEPTEDCARDS>
<HOUSEREDPRICE>18 </HOUSEREDPRICE>
<HOUSEWHITEPRICE>18</HOUSEWHITEPRICE>
<SERVICECHARGE OPTIONAL="TRUE">12.5% </SERVICECHARGE>
</RESTAURANT>
<RESTAURANT STATUS="FEATURED" BOOKONLINE="TRUE" CHILDRENWELCOME="TRUE" GROUPSALLOWED="TRUE" AIRCONDITIONING="TRUE" OUTSIDESEATING="FALSE" RESERVATIONS="TRUE" COVERCHARGE="FALSE">
<NAME>Blueprint Cafe
</NAME>

<ADDRESS>
<STREETNUMBERORNAME>Design Musuem</STREETNUMBERORNAME>
<STREETNAME>Butlers Wharf</STREETNAME>
<CITY>London</CITY>
<POSTCODE>SE1 2YD</POSTCODE>
<COUNTRY>United Kingdom</COUNTRY>
</ADDRESS>
<CUISINE>Modern British</CUISINE>
<AVGLUNCH>26</AVGLUNCH>
<AVGDINNER>45</AVGDINNER>
<CONTACT>
<TEL>+44(0)2073787031</TEL>
<EMAIL>[email protected]</EMAIL>
<WEBSITE>http://www.blueprintcafe.co.uk/</WEBSITE>
</CONTACT>
<SEASONAL>Fixed Price Dinner Menu 2 courses for £22.50 / 3 courses for £27.50</SEASONAL>
<OPENINGTIMES>


<MON-SAT>12:00-15:00,18:00-23:00</MON-SAT>

<SUN>12:00-16:00 </SUN>
</OPENINGTIMES>
<REVIEW DATE="17 October 2007">
<NAME>Josy</NAME>
<INFO>We went there for my husband's birthday after receiving vouchers for a two course meal. The view is impressive and the cafe is located in a great area, right next to Tower Bridge. When we arrived we ordered a mojito and a gin and tonic. The waitress looked at me blankly and repeated mojito, but took the order and left the table. What returned was some kind of rum based drink with no mint or sugar, clearly not a mojito. Nevertheless, I drank it, though in hindsight should've complained then.

The starters were the highlight of the meal, the chicken liver pate was served with toasted bread, which was nice but nothing special and the salsify was coated delicately with parmesan, the highlight of the meal. The vegetarian main was polenta, I chose it because there were only three choices, and I didn't want fish, artichoke and goats’ cheese. It was quite bland and needed something like pesto to give it life. The beef main, which my husband had, was delicious and tender, if not too salty.

In such a great environment and considering we were early and it was not that busy, I would've expected better service. We also ordered dessert - the crème brulee came out with the top quite burnt, however the lemon dessert was delicious and creamy. Would I recommend it? Probably not due to me not thinking that we got value for money .And if they serve cocktails they should at least know what a mojito is. Mainly we found the waitress unwilling to please or help and didn't seem to be able to answer any questions about the menu. </INFO>
</REVIEW>
<ACCEPTEDCARDS>American Express, MasterCard, Meastro, Visa</ACCEPTEDCARDS>
<HOUSEREDPRICE> 15</HOUSEREDPRICE>
<HOUSEWHITEPRICE>15</HOUSEWHITEPRICE>
<SERVICECHARGE OPTIONAL="TRUE"> 12.5%</SERVICECHARGE>
</RESTAURANT>
<RESTAURANT STATUS="FEATURED" BOOKONLINE="TRUE" CHILDRENWELCOME="TRUE" GROUPSALLOWED="TRUE" AIRCONDITIONING="TRUE" OUTSIDESEATING="TRUE" RESERVATIONS="TRUE" COVERCHARGE="FALSE">
<NAME>Cantina Del Ponte
</NAME>
<ADDRESS>
<STREETNUMBERORNAME>Butlers Wharf Building, 36C</STREETNUMBERORNAME>
<STREETNAME>Shad Thames</STREETNAME>
<CITY>London</CITY>
<POSTCODE>SE1 2YE</POSTCODE>
<COUNTRY>United Kingdom</COUNTRY>
</ADDRESS>
<CUISINE>Italian</CUISINE>
<AVGLUNCH>25</AVGLUNCH>
<AVGDINNER>40</AVGDINNER>
<CONTACT>
<TEL>+44(0)2074035403</TEL>
<EMAIL>[email protected]</EMAIL>
<WEBSITE>http://www.cantina.co.uk/</WEBSITE>
</CONTACT>
<SEASONAL>Fixed Price Lunch Set Menu 2 courses £10.95 or 3 courses £13.95</SEASONAL>
<SEASONAL>Fixed Price Dinner Set Menu 2 courses inc glass of wine £15.75 or 3 courses inc glass of wine £18.75</SEASONAL>
<OPENINGTIMES>

<MON-SAT>12:00-15:00,18:00-23:00</MON-SAT>

<SUN>12:00-15:00,18:00-22:00 </SUN>
</OPENINGTIMES>
<REVIEW DATE="8 February 2008">
<NAME>Ricky</NAME>
<INFO>I have used this resturant many times and find it truly Italian, with a varied menu, friendly staff and a lovely view of the Thames. In summer it's great to sit outside - for its location, the prices are very reasonable. </INFO>
</REVIEW>
<REVIEW DATE="6 December 2004">
<NAME> Michelle Owens</NAME>
<INFO>I’m sorry to say that my overall dining experience did not meet my expectations. The ambiance was lovely and the staff were very professional and helpful but the menu was very limited and I feel not enough was done to accommodate vegetarians. The options available were also unimaginative and predictable. To be honest, I felt a little let down.

The meat options were lovely, but I cannot help but feel that more could have been done to accommodate us - it is after all a restaurant with a kitchen and a chef. I would most likely not visit the establishment again and would have some reserve booking a table again, especially if I have vegetarian guests. A nice restaurant for meat lovers however.</INFO>
</REVIEW>
<ACCEPTEDCARDS>American Express, MasterCard, Meastro, Visa</ACCEPTEDCARDS>
<HOUSEREDPRICE> 13.50</HOUSEREDPRICE>
<HOUSEWHITEPRICE>13.50</HOUSEWHITEPRICE>
<SERVICECHARGE OPTIONAL="TRUE"> 12.5%</SERVICECHARGE>
</RESTAURANT>
</RESTAURANTGUIDE>




My xslt: =================================================

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="RESTAURANTGUIDE">

<html>
<head>
<style type="text/css">
body {
background-color : #993300}
</style>

<style type="text/css">
table {
color: yellow;
background-color: black }
</style>

<style type="text/css">
p:first-letter {
Font-size : 200%;
font-weight : bold;
vertical-align : middle;

}
</style>

<style type="text/css">
p {
background-color : yellow;
text-align: center;
text-size:large}
</style>

<style type="text/css">
ul {
list-style-type: none;
background-image: url(navi_bg.png);
height: 80px;
width: 663px;
margin: auto;
background-color :black;
}
</style>

<style type="text/css">
ul li{
float: left;
display : block}
</style>

<style type="text/css">
ul a {
background-image: url(navi_bg_divider.png);
background-repeat: no-repeat;
background-position: right;
padding-right: 32px;
padding-left: 32px;
display: block;
line-height: 80px;
text-decoration: none;
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 21px;
color:#999933 ;
}
</style>

<style type="text/css">
#tablehead {
text-align : center;
vertical-align: middle;
backround-color: #FFCC00;
border-color :yellow}


}
</style>

<title>Restaurant Guide</title>

</head>

<body>
<p> List Of Restaurants</p>
<h1> Sort By:</h1>
<ul>

<li><a href="seasonal.html"> Lunch &lt; 30</a></li>
<li><a href="italian.html"> Featured</a></li>
<li><a href="british.html"> Selected</a></li>
<li><a style="background-image :none;" href="full-list.html"> Seasonal </a></li>
</ul>
<ul>

<li><a href="seasonal.html"> Dinner &lt; 30</a></li>
<li><a href="italian.html"> Italian</a></li>
<li><a href="british.html"> Modern British</a></li>
<li><a style="background-image :none;" href="full-list.html"> Full List</a></li>
</ul>

<xsl:apply-templates/>

</body>
</html>
</xsl:template>

<xsl:template match="RESTAURANT[SEASONAL]">
<tr><td>
<xsl:value-of select="NAME"/>
</td>
<td>
<xsl:value-of select="ACCEPTEDCARDS"/>
</td>
<td>
<xsl:value-of select="ADDRESS"/>
</td>
</tr>

</xsl:template>






</xsl:stylesheet>

my DTD==================================
<?xml version="1.0" encoding="UTF-16LE"?>
<!ELEMENT RESTAURANTGUIDE (RESTAURANT*) >
<!ELEMENT RESTAURANT (NAME, INFO, ADDRESS, ( CUISINE| AVGLUNCH | AVGDINNER | CONTACT| SEASONAL| OPENINGTIMES|REVIEW|ACCEPTEDCARDS)*) >

<!ATTLIST RESTAURANTGUIDE
STATUS (FEATURED | SELECTED | NORMAL) "NORMAL"
BOOKONLINE (TRUE| FALSE) "FALSE"
CHILDRENWELCOME (TRUE| FALSE) "TRUE"
GROUPSALLOWED (TRUE| FALSE) "TRUE"
AIRCONDITIONING (TRUE| FALSE) "TRUE"
OUTSIDESEATING (TRUE| FALSE) "FALSE"
RESERVATIONS (TRUE| FALSE) "TRUE"
COVERCHARGE (TRUE| FALSE) "FALSE" >
<!ELEMENT NAME (#PCDATA) >
<!ELEMENT INFO (#PCDATA) >
<!ELEMENT ADDRESS (STREETNUMBER| STREETNAME|CITY|POSTCODE|COUNTRY)*>
<!ELEMENT STREETNUMBERORNAME (#PCDATA) >
<!ELEMENT STREETNAME (#PCDATA) >
<!ELEMENT CITY (#PCDATA) >
<!ELEMENT POSTCODE (#PCDATA) >
<!ELEMENT COUNTRY (#PCDATA) >
<!ELEMENT CUISINE (#PCDATA) >
<!ELEMENT AVGLUNCH (#PCDATA) >
<!ELEMENT AVGDINNER (#PCDATA) >
<!ELEMENT CONTACT (TEL| EMAIL| WEBSITE)* >
<!ELEMENT TEL (#PCDATA) >
<!ELEMENT EMAIL (#PCDATA) >
<!ELEMENT WEBSITE (#PCDATA) >
<!ELEMENT SEASONAL (#PCDATA) >
<!ELEMENT OPENINGTIMES (MON-THURS|MON-FRI| MON-SAT| FRI-SAT| SUN)* >
<!ELEMENT MON-THURS (#PCDATA) >
<!ELEMENT MON-FRI (#PCDATA) >
<!ELEMENT MON-SAT (#PCDATA) >
<!ELEMENT FRI-SAT (#PCDATA) >
<!ELEMENT SAT-SUN (#PCDATA) >
<!ELEMENT SAT (#PCDATA) >
<!ELEMENT SUN (#PCDATA) >

<!ELEMENT REVIEW (NAME, INFO) >
<!ATTLIST REVIEW
DATE CDATA #REQUIRED >
<!ELEMENT ACCEPTEDCARDS (#PCDATA) >
<!ELEMENT HOUSEREDPRICE (#PCDATA) >
<!ELEMENT HOUSEWHITEPRICE (#PCDATA) >
<!ELEMENT SERVICECHARGE (#PCDATA) >
<!ATTLIST SERVICECHARGE
OPTIONAL (TRUE|FALSE) "FALSE" >
 
Old December 21st, 2009, 09:43 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you simply do <xsl:apply-templates/> then that is a shorthand for <xsl:apply-templates select="node()"/> meaning all child nodes a processed. As there are built-in templates (see http://www.w3.org/TR/xslt#built-in-rule) that means that all RESTAURANT elements are processed, some with template you have written, the other with the built-in template for element nodes. So you need to ensure you only process the elements you are interested in, for instance with
Code:
<xsl:apply-templates select="RESTAURANT[SEASONAL]"/>
if that is what you want.
Also note that your template with match="RESTAURANT[SEASONAL]" outputs a table row without any template outputting a wrapping table element so you are not producing proper HTML with your stylesheet.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old December 21st, 2009, 09:52 AM
Registered User
 
Join Date: Dec 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank alot for the quick reply

now that bit of code looks like this:
Code:
<table border="4">
	<tr>
            <td><div id="tablehead"> Name</div></td>
            <td><div id="tablehead"> Accepted Cards</div></td>
            <td><div id="tablehead"> Address</div></td>
        </tr>
	<xsl:apply-templates select="RESTAURANT[SEASONAL]"/>									
				 </table>
I apologize if my questions seem a bit trivial but im new a tthis,

what i want to do is have more than one table each sorting the restaurants by different mean, but what im having trouble with now is adding static text, as if to say before this Seasonally sort table i want to add "list of restaurants by seasonal offer avaialabitlity" im trying to add using the XSL:element command but it cant be outside a template, and having it in the template forces it type it out with every match!, any help with that?

Thank you
 
Old December 21st, 2009, 10:01 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to have some text in the result tree then simply put it where you need it, the same way you have put other result nodes (like the 'table' element or the 'tr' or 'td' elements) in your template with match="/".
In HTML table elements can also have a 'caption' element so you could simply put that text in the table caption maybe.

Another (HTML related) problem in your posted code is the id="tablehead" you have used three times, that is not allowed, an id attribute value needs to be unique.
I think you simply want
Code:
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Accepted</th>
      <th>Address</th>
    </tr>
  </thead>
  <tbody>
    <xsl:apply-templates select="RESTAURANT[SEASONAL]"/>
  </tbody>
</table>
as the HTML language is rich enough to mark up table heads, no need for id attributes.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old December 21st, 2009, 11:05 AM
Registered User
 
Join Date: Dec 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks alot for all that it helped me loads, im sureley ill posting some question soon so see u then





Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06:36 AM
Performance for <xsl:import> and <xsl:include> vikkiefd XSLT 2 April 16th, 2008 08:06 AM
doubt in using <xsl:template> rajatake XSLT 5 December 15th, 2006 08:09 AM
Doubt in XPATH synax in <xsl:apply-templates > rajatake XSLT 3 May 4th, 2006 06:30 AM
<xsl:choose> and <xsl:otherwise> problem djmarquette XSLT 4 January 21st, 2005 01:56 PM





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