xsl-list
[Top] [All Lists]

Re: Dynamicly Generate a apply-templates match pattern.

2004-08-19 01:46:07
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="{(_at_)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/



<Prev in Thread] Current Thread [Next in Thread>