Copied to clipboard

Flag this post as spam?

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


  • cchehn 28 posts 69 karma points
    Dec 04, 2012 @ 23:40
    cchehn
    0

    Recursive uComponents url picker XSLT values

    Hi!  I am horrible at xslt and am trying to figure out how to retrieve the equivelent of:

    In an xslt macro

    <xsl:value-of select="$currentPage/headerLogoURL/url-picker/new-window"/><br/>
    <xsl:value-of select="$currentPage/headerLogoURL/url-picker/node-id"/><br/>
    <xsl:value-of select="$currentPage/headerLogoURL/url-picker/url"><br/>
    <xsl:value-of select="$currentPage/headerLogoURL/url-picker/link-title"/><br/>

    from ancestor-or-self

    The desired end result is for the content editor to be able to set the headerLogoURL on the homepage and then if needed overide the parameter on a child page.

    I want to place the macro in my top level template.

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Dec 04, 2012 @ 23:53
    Chriztian Steinmeier
    0

    Hi cchehn,

    This is how to lookup recursively in XSLT:

    <!-- Process the first ancestor (or self) that has a value in headerLogoURL -->
    <xsl:apply-templates select="$currentPage/ancestor-or-self::*[normalize-space(headerLogoURL)][1]/headerLogoURL" />
    
    <!-- Template for the headerLogoURL -->
    <xsl:template match="headerLogoURL">
        <xsl:value-of select="url-picker/url" />
        <xsl:value-of select="url-picker/node-id" />
        <xsl:value-of select="url-picker/new-window" />
        <xsl:value-of select="url-picker/link-title" />
    </xsl:template>

    /Chriztian 

  • cchehn 28 posts 69 karma points
    Dec 05, 2012 @ 14:15
    cchehn
    0

    Thanks Chriztian,

    Can you give me a little detail on what *[][1] is actually doing in the select?

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Dec 05, 2012 @ 14:35
    Chriztian Steinmeier
    0

    Yes - of course :-)

    The XPath starts from $currentPage, using the ancestor-or-self:: axis it grabs all nodes that have a value in the headerLogoURL field, takes the first of those and selects the headerLogoURL field.

    When you add square brackets to a selection step, its called a "predicate" and it works like a filter - you can loosely translate them to a WHERE clause.

    normalize-space() is a handy function that trims leading and trailing as well as repeated white-space from an element, so if the value is all spaces og tabs etc. it will return an empty string.

    [1] is a shortcut for [position() = 1] - in case of the ancestor-or-self:: axis, this means the first ancestor encountered when traveling upwards.

    Hope that helps you understand what's going on.

    /Chriztian

  • cchehn 28 posts 69 karma points
    Dec 05, 2012 @ 17:30
    cchehn
    0

    Yes this is very helpful, thanks very much!  I have another followup question if it is not too much trouble.

    If I specify [1] for the first ancestor then in a structure:

    <n1>
      <val>a</val>
      <n2>
         <val>b</val>
           <n3>
              <val></val>
           <n3>
      </n2>
      <n4>
         <val></val>
      </n4>
    </n1>

    On n3 I would get val = b ?
    On n4 I would get val = a ? 

    Am I grasping this correctly?

  • cchehn 28 posts 69 karma points
    Dec 05, 2012 @ 18:24
    cchehn
    0

    Thanks for your help Chriztian,


    Yet another question...  I am using uComponents url picker to set headerLogoURL and it appears that even if nothing is selected or entered in the url picker it still returns a node on every Doc node:

    <url-picker><new-window>False</new-window><node-id></node-id><url></url><link-title><url-picker>

    How do I account for this since in my original example headerLogoURL will always be set but url-picker/url might be blank?

    I tried:

    <xsl:apply-templates select="$currentPage/ancestor-or-self::*[normalize-space(headerLogoURL/url-picker/url)][1]/headerLogoURL/url-picker/url" />

    But then I have no idea what to select in the template.

    <xsl:value-of select="???" /> 

    Thanks very much in advance!

    -C.

  • cchehn 28 posts 69 karma points
    Dec 05, 2012 @ 20:15
    cchehn
    0

    Figured out what I was doing wrong! I can now set a default logo for my site at the top level but it can be overriden on lower level documents.  So now my content editors can easily co-brand a page within the site either with a logo from the media library or an image url hosted on another site.  

    Just in case anyone else is trying to do something similar here is what I ended up with:

    <xsl:template match="/">
    <!-- Process the first ancestor (or self) that has a value in headerLogoURL/url-picker/url
    and work with headerLogoURL in the next template -->
    <xsl:apply-templates select="$currentPage/ancestor-or-self::*[normalize-space(headerLogoURL/url-picker/url)][1]/headerLogoURL" />
    </xsl:template>

    <!-- Template for the headerLogoURL -->
    <xsl:template match="headerLogoURL">
    <a href="/"><xsl:attribute name="style">display:block; width:349px; height:200px; background:transparent url('<xsl:value-of select="url-picker/url"/>') no-repeat center;</xsl:attribute>Home</a>
    </xsl:template>

     Thanks again Chriztian

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Dec 06, 2012 @ 09:35
    Chriztian Steinmeier
    0

    Hi cchehn,

    Great to see you arrived at something that works :-)

    Just to clarify your questions regarding the ancestor:: axis: You're not quite right on that - it gives you the parent, its parent, that one's parent etc., so you would never get any siblings of the current node (which is what you're guessing at).

    The preceding:: axis will give you those - but you should really give the XPath Axes Visualizer a spin to really get the hang of them :-)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft