Created
February 12, 2013 23:06
-
-
Save designermonkey/4774339 to your computer and use it in GitHub Desktop.
Using Names and Matches together in XSL templates, we can choose and apply templates dynamically, based on content provided within the XML being transformed. I've searched for ages on how to do this properly, and a number of sources led me to this solution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<data> | |
<datasource> | |
<section handle="test-section">Test Section</section> | |
<entry> | |
<page-path handle="about">/about</page-path> | |
<page-title handle="about">About</page-title> | |
<title handle="about-this-company">About This Company</title> | |
<template> | |
<item id="2" handle="content" section-handle="page-templates" section-name="Page Templates">Content</item> | |
</template> | |
</entry> | |
</datasource> | |
</data> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<data> | |
<datasource> | |
<section handle="test-section">Test Section</section> | |
<entry> | |
<page-path handle="about-history">/about/history</page-path> | |
<page-title handle="history">History</page-title> | |
<title handle="the-history-of-this-company">The History of This Company</title> | |
<template> | |
<item id="4" handle="article" section-handle="page-templates" section-name="Page Templates">Article</item> | |
</template> | |
</entry> | |
</datasource> | |
</data> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<data> | |
<datasource> | |
<section handle="test-section">Test Section</section> | |
<entry> | |
<page-path handle="contact">/contact</page-path> | |
<page-title handle="contact">Contact</page-title> | |
<title handle="contact-this-company">Contact This Company</title> | |
<template> | |
<item id="3" handle="contact" section-handle="page-templates" section-name="Page Templates">Contact</item> | |
</template> | |
</entry> | |
</datasource> | |
</data> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<!-- | |
Create a variable of this document's templates | |
--> | |
<xsl:variable name="content-templates" select="document('')/*/xsl:template"/> | |
<!-- | |
Match on a content node from the XML | |
--> | |
<xsl:template match="*[section/@handle = 'test-section']/entry"> | |
<xsl:apply-templates select="$content-templates[@name = current()/template/item/@handle]"> | |
<xsl:with-param name="node" select="."/> | |
</xsl:apply-templates> | |
</xsl:template> | |
<!-- | |
A Template to output content as a 'Content' style page | |
--> | |
<xsl:template match="xsl:template[@name='content']" name="content"> | |
<xsl:param name="node"/> | |
<h1>content template called!</h1> | |
<h2><xsl:value-of select="$node/page-title/text()"/></h2> | |
</xsl:template> | |
<!-- | |
Template to output content as a 'Contact' style page | |
--> | |
<xsl:template match="xsl:template[@name='contact']" name="contact"> | |
<xsl:param name="node"/> | |
<h1>contact template called!</h1> | |
<h2><xsl:value-of select="$node/page-title/text()"/></h2> | |
</xsl:template> | |
</xsl:stylesheet> |
That's really cool too! Nice approach.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very interesting idea you showed above. Didn't know XSLT is capable of this stuff.
Another way to do it would be to use the
mode
attribute and thexsl:template match=""
. Here's how I'm doing similar stuff: