Skip to content

Instantly share code, notes, and snippets.

@uniquelau
Created February 28, 2013 16:34
Show Gist options
  • Save uniquelau/5058019 to your computer and use it in GitHub Desktop.
Save uniquelau/5058019 to your computer and use it in GitHub Desktop.
XSLT Document Property templating with Fallback.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:umb="urn:umbraco.library"
exclude-result-prefixes="umb"
>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<!-- Default Article Template, used for all nodes. -->
<xsl:template match="*" mode="article">
<h1>
<xsl:value-of select="contentHeader | @nodeName[not(./contentHeader)] [1]" />
</h1>
<xsl:apply-templates select="contentBody" mode="WYSIWYG" />
<!-- why not template the header, so it can be overidden/used on say a comment, etc-->
<h2>
<xsl:apply-templates select="contentHeader | @nodeName[not(./contentHeader)] [1]" mode="test" />
</h2>
</xsl:template>
<xsl:template match="*" mode="test">
<b>big</b>
<i>
<xsl:value-of select="."/>
<!-- outputs null -->
</i>
<xsl:copy-of select="."/>
<!-- outputs <contentHeader></contentHeader> -->
</xsl:template>
</xsl:stylesheet>
@greystate
Copy link

Line 14 (and 20) doesn't do what you think they do (I think :-)

They will probably always give you @nodeName - and it's a little tricky to explain why (which I'll happily do over beer someday)

Here's what I do:

<!-- If you want @nodeName when no <contentHeader> exists: -->
<xsl:value-of select="(@nodeName[not(../contentHeader)] | contentHeader)[1]" />

<!-- If you want @nodeName when <contentHeader> is empty: -->
<xsl:value-of select="(@nodeName[not(normalize-space(../contentHeader))] | contentHeader)[1]" />

(The second one works for the first scenario too, so I always use the second)

When using apply-templates, you need to make the template match both elements and attributes in "test" mode (e.g.: match="* | @*")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment