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 February 11th, 2011, 07:49 AM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default Generate Index.html page

Hi

I'm generating the webpages for my website as per my code below, I'm having difficulty generating an Index.html page to go alongside these files, can anyone help?

Code:
<?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:template match="/">

<html>
	<style type="text/css">
		table {		border: solid 3px #000000;}		             th,td {		border: solid 3px #000000;}
	</style>			
	<body>				
	<table><th>Title</th><th>Sub Department</th><th></th>
		<xsl:apply-templates select="departments"/>
	</table>
	</body>
</html>

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

	<xsl:template match="department">
		<xsl:apply-templates select="subdepartment">
		<xsl:with-param name="title" select="title"/>
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template match="subdepartment">
		<xsl:param name="title"/>
		<xsl:variable name="subdept" select="(.)"/>
		<xsl:variable name="filename" select="concat('output/',$title,' - ',.,'.html')"/>
		<tr><td><xsl:value-of select="$title"/></td>
		<td><xsl:value-of select="concat(.,'.html')"/></td>
		<td><a href="{$filename}">select</a></td></tr>

		<!-- webpages in html file -->
		<xsl:result-document href="{$filename}" format="html">

		<html>
			<body>
                                      <!-- my page code sits here-->
			</body>
		</html>
		</xsl:result-document>
	</xsl:template>
</xsl:stylesheet>
This generates my pages in the form of:

Department 1 - Sub Department 1.html
Department 1 - Sub Department 2.html
Department 2 - Sub Department 1.html
Department 2 - Sub Department 2.html

...and so on. I would like an Index which for the time being just includes hyperlinks to each page, and preferably a higher level link for each department which takes you to the first sub department within it.

I'm still trying to achieve this but in the meantime any assistance is much appreciated, my current attempts fail with errors such as:

Cannot write one than one result document to the same URI;
Or my variables store both sub departments in the same string

By the way I'm parsing this stylesheet through Saxon.

Many thanks
 
Old February 11th, 2011, 08:01 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you have code that generates an error then please show us the code generating that error and the exact error message. And of course we need to see an XML input sample.
If you want to output data from some nodes in different result documents then using modes on your templates can help.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 11th, 2011, 08:31 AM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Example of input xml:

Code:
<?xml version = "1.0" encoding = "UTF-8"?>

<departments>
	<department>
		<title>Department 1</title>
		<subdepartment>Dept 1 Sub Dept 1</subdepartment>
		<subdepartment>Dept 1 Sub Dept 2</subdepartment>
	</department>
	<department>
		<title>Department 2</title>
		<subdepartment>Dept 2 Sub Dept 1</subdepartment>
		<subdepartment>Dept 2 Sub Dept 2</subdepartment>
	</department>
	<department>
		<title>Department 3</title>
		<subdepartment>Dept 3 Sub Dept 1</subdepartment>
		<subdepartment>Dept 3 Sub Dept 2</subdepartment>
	</department>
</departments>
The code I'm currently working on I have positioned immediately after the last result-document tag in my above stylessheet:

Code:
<!-- Creating the index file-->
<xsl:result-document href="output/index.html" format="html">
<html><head><title>Index Page</title></head>
	<body>
	<xsl:for-each select="departments/department/subdepartment">
		<xsl:variable name="varDept" select="../title"/>
		<a href="'{$varDept}, - ',{.}'.html"><xsl:value-of select="concat($varDept,' - ',.,'.html')"/>
		</a><br/>
	</xsl:for-each>
	</body>
</html>		
</xsl:result-document>
This generates a blank index.html, only two of the sub department pages where there should be 16 (and works if I remove my Index creating code) and returns the error:

Error at xsl:result-document on line 44 of copystylesheet.xslt:
XTDE1490: Cannot write more than one result document to the same URI:

file:/C:/websites/diecastcars/webpages/index.html
at xsl:apply-templates (file:/C:/websites/diecastcars/copystylesheet.xslt#23)
processing /categories/category[1]/subcategory[2]
at xsl:apply-templates (file:/C:/websites/diecastcars/copystylesheet.xslt#19)
processing /categories/category[1]
at xsl:apply-templates (file:/C:/websites/diecastcars/copystylesheet.xslt#12)
processing /categories
Transformation failed: Run-time errors were reported

NB: line 44 is:

<xsl:result-document href="webpages/index.html" format="html">
 
Old February 11th, 2011, 08:34 AM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Correction:

NB: line 44 is:
<xsl:result-document href="output/index.html" format="html">
 
Old February 11th, 2011, 08:51 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you put your
<xsl:result-document href="output/index.html" format="html">
into a template for an element which exists multiple times (like your subcategory elements) then of course your code tries to create the same index.html multiple times. That is not possible. So you need to restructure your code to make sure you create the document only once. As already suggested, in my view using modes is the right way or at least one way to achieve that e.g.
Code:
<xsl:template match="/">
  <xsl:result-document href="output/index.html" format="html">
    <xsl:apply-templates mode="index"/>
  </xsl:template>
  <!-- put other code here -->
</xsl:template>

<xsl:template match="departments" mode="index">
  <html>
    <head>
      <title>Department index</title>
    </head>
    <body>
      <ul>
        <xsl:apply-templates select="index"/>
      </ul>
    </body>
  </html>
</xsl:template>

<xsl:template match="subdepartment" mode="index">
   <li>
     <a href="output/{../title}{.}.html">
        <xsl:value-of select="."/>
     </a>
   </li>
</xsl:template>

<!-- other templates here for other content and/or other modes -->

</xsl:template>
Untested sample, and I am not sure I have used the proper URL in the href attribute you are constructing in your other code but it should give you an idea how to approach that to avoid the error you have described.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
tombliboo (February 11th, 2011)
 
Old February 11th, 2011, 01:00 PM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thanks cracked it!





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to generate in HTML lily611 C# 1 July 29th, 2013 05:44 AM
generate index rshan XSLT 1 May 23rd, 2007 01:14 AM
how to disable toobars for index.html ? Michael8 Javascript How-To 0 November 24th, 2006 01:27 PM
Response.Write to generate HTML fhenderson BOOK: Beginning ASP.NET 1.0 0 March 10th, 2006 02:57 PM
can i generate index column alyeng2000 SQL Server 2000 0 February 25th, 2004 07:12 PM





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