Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1250 posts 3747 karma points admin c-trib
    Apr 16, 2010 @ 18:02
    Dan
    0

    List children and parent if child has property

    Hi,

    I'm looking to include a list of resource categories and their resources on a page of a site.  The resources are under a node of id 1094 and they're of the structure:

    - Resource category name 1
    -- Resource name 1
    -- Resource name 2
    -- Resource name 3
    -Resource category name 2
    -- Resource name 4
    -- Resource name 5
    etc, etc.

    I want to put this into a macro so that the client can embed the macro on the page and have it render as:

    <h3>Resource category name 1</h3>
    <h4>Resource name 1</h4>
    <h4>Resource name 2</h4>
    <h4>Resource name 3</h4>
    <h3>Resource category name 2</h3>
    <h4>Resource name 4</h4>
    <h4>Resource name 5</h4>

     

    I've managed to do this with the following code:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:CWS.Twitter="urn:CWS.Twitter"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets CWS.Twitter ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>

    <xsl:template match="/">
    <xsl:call-template name="drawNodes">
    <xsl:with-param name="parent" select="umbraco.library:GetXmlNodeById(1094)/ancestor-or-self::node [@id=1094 and string(./data [@alias='umbracoNaviHide']) != '1']"/>
    </xsl:call-template>
    </xsl:template>

    <xsl:template name="drawNodes">
    <xsl:param name="parent"/>
    <h3><xsl:value-of select="@nodeName"/></h3>
    <xsl:for-each select="$parent/node [string(./data [@alias='umbracoNaviHide']) != '1']">
    <xsl:if test="@level = 4">
    <h4>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:value-of select="@nodeName"/>
    </a>
    </h4>
    </xsl:if>
    <xsl:if test="count(./node) &gt; 0">
    <xsl:call-template name="drawNodes">
    <xsl:with-param name="parent" select="."/>
    </xsl:call-template>
    </xsl:if>
    </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>

    The resource document type has a property called 'showElsewhere' (true/false).  Now I only want to list the resource if it has this property set to true, and also only show the resource category title if it has any resources under it which have the property set to true.

    Can anyone see how to do this?

    Thanks all...

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Apr 16, 2010 @ 18:51
    Chriztian Steinmeier
    0

    Hi Dan,

    This is a good example of where you'd let XSLT do its thing, and just define the templates for the output you want:

    <?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" />
    
        <xsl:param name="currentPage" />
    
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::node[@level = 1]" />
        <xsl:variable name="resourceRoot" select="$siteRoot/descendant-or-self::node[@id = 1094]" />
    
        <xsl:template match="/">
    
            <xsl:apply-templates select="$resourceRoot/node[@nodeTypeAlias = 'ResourceCategory']" />
    
        </xsl:template>
    
        <!-- Template for Category nodes -->
        <xsl:template match="node[@nodeTypeAlias = 'ResourceCategory']">
            <h3><xsl:value-of select="@nodeName" /></h3>
            <xsl:apply-templates select="node[@nodeTypeAlias = 'Resource']" />
        </xsl:template>
    
        <!-- Template for Resource nodes -->
        <xsl:template match="node[@nodeTypeAlias = 'Resource']">
            <h4><xsl:value-of select="@nodeName" /></h4>
        </xsl:template>
    
        <!-- Only output Resource Categories having a Resource with the showElsewhere property checked -->
        <xsl:template match="node[@nodeTypeAlias = 'ResourceCategory'][not(node[data[@alias = 'showElsewhere'] = 1])]" />
        <xsl:template match="node[@nodeTypeAlias = 'Resource'][not(data[@alias = 'showElsewhere'] = 1)]" />
    
        <!-- Never output these -->
        <xsl:template match="node[data[@alias = 'umbracoNaviHide'] = 1]" />
    
    </xsl:stylesheet>

     

    /Chriztian

  • Dan 1250 posts 3747 karma points admin c-trib
    Apr 16, 2010 @ 19:01
    Dan
    0

    Very nice indeed - I'm going to read up on XSLT templates this weekend I reckon, thanks Chriztian!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies