Copied to clipboard

Flag this post as spam?

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


  • eddy 43 posts 64 karma points
    Aug 24, 2010 @ 16:35
    eddy
    0

    Body ID selector based on ancestor

    I'm new to Umbraco and XSLT and have watched Pers great videos and read a few documents but have fallen at the first hurdle.

    Depending on the active page, I would like to use a macro to render an ID selector to the pages' Body Tag based on a sections' parent node in order to apply CSS styles specific to that section. Thus all children will inherit the correct Body ID for that section and receive the correct styling from the stylesheet. I don't want to enter a property into the document type for the editor to enter each time they create a new article.

    There's a few similar posts in the forums but nothing I've tried works. I tried Heather Mills' plugin but it renders a new ID selector for every page rather than each section.

    I don't receive any errors but at the same time am not receiving any data. Could someone please point me in the right direction with some really basic tips? Am I using the correct output method? 

    Here's one of the versions I tried which was very basic:

     

    <?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" 

     

      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

     

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

     

    <xsl:param name="currentPage"/>

    <xsl:variable name="myID" select="$currentPage/ancestor::node [@level=2]"/>
    <xsl:template match="/">
    <!-- The fun starts here -->
      <xsl:value-of select="$myID" />
    </xsl:template>
    </xsl:stylesheet>

     

     

    Heres the code which I got from Heather Mills' dynamic Body ID plugin. As you can see I tried to change the current page id to current page ancestor but without any joy.

    <?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:fi="urn:schemas-floyd-innovations"
      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:Designit.VideoEmbed="urn:Designit.VideoEmbed" xmlns:pdcalendar="urn:pdcalendar" xmlns:CWSHelper.twitter="urn:CWSHelper.twitter" 
      exclude-result-prefixes="fi msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets Designit.VideoEmbed pdcalendar CWSHelper.twitter ">
    <!--Util_BodyTag.xslt 2/24/2010-->
    <!--Developed by Heather Floyd - http://HeatherFloyd.com-->;
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <!--MACRO PARAMS:                      
      TextForTag      Text to add to the tag (optional)  text
      IncludeClass  Add Class=[TemplateAlias]?  bool
    -->
    <xsl:param name="currentPage"/>
    <xsl:variable name="TextForTag" select="macro/TextForTag"/>
    <xsl:variable name="IncludeClass" select="macro/IncludeClass"/>
    <xsl:template match="/">
    <xsl:variable name="TemplateID" select="$currentPage/@template"/>
    <xsl:variable name="TemplateName" select="fi:GetTemplateAlias($TemplateID)"/>
    <xsl:variable name="NodeID" select="$currentPage/ancestor::node"/>
    <xsl:variable name="BodyTagStart" select="concat('&lt;body id=&quot;Node',$NodeID,'&quot; ')"/>
    <xsl:variable name="BodyTagEnd" select="concat($TextForTag,'&gt;')"/>
    <xsl:if test="$IncludeClass=1">
      <xsl:value-of select="concat($BodyTagStart,'class=&quot;',$TemplateName,'&quot; ',$BodyTagEnd)" disable-output-escaping="yes"/>
    </xsl:if>
    <xsl:if test="string($IncludeClass)!='1'">
      <xsl:value-of select="concat($BodyTagStart,$BodyTagEnd)" disable-output-escaping="yes"/>
    </xsl:if>
    </xsl:template>
    <msxml:script language="C#" implements-prefix="fi">
    <msxml:assembly name="cms"/>
    <msxml:assembly name="businesslogic"/>
    <msxml:using namespace="umbraco.cms.businesslogic"/>
    <msxml:using namespace="umbraco.cms.businesslogic.template"/>
    <![CDATA[
    public string GetTemplateAlias(int TemplateID)
    {
      Template templateLookup = Template.GetTemplate(TemplateID);
          return templateLookup.Alias;
    }
    ]]>
    </msxml:script>
    </xsl:stylesheet>

     

    Any tips really apprecaited!

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Aug 24, 2010 @ 21:28
    Chriztian Steinmeier
    2

    Hi eddy,

    Wow, that's a truckload of code :-)

    I've been doing this with a snippet of Inline XSLT for a long time and it "just" works - it's done right in the template:

    <body<umbraco:Item field="pageID" runat="server" xslt="concat(' id=&quot;', $currentPage/ancestor-or-self::*[@level = 2]/@nodeName, '&quot;')"></umbraco:Item>>

    This one just puts the nodeName of the level2 ancestor in there but you can take pretty much any value you want, if you can grab it with a "one-liner" XPath selection. 

    /Chriztian

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 24, 2010 @ 21:44
    Jan Skovgaard
    0

    Hi Chriztian

    May I ask why you prefer to make the XSLT inline? Is there any particular reason?...Just being curious :-)

    /Jan

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Aug 24, 2010 @ 21:55
    Chriztian Steinmeier
    0

    Of course Jan,

    When the body tag is sitting directly in the template, there is no good looking way of doing this (any tag *inside* another tag is definitely not my cup of tea - you ever done any DW templates :-), so I just take what's the fastest way with the least amount of "extras" to keep track of. Creating a separate Macro+XSLT file for this is a little too much for me, especially since the output of said macro is not even going to be "real" XML - you can't generate just an attribute as a result from a transformation so you end up creating a string of text, which is exactly what we accomplish with the Inline XSLT method. It's just as ugly but there's only one place I have to go, should I need o change something.

    Hope that's helpful-

    /Chriztian 

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 24, 2010 @ 22:13
    Jan Skovgaard
    0

    Hehe...I've done a few DW templates...it was a joy! (not!) ;-)

    I usually make the XSLT macro and I feel dirty every time...I think that I'll be using the inline XSLT in the future since it makes more sense to me to do it this way now. I'll probably still feel dirty but maybe not as much. Thanks for the input.

    /Jan

  • eddy 43 posts 64 karma points
    Aug 25, 2010 @ 09:37
    eddy
    0

    Chriztian - worked like a charm! Thank you so much.

    Eddy

Please Sign in or register to post replies

Write your reply to:

Draft