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 2nd, 2011, 09:40 PM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default Generate multiple HTML pages using common node name

Hi

Please help this is driving me insane, I've simplified my XML and stylesheet below for explaining what I'm trying to achieve.

My stylesheet is successfully generating a HTML file for each of my categories using title i.e.

This Category.html
That Category.html

It also generates a simple index.html at the end which contains links to each of these pages.. However what I also want to achieve is:

This Category - This Sub Category 1.html
This Category - This Sub Category 2.html
This Category - This Sub Category 3.html
That Category - That Sub Category 1.html
That Category - That Sub Category 2.html
That Category - That Sub Category 3.html

With an index page that references each page, once I have this in place I can really start my website for my shop. I'm assuming I need another for-each loop

within the category for-each loop but when I do this I only get the 'This Category.html' level files generated. Whereas if I remove the inner for-each loop

I also get:

'This Sub Category 1 This Sub Category 2 This Sub Category 3.html' etc

Not what I want but hopefully I'm already close.

Hope this makes sense and thanks in advance.



XML:

<?xml version = "1.0" encoding = "UTF-8"?>
<categories>
<category>
<title>This Category</title>
<subcategory>This Sub Category 1</subcategory>
<subcategory>This Sub Category 2</subcategory>
<subcategory>This Sub Category 3</subcategory>
</category>
<category>
<title>That Category</title>
<subcategory>That Sub Category 1</subcategory>
<subcategory>That Sub Category 2</subcategory>
<subcategory>That Sub Category 3</subcategory>
</category>
</categories>



XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output method="text"/>
<xsl:output method="html" indent="yes" name="html"/>

<xsl:template match="/">

<xsl:for-each select="categories/category">

<xsl:variable name="varCat">
<xsl:value-of select="title"/>
</xsl:variable>


<xsl:for-each select="categories/category">

<xsl:variable name="varSubCat">
<xsl:value-of select="subcategory"/>
</xsl:variable>

<xsl:variable name="filename"
select="concat('output/',$carCat,' - '$varSubCat,'.html')" />
<xsl:value-of select="$filename" />

<xsl:result-document href="{$filename}" format="html">
<html><body>
<xsl:value-of select="subcategory"/>
</body></html>
</xsl:result-document>

</xsl:for-each>

<xsl:variable name="filename"
select="concat('output/',$varCat,'.html')" />
<xsl:value-of select="$filename" />


<xsl:result-document href="{$filename}" format="html">
<html><body>
<xsl:value-of select="title"/>
</body></html>
</xsl:result-document>

</xsl:for-each>

<xsl:result-document href="output/index.html"
format="html">
<html><head><title>Test Index</title></head>
<body>
<xsl:for-each select="categories/category">

<a href="{title}.html"><xsl:value-of select="title" />
</a><br/>
</xsl:for-each>
</body>
</html>
</xsl:result-document>

</xsl:template>

</xsl:stylesheet>
 
Old February 3rd, 2011, 06:29 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you post code samples then consider to indent them and mark them up with http://p2p.wrox.com/misc.php?do=bbcode#code as that makes it easier for other to read them.
As for your problem, I would not write one template with lots of for-each, instead I would write templates with different modes so that you can have one mode for creating an index for instance. That way it is easier to separate the different tasks.

Also note that instead of doing
Code:
<xsl:variable name="varCat">
		<xsl:value-of select="title"/>
	</xsl:variable>
you can and should simply use
Code:
<xsl:variable name="varCat" select="title"/>
__________________
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 5th, 2011)
 
Old February 5th, 2011, 09:45 AM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Many thanks I have succesfully managed to create the desired pages based on your advice, and apologies for being unaware of the etiquette of posting code I will do so properly in future. :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to generate in HTML lily611 C# 1 July 29th, 2013 05:44 AM
Creating a template to generate a node set Chamkaur XSLT 1 July 27th, 2007 02:51 AM
Response.Write to generate HTML fhenderson BOOK: Beginning ASP.NET 1.0 0 March 10th, 2006 02:57 PM
Multiple XML files matched with common Number Id gurbani XSLT 3 June 23rd, 2004 05:58 AM
Common question, not so common answer? flyin ADO.NET 5 March 24th, 2004 06:50 PM





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