tags:

views:

25

answers:

1

I am trying to basically recreate the functionality of an ASP.NET master page with an XSLT template.

I have a "master page" template that contains much of the page html stored in an .xslt file. I have another .xslt file specific to a single page, that takes in xml representing the page data. I want to call the master page template from my new template, and still have the ability to insert my own xml that will be applied. If I could pass a param that would allow me to call template with the param as the name, that would do the trick, but that doesn't appear to be allowed.

Basically I have this:

<xsl:template name="MainMasterPage">
  <xsl:with-param name="Content1"/>
  <html>
    <!-- bunch of stuff here -->
    <xsl:value-of select="$Content1"/>
  </html>
</xsl:template>

And this:

<xsl:template match="/">
  <xsl:call-template name="MainMasterPage">
    <xsl:with-param name="Content1">
      <h1>Title</h1>
      <p>More Content</p>
      <xsl:call-template name="SomeOtherTemplate"/>
     </xsl:with-param>
   </xsl-call-template>
</xsl:template>

What happens is that the nested xml is basically stripped and all that is inserted is "TitleMore Content"

+2  A: 

The problem with the provided code is here:

<xsl:value-of select="$Content1"/>

This will output either the concatenation of all text-nodes descendents of the top node of $Content1 (if it contains a document) or the string value of its first element or text child (if it is an XML fragment).

You need to use

<xsl:copy-of select='$pContent1'>

instead of

<xsl:value-of select='$pContent1'>.

This correctly copies all children nodes of $pContent1

Below is a corrected transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

<xsl:template match="/">
  <xsl:call-template name="MainMasterPage">
    <xsl:with-param name="pContent1">
      <h1>Title</h1>
      <p>More Content</p>
      <xsl:call-template name="SomeOtherTemplate"/>
     </xsl:with-param>
   </xsl:call-template>
</xsl:template>

<xsl:template name="MainMasterPage">
  <xsl:param name="pContent1"/>
  <html>
    <!-- bunch of stuff here -->
    <xsl:copy-of select="$pContent1"/>
  </html>
</xsl:template>

 <xsl:template name="SomeOtherTemplate">
   <h2>Hello, World!</h2>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document (not used), the wanted, correct result is produced:

<html>
   <h1>Title</h1>
   <p>More Content</p>
   <h2>Hello, World!</h2>
</html>
Dimitre Novatchev