Link to home
Start Free TrialLog in
Avatar of kipl20
kipl20

asked on

xml and html links

hi,

i have this code below in a xslt sheet which transforms from my 2 xml files, also included.

what i would like to do is make links from the url element in the struct.xml and use them as links for navigation around the created pages. so the created pages at the moment have a table filled with data from products.xml. i would like navigation on the left hand side of the page just as simple hyperlinks to each of the created pages, so for example 1 link would be laptops.html

any ideas? thanks in advance
XSL sheet
 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="navigation">
        <xsl:apply-templates select="menu"/>
    </xsl:template>
    
    <xsl:template match="menu">
        <xsl:result-document href="{id}.html">
            <html>
                <head/>
                <body/>
            </html>
        </xsl:result-document>
        <xsl:apply-templates select="menu|submenu"/>
    </xsl:template>
    
    <xsl:template match="submenu">
        <xsl:result-document href="{id}.html">
            <html>
                <head/>
                <body>
                    <xsl:if test="id = 'laptops'">
                        <xsl:apply-templates select="document('products.xml')//products" mode="products"></xsl:apply-templates>
                    </xsl:if>
                     <xsl:if test="id = 'comp accesories'">
                        <xsl:apply-templates select="document('products.xml')//products" mode="products1"></xsl:apply-templates>
                    </xsl:if>
                </body>
            </html>
        </xsl:result-document>
        <xsl:apply-templates select="menu|submenu"/>
    </xsl:template>
    
    <xsl:template match="products" mode="products">
                <h2>Computing - Laptops</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th align="left">Name</th>
                        <th align="left">Price</th>
                        <th align="left">Description</th>
                        <th align="left">image</th>
                    </tr>
                    <xsl:for-each select="Computing/Laptops">
                        <tr>
                            <td><xsl:value-of select="name"/></td>
                            <td><xsl:value-of select="price"/></td>
                            <td><xsl:value-of select="description"/></td>
                            <td><xsl:value-of select="img" disable-output-escaping="yes"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
    </xsl:template>
    
    <xsl:template match="products" mode="products1">
                <h2>Computing</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th align="left">Name</th>
                        <th align="left">Price</th>
                        <th align="left">Desc</th>
                    </tr>
                    <xsl:for-each select="Computing/Accesories">
                        <tr>
                            <td><xsl:value-of select="name"/></td>
                            <td><xsl:value-of select="price"/></td>
                            <td><xsl:value-of select="description"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
    </xsl:template>
 
</xsl:stylesheet>
 
Struct.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="lappy.xslt"?>
<navigation>
		<menu>
			<id>home</id>
			<title>Home Page</title>
				<menu>
					<id>homeproducts</id>
					<title>Home Products</title>
					<url>homeproducts.html</url>
					<submenu>
						<id>homefurniture</id>
						<title>Home Furniture</title>
						<url>homefurniture.html</url>
					<submenu>
						<id>homeelectrical</id>
						<title>Home Electrical</title>
						<url>homeelectrical.html</url>
					<submenu>
						<id>homeentertainment</id>
						<title>Home Entertainment</title>
						<url>homeentertainment.html</url>
					<submenu>
						<id>homegym</id>
						<title>Home Gym</title>
						<url>homegym.html</url>
				<menu>
					<id>gardenproducts</id>
					<title>Garden Products</title>
					<url>gardenproducts.html</url>
					<submenu>
						<id>gardenfurniture</id>
						<title>Garden Furniture</title>
						<url>gardenfurniture.html</url>
					<submenu>
						<id>gardenelectrical</id>
						<title>Garden Electrical</title>
						<url>gardenelectrical.html</url>
					<submenu>
						<id>gardenentertainment</id>
						<title>Garden Entertainment</title>
						<url>gardenentertainment.html</url>
					<submenu>
						<id>gardentools</id>
						<title>Garden Tools</title>
						<url>gardentools.html</url>
				<menu>
					<id>gymproducts</id>
					<title>Gym Products</title>
					<url>gymproducts.html</url>
					<submenu>
						<id>weights</id>
						<title>Weights</title>
						<url>gymweights.html</url>
					<submenu>
						<id>treadmills</id>
						<title>Treadmills</title>
						<url>gymtreadmills.html</url>
					<submenu>
						<id>exercisebikes</id>
						<title>Exercise Bikes</title>
						<url>gymexercisebikes.html</url>
					<submenu>
						<id>accesories</id>
						<title>Gym accesories</title>
						<url>gymaccesories.html</url>
				<menu>
					<id>computerproducts</id>
					<title>Computer Products</title>
					<url>compproducts.html</url>
					<submenu>
						<id>pcs</id>
						<title>PCs</title>
						<url>comppcs.html</url>
					<submenu>
						<id>laptops</id>
						<title>Laptops</title>
						<url>complaptops.html</url>
					<submenu>
						<id>storage</id>
						<title>computerStorage</title>
						<url>compstorage.html</url>
					<submenu>
						<id>compaccesories</id>
						<title>Comp Accesories</title>
						<url>compaccesories.html</url>		
					</submenu>
					</submenu>
					</submenu>
					</submenu>
					</menu>
					</submenu>
					</submenu>
					</submenu>	
					</submenu>
					</menu>
					</submenu>
					</submenu>	
					</submenu>
					</submenu>
					</menu>
					</submenu>		
				</submenu>
				</submenu>
				</submenu>
				</menu>
			</menu>
	</navigation>
 
Products.xml
 
<products>
<Computing>
<Laptops>
<name>Dell xps</name>
<price>£300</price>
<description>great lightweight and powerfull laptop</description>
<img> &lt;img src="untitled.bmp"&gt; </img>
</Laptops>
<Laptops>
<name>Macbook</name>
<price>£700</price>
<description>What a laptop, brilliant!!</description>
<img> &lt;img src="macbook.jpg"&gt; </img>
</Laptops>
<Accesories>
<name>Mouse</name>
<price>£10</price>
<description>great lightweight and powerfull mouse</description>
</Accesories>
<Accesories>
<name>Keyboard</name>
<price>£15</price>
<description>great lightweight and powerfull keyboard</description>
</Accesories>
<Storage>
<name>USB flash drive 8GB</name>
<price>£15</price>
<description>great lightweight and powerfull keyboard</description>
</Storage>
<Storage>
<name>£300GB sata Hdd</name>
<price>£150</price>
<description>great lightweight and powerfull keyboard</description>
</Storage>
<PC>
<name>Dell desktop</name>
<price>£300</price>
<description>great lightweight and powerfull keyboard</description>
</PC>
<PC>
<name>iMac</name>
<price>£700</price>
<description>great lightweight and powerfull keyboard</description>
</PC>
</Computing>
<products>

Open in new window

Avatar of abel
abel
Flag of Netherlands image

Can you also show the output that you have now and the output that you wish to have? That will make it clear what exactly you mean with the position of the links.
Ah, I see now that you use XSLT 2.0 to multiple result document. You can use your default output document to produce the links something like as follows. You probably want to wrap it into some HTML, which should go in the main matching template.


    <xsl:template match="navigation">
        <xsl:apply-templates select="menu"/>
        <xsl:apply-templates select="menu" mode="url-list" />
    </xsl:template>
 
     <xsl:template match="menu|sub-menu" mode="url-list">
          <a href="{id}.html"><xsl:value-of select="id" /></a><br />
          <xsl:apply-templates select="menu | sub-menu" />
     </xsl:template>

Open in new window

Avatar of kipl20
kipl20

ASKER

yeah this is the output of the laptops.html at the moment

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <head/>
      <body>
            <h2>Computing - Laptops</h2>
            <table border="1">
                  <tr bgcolor="#9acd32">
                        <th align="left">Name</th>
                        <th align="left">Price</th>
                        <th align="left">Description</th>
                        <th align="left">image</th>
                  </tr>
                  <tr>
                        <td>Dell xps</td>
                        <td>£300</td>
                        <td>great lightweight and powerfull laptop</td>
                        <td> <img src="untitled.bmp"> </td>
                  </tr>
                  <tr>
                        <td>Macbook</td>
                        <td>£700</td>
                        <td>What a laptop, brilliant!!</td>
                        <td> <img src="macbook.jpg"> </td>
                  </tr>
            </table>
      </body>
</html>

and how i want it to be, similar to this but with the links down the left hand side like a menu and the table in the middle of the page

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <head/>
      <body>
      <br><a href="laptops.html">Laptops</a></br>
      <br><a href="test.html">Link One</a></br>
    <br><a href="test.html">Link Two</a></br>
    <br><a href="test.html">Link Three</a></br>
   <br><a href="test.html">Link Four</a></br>
    <br><a href="test.html">Link Five</a></br>
            <h2 align="center">Computing - Laptops</h2>
            <table border="1" align="center">
                  <tr bgcolor="#9acd32">
                        <th align="left">Name</th>
                        <th align="left">Price</th>
                        <th align="left">Description</th>
                        <th align="left">image</th>
                  </tr>
                  <tr>
                        <td>Dell xps</td>
                        <td>£300</td>
                        <td>great lightweight and powerfull laptop</td>
                        <td> <img src="untitled.bmp"> </td>
                  </tr>
                  <tr>
                        <td>Macbook</td>
                        <td>£700</td>
                        <td>What a laptop, brilliant!!</td>
                        <td> <img src="macbook.jpg"> </td>
                  </tr>
            </table>
      </body>
</html>
Avatar of kipl20

ASKER

hi abel,
have tried this code but it comes up with and error in xmlspy

File C:\Documents and Settings\Administrator\My Documents\XML\lappy.xslt: XSL transformation failed
      A result document with this href has already been implicitly or explicitly created

what i would like to do is basically kind of loop through all the urls and add them as links to each page created
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 
    <xsl:template match="navigation">
        <xsl:apply-templates select="menu"/>
          <xsl:apply-templates select="menu|sub-menu" mode="url-list" />
    </xsl:template>
    
       <xsl:template match="menu|sub-menu" mode="url-list">
          <a href="{id}.html"><xsl:value-of select="id" /></a><br />
          <xsl:apply-templates select="menu | sub-menu" />
     </xsl:template>
     
    <xsl:template match="menu">
        <xsl:result-document href="{id}.html">
            <html>
                <head/>
                <body/>
            </html>
        </xsl:result-document>
        <xsl:apply-templates select="menu|submenu"/>
    </xsl:template>
    
    <xsl:template match="submenu">
        <xsl:result-document href="{id}.html">
            <html>
                <head/>
                <body>
                    <xsl:if test="id = 'laptops'">
                        <xsl:apply-templates select="document('products.xml')//products" mode="products"></xsl:apply-templates>
                    </xsl:if>
                     <xsl:if test="id = 'comp accesories'">
                        <xsl:apply-templates select="document('products.xml')//products" mode="products1"></xsl:apply-templates>
                    </xsl:if>
                </body>
            </html>
        </xsl:result-document>
        <xsl:apply-templates select="menu|submenu"/>
    </xsl:template>
    
    <xsl:template match="products" mode="products">
                <h2>Computing - Laptops</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th align="left">Name</th>
                        <th align="left">Price</th>
                        <th align="left">Description</th>
                        <th align="left">image</th>
                    </tr>
                    <xsl:for-each select="Computing/Laptops">
                        <tr>
                            <td><xsl:value-of select="name"/></td>
                            <td><xsl:value-of select="price"/></td>
                            <td><xsl:value-of select="description"/></td>
                            <td><xsl:value-of select="img" disable-output-escaping="yes"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
    </xsl:template>
    
    <xsl:template match="products" mode="products1">
                <h2>Computing</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th align="left">Name</th>
                        <th align="left">Price</th>
                        <th align="left">Desc</th>
                    </tr>
                    <xsl:for-each select="Computing/Accesories">
                        <tr>
                            <td><xsl:value-of select="name"/></td>
                            <td><xsl:value-of select="price"/></td>
                            <td><xsl:value-of select="description"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
    </xsl:template>
 
</xsl:stylesheet>

Open in new window

If you get the message you say you are getting, then two or more ids share the same value. Did the original code you showed me, work well? Because the change I suggested didn't add anything to the chain of output documents, it merely changed the default output.
Avatar of kipl20

ASKER

hi yes it worked fine, i think i must be doing soomething wrong.
what did you mean by this "You probably want to wrap it into some HTML, which should go in the main matching template."

where would i put this? sorry for all the questions i am new to this

thanks


Ah, apologies, my mistake. I forgot to put the mode in the last xsl:apply-templates, which is why it went into the wrong matching templates.

You will have to tweak my code a bit though. In the template "navigation" you can place any HTML code that you want wrapped around that list of href-links.

    <xsl:template match="navigation">
        <xsl:apply-templates select="menu"/>
          <xsl:apply-templates select="menu|sub-menu" mode="url-list" />
    </xsl:template>
    
       <xsl:template match="menu|sub-menu" mode="url-list">
          <a href="{id}.html"><xsl:value-of select="id" /></a><br />
          <xsl:apply-templates select="menu | sub-menu" mode="url-list" />
     </xsl:template>

Open in new window

Avatar of kipl20

ASKER

hi thanks for the help abel i really appriciate it,

im totally confused on where is should be putting your code, when i put it in i come up with that error all the time.

where shall i put the code?

thanks

I see now that I misunderstood your problem. You do not want one extra document with all the links, but in every document you want all the links, repeated (understandable). Hold on, I'll try to come up with something.
Avatar of kipl20

ASKER

thanks abel i appriciate it alot
Avatar of kipl20

ASKER

hi have done it this way

<xsl:template match="submenu">
        <xsl:result-document href="{id}.html">
            <html>
                <head/>
              <table align="left">
              <tr>
             <td>
                        <h1>Menu</h1>
                <a href="home.html">Home Page</a><br/>
                        Home Products
                        <li/><a href="homefurniture.html">Home Furniture</a><br/>
                        <li/><a href="homeelectrical.html">Home Electrical</a><br/>
                        <li/><a href="homeentertainment.html">Home Entertainment</a><br/>
                        <li/><a href="homegym.html">Home Gym</a><br/>
                        Garden Products
                        <li/><a href="gardenfurniture.html">Garden Furniture</a><br/>
                        <li/><a href="gardenelectrical.html">Garden Electrical</a><br/>
                        <li/><a href="gardenentertainment.html">Garden Entertainment</a><br/>
                        <li/><a href="gardentools.html">Garden Tools</a><br/>
                        Gym Products
                        <li/><a href="gymweights.html">Weights</a><br/>
                        <li/><a href="gymtreadmills.html">Treadmills</a><br/>
                        <li/><a href="gymexercisebikes.html">Exercise Bikes</a><br/>
                        <li/><a href="gymaccesories.html">Gym accesories</a><br/>
                        <!--<a href="products.html">Computer Products</a><br/>-->
                        Computer Products
                        <li/><a href="pcs.html">PCs</a><br/>
                        <li/><a href="laptops.html">Laptops</a><br/>
                        <li/><a href="storage.html">computerStorage</a><br/>
                        <li/><a href="compaccesories.html">Comp Accesories</a><br/>
                        </td>
                        </tr>
                        </table>
                <body>
                 <xsl:if test="id = 'home'">
                        <xsl:apply-templates select="document('products.xml')//products" mode="home"></xsl:apply-templates>
                        gbcgb
                    </xsl:if>
                    <xsl:if test="id = 'laptops'">
                        <xsl:apply-templates select="document('products.xml')//products" mode="products"></xsl:apply-templates>
                    </xsl:if>
                     <xsl:if test="id = 'compaccesories'">
                        <xsl:apply-templates select="document('products.xml')//products" mode="products1"></xsl:apply-templates>
                    </xsl:if>
                </body>
            </html>
        </xsl:result-document>
        <xsl:apply-templates select="menu|submenu"/>
    </xsl:template>

but as you can see they are all hard coded and im not entirly sure whether you are supposed to combine html and xml like that?  i have put the output in the code snippit just to see how i would like the page to look. i have a table aligned on the left that contains the links then the data is displyed on a table on the cneter of the screen
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<head/>
	<table align="left">
		<tr>
			<td><h1>Menu</h1><a href="home.html">Home Page</a><br/>
				Home Products
				<li/><a href="homefurniture.html">Home Furniture</a><br/><li/><a href="homeelectrical.html">Home Electrical</a><br/><li/><a href="homeentertainment.html">Home Entertainment</a><br/><li/><a href="homegym.html">Home Gym</a><br/>
				Garden Products
				<li/><a href="gardenfurniture.html">Garden Furniture</a><br/><li/><a href="gardenelectrical.html">Garden Electrical</a><br/><li/><a href="gardenentertainment.html">Garden Entertainment</a><br/><li/><a href="gardentools.html">Garden Tools</a><br/>
				Gym Products
				<li/><a href="gymweights.html">Weights</a><br/><li/><a href="gymtreadmills.html">Treadmills</a><br/><li/><a href="gymexercisebikes.html">Exercise Bikes</a><br/><li/><a href="gymaccesories.html">Gym accesories</a><br/>
				Computer Products
				<li/><a href="pcs.html">PCs</a><br/><li/><a href="laptops.html">Laptops</a><br/><li/><a href="storage.html">computerStorage</a><br/><li/><a href="compaccesories.html">Comp Accesories</a><br/></td>
		</tr>
	</table>
	<body>
		<h1 align="center">Computing - Laptops</h1>
		<table border="1" align="center">
			<tr bgcolor="#9acd32">
				<th align="left">Name</th>
				<th align="left">Price</th>
				<th align="left">Description</th>
				<th align="left">image</th>
			</tr>
			<tr>
				<td>Dell xps</td>
				<td>£300</td>
				<td>great lightweight and powerfull laptop</td>
				<td> <img src="untitled.bmp"> </td>
			</tr>
			<tr>
				<td>Macbook</td>
				<td>£700</td>
				<td>What a laptop, brilliant!!</td>
				<td> <img src="macbook.jpg"> </td>
			</tr>
		</table>
	</body>
</html>

Open in new window

May I ask you something? Why is the submenu nested under each other submenu?In other words, why is gardenproducts a submenu to homegym? There's on large nesting of submenus and I just can't get the idea out of my head that the structure might have been intended differently. Are you sure you sent the correct data?
Avatar of kipl20

ASKER

hi yeah that wa wrong i changed the structure to this below and found a solution in the end

xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="lappy.xslt"?>
<navigation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Structure.xsd">
      <menu>
            <id>homeproducts</id>
            <title>Home Products</title>
            <url>homeproducts.html</url>
            <submenu>
                  <id>homefurniture</id>
                  <title>Home Furniture</title>
                  <url>homefurniture.html</url>
            </submenu>
            <submenu>
                  <id>homeelectrical</id>
                  <title>Home Electrical</title>
                  <url>homeelectrical.html</url>
            </submenu>
            <submenu>
                  <id>homeentertainment</id>
                  <title>Home Entertainment</title>
                  <url>homeentertainment.html</url>
            </submenu>
            <submenu>
                  <id>homegym</id>
                  <title>Home Gym</title>
                  <url>homegym.html</url>
            </submenu>
      </menu>
      <menu>
            <id>gardenproducts</id>
            <title>Garden Products</title>
            <url>gardenproducts.html</url>
            <submenu>
                  <id>gardenfurniture</id>
                  <title>Garden Furniture</title>
                  <url>gardenfurniture.html</url>
            </submenu>
            <submenu>
                  <id>gardenelectrical</id>
                  <title>Garden Electrical</title>
                  <url>gardenelectrical.html</url>
            </submenu>
            <submenu>
                  <id>gardenentertainment</id>
                  <title>Garden Entertainment</title>
                  <url>gardenentertainment.html</url>
            </submenu>
            <submenu>
                  <id>gardentools</id>
                  <title>Garden Tools</title>
                  <url>gardentools.html</url>
            </submenu>
      </menu>
      <menu>
            <id>gymproducts</id>
            <title>Gym Products</title>
            <url>gymproducts.html</url>
            <submenu>
                  <id>weights</id>
                  <title>Weights</title>
                  <url>gymweights.html</url>
            </submenu>
            <submenu>
                  <id>treadmills</id>
                  <title>Treadmills</title>
                  <url>gymtreadmills.html</url>
            </submenu>
            <submenu>
                  <id>exercisebikes</id>
                  <title>Exercise Bikes</title>
                  <url>gymexercisebikes.html</url>
            </submenu>
            <submenu>
                  <id>accesories</id>
                  <title>Gym accesories</title>
                  <url>gymaccesories.html</url>
            </submenu>
      </menu>
      <menu>
            <id>computerproducts</id>
            <title>Computer Products</title>
            <url>compproducts.html</url>
            <submenu>
                  <id>pcs</id>
                  <title>PCs</title>
                  <url>comppcs.html</url>
            </submenu>
            <submenu>
                  <id>laptops</id>
                  <title>Laptops</title>
                  <url>complaptops.html</url>
            </submenu>
            <submenu>
                  <id>storage</id>
                  <title>Computer Storage</title>
                  <url>compstorage.html</url>
            </submenu>
            <submenu>
                  <id>compaccesories</id>
                  <title>Computer Accesories</title>
                  <url>compaccesories.html</url>
            </submenu>
      </menu>
</navigation>
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kipl20

ASKER

thanks for your help abel
you're welcome, but you're actually the one that deserves the points, I wasn't back in time to serve you timely ... ;-)
Avatar of kipl20

ASKER

thats ok, you were trying to help and did give me some code to try so i think you deserve them