Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 11th, 2011, 11:37 AM
Registered User
 
Join Date: Mar 2011
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default XSLT Help

Hey,

I have a personal project that i though i would tackle with XSLT and XML as i have not used them before and thought it would be a good tool to have under my belt for the future.

Basically, i have two xml files:

structure.xml - used to store details about the site structure

products.xml - used to store product details

I want to create multiple web pages from a single stylesheet, so far i have the following:

structure.xml:

<departments>
<department continent="Europe" img="europe.jpg">
<sub-department country="United Kingdom" img="uk.jpg"/>
<sub-department country="Germany" img="germany.jpg"/>
<sub-department country="France" img="france.jpg"/>
<sub-department country="Netherlands" img="netherlands.jpg"/>
</department>
</departments>

products.xml:

<destinations>
<destination continent="Europe" country="United Kingdom" city="London">
<images img="london_main.jpg"/>
<description>Information about london</description>
<hotels>
<hotel name="London Hilton" rating="5">
<images img1="lh_main.jpg" img2="lh_lobby.jpg"/>
<description>Info about london hilton hotel</description>
<prices single="£30.00" double="£50.00" suite="£130.00"/>
</hotel>
</hotels>
</destination>

<destination continent="Europe" country="United Kingdom" city="Cardiff">
<images img="cardiff_main.jpg"/>
<description>Information about cardiff</description>
<hotels>
<hotel name="Cardiff Sleeperz" rating="3">
<images img1="cs_main.jpg" img2="cs_lobby.jpg"/>
<description>Info about cardiff sleepers hotel</description>
<prices single="£20.00" double="£40.00" suite="£100.00"/>
</hotel>
</hotels>
</destination>

<destination continent="Europe" country="United Kingdom" city="Belfast">
<images img="belfast_main.jpg"/>
<description>Information about belfast</description>
<hotels>
<hotel name="Belfast Hilton" rating="5">
<images img1="bh_main.jpg" img2="bh_lobby.jpg"/>
<description>Info about Belfast hilton hotel</description>
<prices single="£30.00" double="£50.00" suite="£130.00"/>
</hotel>
</hotels>
</destination>

<destination continent="Europe" country="United Kingdom" city="Glasgow">
<images img="glasgow_main.jpg"/>
<description>Information about glasgow</description>
<hotels>
<hotel name="Glasgow Grand" rating="5">
<images img1="gg_main.jpg" img2="gg_lobby.jpg"/>
<description>Info about glasgow hotel</description>
<prices single="£40.00" double="£70.00" suite="£150.00"/>
</hotel>
</hotels>
</destination>
</destinations>

structure.xsl (so far):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" indent="yes" name="html"/>
<xsl:variable name="departments" select="document(structure.xml)"/>
<xsl:variable name="destinations" select="document(products.xml)"/>
<xsl:template match="/">
<xsl:result-document href="index.html" format="html" indent="yes">
<html>
<body>
<table>
<tr>
<xsl:for-each select="//department">
<xsl:variable name="department" select="@continent" />
<xsl:variable name="image" select="concat('Images/',$department,'/',$department,'_main.jpg')"/>
<td>
<a href="{$department}.html"><img alt="{$department}" src="{$image}"/></a>
<br />
<p><xsl:value-of select="$department"/></p>
</td>
<xsl:result-document href="{$department}.html">
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr>
<xsl:for-each select="sub-department">
<td>
<xsl:variable name="sub-department" select="@country" />
<xsl:variable name="image" select="concat('Images/',$department,'/',$sub-department,'/',$sub-department,'_main.jpg')"/>
<xsl:result-document href="{$department}/{$sub-department}.html">
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr>
<xsl:variable name="test" select="document('destinations.xml')"/>
<xsl:for-each select="test/destination">
<td>
<p><xsl:value-of select="@city"/></p>
</td>
</xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:result-document>
<a href="{$department}/{$sub-department}.html"><img alt="{$sub-department}" src="{$image}"/></a>
<br />
<p><xsl:value-of select="$sub-department"/></p>
</td>

</xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:result-document>
</xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:result-document>
</xsl:template>

</xsl:stylesheet>

what im stuck on atm is opening/accessing the products.xml, and being able to populate pages with products.

any help would be amazing as this has been bugging me for weeks, and i cant seem to get my head around it!

cheers
Reply With Quote
  #2 (permalink)  
Old March 11th, 2011, 01:37 PM
Friend of Wrox
Points: 6,673, Level: 34
Points: 6,673, Level: 34 Points: 6,673, Level: 34 Points: 6,673, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,242
Thanks: 0
Thanked 244 Times in 243 Posts
Default

Consider to mark up your code samples with http://p2p.wrox.com/misc.php?do=bbcode#code to make them more readable. As for the problem, if you want help with XSLT then posting XML sample input documents is fine but you should then also show us the corresponding HTML result documents you want to create. Don't expect us to read your not working XSLT code to try to understand what output you want to achieve.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #3 (permalink)  
Old March 11th, 2011, 02:32 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

A couple of things that hopefully will point you in the correct direction:

First, I don't think having one xsl:result-document inside another one is a good idea. It probably works but it is probably error prone and hard to debug. What I would do is split each type of document out into a separate template. e.g.:

Code:
<xsl:template match="departments">
  <xsl:result-document href="index.html">
  <xsl:for-each select="department">
   ...
  </xsl:for-each>
 </xsl:result-document>

  <xsl:apply-templates select="department"/>
</xsl:template>

<xsl:template match="departments" mode="index">

</xsl:template>

<xsl:template match="department">
  <xsl:result-document href="{@continent}.html">
   ...
  </xsl:result-document>
  
  <xsl:apply-templates select="sub-department"/>
</xsl:template>

<xsl:template match="sub-department">
 <xsl:result-document href={../@continent}/{@country}.html">
  ...
  </xsl:result-document>
</xsl:templates>
As for loading the products, well you're doing that at the top, into the $destinations variable, but then you never use that variable.

I'm guessing you want to look up a 'product' you could add a key:

Code:
<xsl:key name="productKey" match="destination" use="@country"/>
Then, inside (i'm guessing) you sub-department template you'd do something like this:

Code:
<xsl:apply-templates select="$products/key('productKey', current()/@country" mode="product"/>
and then a template with a matching mode:

Code:
<xsl:template match="destination" mode="product">
  Description: <xsl:value-of select="description">
  <xsl:for-each select=".//hotel">
   ...
  </xsl:for-each>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
mrfletch (March 11th, 2011)
  #4 (permalink)  
Old March 11th, 2011, 04:46 PM
Registered User
 
Join Date: Mar 2011
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hey,

Sorry about the sloppy explanation, i was a bit frustrated with it!

basically, i want to read in the departments.xml file, and create a web page tree based on the content, which flows like this:

Home Page

Department Page Department Page Department Page Department Page

4 Sub-Department Pages per Department

Each of the sub department pages will include information taken out of the destinations.xml file.

The '<include_in>' tag within each destination states which department and sub-department that the destination is to be displayed in.


the input is as follows:

departments.xml:
Code:
<departments>
	<department name="Europe" img="europe.jpg">
		<sub-department id="Italy" img="italy.jpg"/>
		<sub-department id="France" img="france.jpg"/>
		<sub-department id="UK" img="uk.jpg"/>
		<sub-department id="Germany" img=".jpg"/>
	</department>
	
	<department name="Asia" img="asia.jpg">
		<sub-department id="China" img="china.jpg"/>
		<sub-department id="Turkey" img="turkey.jpg"/>
		<sub-department id="Japan" img="japan.jpg"/>
		<sub-department id="Special Offers" img="special_offers.jpg"/>
	</department>
	
	<department name="Africa" img="africa.jpg">
		<sub-department id="Egypt" img="egypt.jpg"/>
		<sub-department id="Kenya" img="kenya.jpg"/>
		<sub-department id="Madagascar" img="madagascar.jpg"/>
		<sub-department id="South Africa" img="south_africa.jpg"/>
	</department>

	<department name="Americas" img="americas.jpg">
		<sub-department id="Barbados" img="barbados.jpg"/>
		<sub-department id="Cuba" img="cuba.jpg"/>
		<sub-department id="Jamaica" img="jamaica.jpg"/>
		<sub-department id="Saint Lucia" img="saint_lucia.jpg"/>
	</department>
	
	<department name="special offers" img="special offers.jpg">
		<sub-department id="Americas" img="americas.jpg"/>
		<sub-department id="europe" img="europe.jpg"/>
		<sub-department id="africa" img="africa.jpg"/>
		<sub-department id="asia" img="asia.jpg"/>
	</department>
</departments>
destinations.xml (not yet complete, just need to add more destinations):

Code:
<destinations>
	<destination continent="europe" country="italy" city="Rome">
		<images img1="rome.jpg" />
		<description>Information about rome</description>
		<hotels>
			<hotel name="Rome Grand">
				<images img1="rome_grand.jpg" img2="rg_pool.jpg" img3="rg_lobby.jpg" img4="rg_suite.jpg" />
				<description>Info about rome grand hotel</description>
				<price single="30.00" double="50.00" suit="100.00" />
			</hotel>
		</hotels>
		<include_in department="europe" sub-department="italy"/>
		<include_in department="special offers" sub-department="europe"/>
	</destination>

	<destination continent="europe" country="italy" city="Venice">
		<images img1="venice.jpg" />
		<description>Information about Venice</description>
		<hotels>
			<hotel name="Venice Royal">
				<images img1="venice_royal.jpg" img2="vr_pool.jpg" img3="vr_lobby.jpg" />
				<description>Info about venice royal hotel</description>
				<price single="50.00" double="70.00" />
			</hotel>
		</hotels>
	</destination>
	
	<destination continent="europe" country="france" city="Paris">
		<images img1="paris.jpg" />
		<description>Information about paris</description>
		<hotels>
			<hotel name="Paris Plaza">
				<images img1="paris_plaza.jpg" img2="pp_pool.jpg" />
				<description>Info about paris plaza hotel</description>
				<price single="90.00" double="140.00" suit="210.00" />
			</hotel>
		</hotels>
	</destination>
	
	<destination continent="europe" country="france" city="Nice">
		<images img1="nice.jpg" />
		<description>Information about nice</description>
		<hotels>
			<hotel name="Le' Nice">
				<images img1="le_nice.jpg" img2="ln_pool.jpg" img3="ln_lobby.jpg" />
				<description>Info about le' nice hotel</description>
				<price single="40.00" double="60.00" suit="90.00" />
			</hotel>
		</hotels>
		<include_in department="europe" sub-department="france"/>
		<include_in department="special offers" sub-department="europe"/>
	</destination>
	
	<destination continent="europe" country="uk" city="London">
		<images img1="london.jpg" />
		<description>Information about london</description>
		<hotels>
			<hotel name="London Hilton">
				<images img1="london_hilton.jpg" img2="lh_single.jpg" img3="lh_double.jpg" img4="lh_suite.jpg" img5="lh_lobby.jpg" />
				<description>Info about london hilton hotel</description>
				<price single="80.00" double="140.00" suit="250.00" />
			</hotel>
		</hotels>
	</destination>
	
	<destination continent="europe" country="uk" city="Cardiff">
		<images img1="cardiff.jpg" />
		<description>Information about cardiff</description>
		<hotels>
			<hotel name="Cardiff Sleepers">
				<images img1="cs_main.jpg" img2="cs_lobby.jpg" img3="cs_bar.jpg" />
				<description>Info about cardiff sleepers hotel</description>
				<price single="17.00" double="35.00" />
			</hotel>
		</hotels>
	</destination>
	
	<destination continent="europe" country="germany" city="Berlin">
		<images img1="berlin.jpg" />
		<description>Information about berlin</description>
		<hotels>
			<hotel name="Wise Place Hotel">
				<images img1="wise_place.jpg" img2="wp_lobby.jpg" img3="wp_bar.jpg" img4="wp_restaurant.jpg" />
				<description>Info about wise place hotel</description>
				<price single="30.00" double="50.00" />
			</hotel>
		</hotels>
		<include_in department="europe" sub-department="germany"/>
		<include_in department="special offers" sub-department="europe"/>
	</destination>
</destinations>
Basically, the output i'm looking for is as follows:

Index.html:

Code:
<html>
	<head>
		<title>Index Page</title>
	</head>
	<body>
		<h1>Welcome</h1>
		<hr />
		<table>
			<tbody>
				<tr>
					<td>Hyperlink to 1st department with image</td>
				</tr>
				<tr>
					<td>Hyperlink to 2nd department with image</td>
				</tr>
				<tr>
					<td>Hyperlink to 3rd department with image</td>
				</tr>
				<tr>
					<td>Hyperlink to 4th department with image</td>
				</tr>
			</tbody>
		</table>
	</body>
</html>
Each department page:

Code:
<html>
	<head>
		<title>department name</title>
	</head>
	<body>
		<h1>department name</h1>
		<hr />
		<table>
			<tbody>
				<tr>
					<td>Hyperlink to 1st sub-department with image</td>
				</tr>
				<tr>
					<td>Hyperlink to 2nd sub-department with image</td>
				</tr>
				<tr>
					<td>Hyperlink to 3rd sub-department with image</td>
				</tr>
				<tr>
					<td>Hyperlink to 4th sub-department with image</td>
				</tr>
			</tbody>
		</table>
	</body>
</html>
Each sub-department page:

Code:
<html>
	<head>
		<title>sub-department name</title>
	</head>
	<body>
		<h1>sub-department name</h1>
		<hr />
		<!-- for each product within the department -->
		<h2>city name</h2>
		<!-- for each image in the <images> node within products.xml) side by side -->
		<img alt="city name" src="each image"></img>
		<br />
		<h2>description</h2>
		<p>description of each city</p>
		<br />
		<h2>Hotels</h2>
		<!-- for each hotel within a product -->
		<!-- for each image in the <images> node) side by side -->
		<img alt="hotel name" src="each image"></img>
		<br />
		<h2>description</h2>
		<p>description of each hotel</p>
		<br />
		<hr />
	</body>
</html>
I hope that this is a better explanation than my previous one, and any help with this would be amazing.

Thanks Sam for those pointers, i'm still trying to figure out how it all goes together, its a bit strange as i can usually pick up programming quite quickly, but this has me stumped!

P.S

Sorry for not reading the post requirement info! should of looked for something like that!

cheers

martin
Reply With Quote
  #5 (permalink)  
Old March 11th, 2011, 07:02 PM
Registered User
 
Join Date: Mar 2011
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default Update

I have changed some of the design of the input xml files, and have got to a certain point with the xslt.

The problem i now have is getting the path correct for the file structure, as it is created as ..//sub-department.html rather than ../department/sub-department. The problem is an undeclaired variable, but i have declaired it :S.

Code:
	<xsl:template match="sub-department">
		<xsl:result-document href="site1/{$department}/{@id}.html">
			<html>
				<head>
					<title><xsl:value-of select="@id"/></title>
				</head>
				<body>
						<h1><xsl:value-of select="@id"/></h1>
						<hr />						
						<xsl:apply-templates select="$destinations/key('destination_key', current()/include_in/@sub-department)" mode="destination"/>
				</body>
			</html>			
		</xsl:result-document>
also, i have a problem in getting the data from the destinations.xml file, i have used a key as suggested, but for some reason, cannot et it to work, after looking around on the internet i have come up with this:

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
	<xsl:output method="html" indent="yes" name="html"/>

	<xsl:variable name="departments" select="document(departments.xml)"/>
	<xsl:variable name="destinations" select="document(destinations.xml)"/>

	<xsl:key name="destination_key" match="destination/include_in" use="@sub-department"/>	
	
	<xsl:template match="departments">
		<xsl:result-document href="site1/index.html">
			<html>
				<head>
					<title>Index Page</title>
				</head>
				<body>
					<h1>Welcome</h1>
					<hr />
					<h2>Continents</h2>
					<table>
						<tbody>
							<tr>
								<xsl:for-each select="//department">
									<xsl:variable name="department" select="@name" />
									<xsl:variable name="image" select="concat('Images/',$department,'/',$department,'_main.jpg')"/>
									<td>
										<a href="{$department}.html"><img alt="{$department}" src="{$image}"/></a>
									</td>
								</xsl:for-each>
							</tr>
						</tbody>
					</table>
				</body>
			</html>
		</xsl:result-document>
		<xsl:apply-templates select="department"/>
	</xsl:template>
	
	<xsl:template match="department">
		<xsl:result-document href="site1/{@name}.html">
			<html>
				<head>
					<title><xsl:value-of select="@name"/></title>
				</head>
				<body>
					<h1><xsl:value-of select="@name"/></h1>
					<hr />
					<table>
						<tbody>
							<tr>
								<xsl:for-each select="sub-department">
									<xsl:variable name="sub-department" select="@id" />
									<xsl:variable name="img_name" select="@img"/>
									<xsl:variable name="image" select="concat('Images/',$sub-department,'/',$img_name)"/>
									<td>
										<a href="{$sub-department}.html"><img alt="{$sub-department}" src="{$image}"/></a>
										<p><xsl:value-of select="$sub-department"/></p>
									</td>
								</xsl:for-each>	
							</tr>
						</tbody>
					</table>
				</body>
			</html>
		</xsl:result-document>
		<xsl:apply-templates select="sub-department"/>
	</xsl:template>

	<xsl:template match="sub-department">
		<xsl:result-document href="site1/{$department}/{@id}.html">
			<html>
				<head>
					<title><xsl:value-of select="@id"/></title>
				</head>
				<body>
						<h1><xsl:value-of select="@id"/></h1>
						<hr />						
						<xsl:apply-templates select="$destinations/key('destination_key', current()/include_in/@sub-department)" mode="destination"/>
				</body>
			</html>			
		</xsl:result-document>
		
	</xsl:template>

	<xsl:template match="destination" mode="destination">
		<h2>{@city}</h2>
		<xsl:for-each select="images/image">
			<xsl:variable name="img" select="image"/>
			<img alt="{@city}" src="Images/{@name}/{$img}"></img>
		</xsl:for-each>		
		<br />
		<h2>Description</h2>
		<p><xsl:value-of select="description"/></p>
		<br />
		<h2>Hotels</h2>
		<xsl:for-each select=".//hotel">
			<h2>@name</h2>
			<xsl:variable name="img" select="image"/>
			<xsl:for-each select="images/image">
				<img alt="{@name}" src="Images/{@name}/{$img}"></img>
			</xsl:for-each>
			<br />
			<h2>Description</h2>
			<p><xsl:value-of select="description"/></p>
			<br />
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
any help would be amazing

cheers

martin
Reply With Quote
  #6 (permalink)  
Old March 11th, 2011, 07:05 PM
Registered User
 
Join Date: Mar 2011
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default

forgot to post the xml files!

departments.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<departments>
	<department name="Europe" img="europe.jpg">
		<sub-department id="Italy" img="italy.jpg"/>
		<sub-department id="France" img="france.jpg"/>
		<sub-department id="UK" img="uk.jpg"/>
		<sub-department id="Germany" img=".jpg"/>
	</department>
	
	<department name="Asia" img="asia.jpg">
		<sub-department id="China" img="china.jpg"/>
		<sub-department id="Turkey" img="turkey.jpg"/>
		<sub-department id="Japan" img="japan.jpg"/>
		<sub-department id="Special Offers" img="special_offers.jpg"/>
	</department>
	
	<department name="Africa" img="africa.jpg">
		<sub-department id="Egypt" img="egypt.jpg"/>
		<sub-department id="Kenya" img="kenya.jpg"/>
		<sub-department id="Madagascar" img="madagascar.jpg"/>
		<sub-department id="South Africa" img="south_africa.jpg"/>
	</department>

	<department name="Americas" img="americas.jpg">
		<sub-department id="Barbados" img="barbados.jpg"/>
		<sub-department id="Cuba" img="cuba.jpg"/>
		<sub-department id="Jamaica" img="jamaica.jpg"/>
		<sub-department id="Saint Lucia" img="saint_lucia.jpg"/>
	</department>
	
	<department name="special offers" img="special offers.jpg">
		<sub-department id="Americas" img="americas.jpg"/>
		<sub-department id="europe" img="europe.jpg"/>
		<sub-department id="africa" img="africa.jpg"/>
		<sub-department id="asia" img="asia.jpg"/>
	</department>
</departments>
destinations.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<destinations>
	<destination continent="europe" country="italy" city="Rome">
		<images>
			<image>rome.jpg"</image>
		</images>
		<description>Information about rome</description>
		<hotels>
			<hotel name="Rome Grand">
				<images>
					<image>rome_grand.jpg</image>
					<image>rg_pool.jpg</image>
					<image>rg_lobby.jpg</image>
					<image>rg_suite.jpg</image>
				</images>
				<description>Info about rome grand hotel</description>
				<price single="30.00" double="50.00" suit="100.00" />
			</hotel>
		</hotels>
		<include_in department="europe" sub-department="italy"/>
		<include_in department="special offers" sub-department="europe"/>
	</destination>

	<destination continent="europe" country="italy" city="Venice">
		<images>
			<image>rome.jpg"</image>
		</images>
		<description>Information about Venice</description>
		<hotels>
			<hotel name="Venice Royal">
				<images>
					<image>rome_grand.jpg</image>
					<image>rg_pool.jpg</image>
					<image>rg_lobby.jpg</image>
					<image>rg_suite.jpg</image>
				</images>
				<description>Info about venice royal hotel</description>
				<price single="50.00" double="70.00" />
			</hotel>
		</hotels>
	</destination>
	
	<destination continent="europe" country="france" city="Paris">
		<images>
			<image>rome.jpg"</image>
		</images>
		<description>Information about paris</description>
		<hotels>
			<hotel name="Paris Plaza">
				<images>
					<image>rome_grand.jpg</image>
					<image>rg_pool.jpg</image>
					<image>rg_lobby.jpg</image>
					<image>rg_suite.jpg</image>
				</images>
				<description>Info about paris plaza hotel</description>
				<price single="90.00" double="140.00" suit="210.00" />
			</hotel>
		</hotels>
	</destination>
	
	<destination continent="europe" country="france" city="Nice">
		<images>
			<image>rome.jpg"</image>
		</images>
		<description>Information about nice</description>
		<hotels>
			<hotel name="Le' Nice">
				<images>
					<image>rome_grand.jpg</image>
					<image>rg_pool.jpg</image>
					<image>rg_lobby.jpg</image>
					<image>rg_suite.jpg</image>
				</images>
				<description>Info about le' nice hotel</description>
				<price single="40.00" double="60.00" suit="90.00" />
			</hotel>
		</hotels>
		<include_in department="europe" sub-department="france"/>
		<include_in department="special offers" sub-department="europe"/>
	</destination>
	
	<destination continent="europe" country="uk" city="London">
		<images>
			<image>rome.jpg"</image>
		</images>
		<description>Information about london</description>
		<hotels>
			<hotel name="London Hilton">
				<images>
					<image>rome_grand.jpg</image>
					<image>rg_pool.jpg</image>
					<image>rg_lobby.jpg</image>
					<image>rg_suite.jpg</image>
				</images>
				<description>Info about london hilton hotel</description>
				<price single="80.00" double="140.00" suit="250.00" />
			</hotel>
		</hotels>
	</destination>
	
	<destination continent="europe" country="uk" city="Cardiff">
		<images>
			<image>rome.jpg"</image>
		</images>
		<description>Information about cardiff</description>
		<hotels>
			<hotel name="Cardiff Sleepers">
				<images>
					<image>rome_grand.jpg</image>
					<image>rg_pool.jpg</image>
					<image>rg_lobby.jpg</image>
					<image>rg_suite.jpg</image>
				</images>
				<description>Info about cardiff sleepers hotel</description>
				<price single="17.00" double="35.00" />
			</hotel>
		</hotels>
	</destination>
	
	<destination continent="europe" country="germany" city="Berlin">
		<images>
			<image>rome.jpg"</image>
		</images>
		<description>Information about berlin</description>
		<hotels>
			<hotel name="Wise Place Hotel">
				<images>
					<image>rome_grand.jpg</image>
					<image>rg_pool.jpg</image>
					<image>rg_lobby.jpg</image>
					<image>rg_suite.jpg</image>
				</images>
				<description>Info about wise place hotel</description>
				<price single="30.00" double="50.00" />
			</hotel>
		</hotels>
		<include_in department="europe" sub-department="germany"/>
		<include_in department="special offers" sub-department="europe"/>
	</destination>
</destinations>
Reply With Quote
  #7 (permalink)  
Old March 12th, 2011, 05:08 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well $department isn't in scope in the sub-department template, so you'd either need to declare it again, or just use "../@name" to get the parent department's name attribute.

A couple of changes to get the key working correctly.

Firstly, the $destinations variable should be the following (note the single quotes around the filename):

Code:
 <xsl:variable name="destinations" select="document('destinations.xml')"/>
And when calling the key there are a few things to take into account. Firstly you are passing in the local element that you want to find in the remote element. So you should be passing in sub-department/@id. Secondly the context once you've found your element is the inner element found by your key, in this case the <include_in> so you need to go back up one level to get the departments. Finally, searches are case sensitive, so I'd wrap both ends in lower-case() function:

Code:
<xsl:key name="destination_key" match="include_in" use="lower-case(@sub-department)"/>
and then when using it:

Code:
<xsl:apply-templates select="$destinations/key('destination_key', lower-case(current()/@id))/.." mode="destination"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
mrfletch (March 12th, 2011)
  #8 (permalink)  
Old March 12th, 2011, 10:43 AM
Registered User
 
Join Date: Mar 2011
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hey,

Sorry to sound a bit dumb, as it might be something simple, but could you explain how the following code works, as i cannot seem to get it to work :S

you said to use this to select each of the destinations, but it doesnt seem to select them?

Code:
<xsl:apply-templates select="$destinations/key('destination_key', lower-case(current()/@id))/.." mode="destination"/>
cheers

martin
Reply With Quote
  #9 (permalink)  
Old March 12th, 2011, 01:37 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The first part runs the key() function in the context of the loaded document.

The key function itself looks for all <include_in> elements (taken from the 'match' attribute of the key) whose @sub-department attribute (taken from the 'use' attribute of the key declaration) match the second parameter, which is the current @id attribute.

Once you have the <include_in> element you then want the parent <destination> element, so appending "/.." will take you to the parent element.

I got this to work with the changes detailed above, so you've either missed a step, or there is some problem in your XSLT that I can't see from here.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Another question about generating XSLT with XSLT danblick XSLT 2 July 16th, 2009 08:40 PM
General XSLT Questions in the XSLT Forum jminatel BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 0 March 31st, 2008 07:50 PM
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
xslt with an xslt outputfile alleycat XSLT 4 February 20th, 2006 09:56 AM



All times are GMT -4. The time now is 10:18 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.