Re: [xsl] Dynamicly Generate a apply-templates match pattern.

Subject: Re: [xsl] Dynamicly Generate a apply-templates match pattern.
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 19 Aug 2004 09:46:07 +0100
Hi Peter,

> I would like to be able to transform my html template so that the
> match attribute of my rui:insert element is used to generate an
> apply-templates select statement.
>
> The template would be something like this:
>
>  <xsl:template match="rui:insert">
>    <xsl:apply-templates select="$content/@match"/>
> 	</xsl:template>

You can't do this in XSLT. What you can do, however, is create an XSLT
stylesheet that uses your HTML template to create an XSLT stylesheet
that, when run over your content, generates the output you want.
Diagrammatically:

                 generator
                 stylesheet
                     |
                     v
  html template -- (XSLT) -->  stylesheet
                                   |
                                   v
                      content -- (XSLT) --> result

The generator stylesheet would look something like:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns="http://www.w3.org/1999/XSL/TransformAlias";
                xmlns:rui="user.interface"
                exclude-result-prefixes="rui">

<xsl:namespace-alias stylesheet-prefix="#default"
                     result-prefix="xsl" />

<!-- create a styelsheet -->
<xsl:template match="/">
  <stylesheet version="1.0">
    <template match="results">
      <xsl:apply-templates />
    </template>
    <include href="main.xsl" />
  </stylesheet>
</xsl:template>

<!-- copy everything by default -->
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" />
  </xsl:copy>
</xsl:template>

<!-- insert <xsl:apply-templates> instructions in place of
     <rui:insert> elements -->
<xsl:template match="rui:insert">
  <apply-templates select="{@match}" />
</xsl:template>

</xsl:stylesheet>

Running this over your html template should generate something like:

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

<xsl:template match="results">
  <html>
    <header>
      <xsl:apply-templates select="pageDisplayName" />
    <header>
    <body>
      <xsl:apply-templates select="document('header.xml')" />
    </body>
  </html>
</xsl:template>

<xsl:include href="main.xsl" />

</xsl:stylesheet>

main.xsl should contain the templates for processing the
<pageDisplayName> element and the other elements that appear within
the <results> document.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Current Thread