Using result-document - how can I create a template within?

Here should go questions about transforming XML with XSLT and FOP.
slaterino
Posts: 11
Joined: Sat Jan 09, 2010 8:13 pm

Using result-document - how can I create a template within?

Post by slaterino »

Hi,
I am using result-document to create a number of pages for my site. I am just about to start customizing the styles a lot more to get them looking how I need them. However, I can't work out how to use the templates within the result-document tag. I have posted my code below. You will see that the <html>, <head> and <body> tags are repeated in both result-document sections. I am trying to find a way that I could use a template to create these tags so that there is no need for a duplicate, but can't work out how. The only way I know to use <xsl:apply-templates> is by selecting the names of one of the elements but I wouldn't want to do this in this example. Does anyone know how this is done?

Here's the code appropriate to this question:

Code: Select all

    <xsl:template match="/">
<xsl:apply-templates select="//chapter"/>
<xsl:result-document href="new/index.php" format="html">
<html><head><title>Index</title></head>
<body>
<xsl:for-each select="//chapter">
<a href="{abbrev}.php"><xsl:value-of select="title" />
</a><br/>
</xsl:for-each>
</body>
</html>
</xsl:result-document>
</xsl:template>

<xsl:template match="//chapter">
<xsl:variable name="filename" select="concat('new/',abbrev,'.php')" />
<xsl:value-of select="$filename" />
<xsl:result-document href="{$filename}" format="html">
<html><head>
<title>Test results - <xsl:value-of select="title"/></title>
</head><body>
<xsl:value-of select="title"/>
<xsl:apply-templates select="section"/>
</body></html>
</xsl:result-document>
</xsl:template>
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Using result-document - how can I create a template within?

Post by adrian »

Hello,

You can create a template and give it a name, like this for example:

Code: Select all

<xsl:template name="common">
<html>
<head></head>
<body></body>
</html>
</xsl:template>
And call it from result-document with <xsl:call-template name="common"/>.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
slaterino
Posts: 11
Joined: Sat Jan 09, 2010 8:13 pm

Re: Using result-document - how can I create a template within?

Post by slaterino »

Cheers for that Adrian. I have tried using call-template but am still unsure exactly how I can use this template for both of my xsl:result-document needs. For example, with your script, I would have:

Code: Select all


<xsl:template name="common">
<html>
<head></head>
<body>
[in the middle here I would want calls to two different templates]
</body>
</html>
</xsl:template>
Is there anyway I would be able to work it so that include this template inside of my two different xsl:result-document tags. Is there a way that I could attach a variable to each of the two different result-document tags and then use xsl:if asking for the name of that variable. What would be the way of doing this? I have been looking through all kinds of manuals for the answer on this but it has not been easy to find!!

Thanks for your help though. It's appreciated!
Post Reply