Copied to clipboard

Flag this post as spam?

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


  • Nathan Syverson 4 posts 24 karma points
    Feb 08, 2011 @ 18:19
    Nathan Syverson
    0

    Parameterized Macro: I want to display True/False field as "Yes" or "No"

    Hi,

    XSLT newbie here.  I can't seem to figure this one out.  I want to pass the field alias of a document's True/False field to a macro, and have it display the value as Yes or No.  Here is what I have:

    <?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="field" select="/macro/field"/>
        
    <xsl:template match="/">
    <!-- start writing XSLT -->
    <xsl:if test="data[@alias= '$field']=1">
    &nbsp;Yes
    </xsl:if>
    <xsl:if test="data[@alias= '$field']=0">
    &nbsp;No
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>
  • Patrick McAndrew 48 posts 163 karma points
    Feb 08, 2011 @ 19:04
    Patrick McAndrew
    0

    Hi Nathan,

    Sorry, just about to leave work, so don't have time to actually verify this syntax, but hopefully it gives you the idea to start searching around...

    Typically, I would pass the value of the field to the xslt as a parameter rather than the field name - something like:

     <umbraco:Macro Alias="macroAlias" runat="server" field="[#field]"></umbraco:Macro> 

    then something like

    ...
    <xsl:template
    match="/">
    <!-- start writing XSLT -->
    <xsl:if test="$field='1'">
    Yes
    </xsl:if>
    ...

    If you want to do it the way your're doing, its something like (although depends on if you're using 4 or 4.5:

    ...
    <
    xsl:template match="/">
    <!-- start writing XSLT -->
    <xsl:if test="string($currentPage/data[@alias= '$field'])='1'">
     Yes
    </xsl:if>
    ...
  • Nathan Syverson 4 posts 24 karma points
    Feb 08, 2011 @ 19:17
    Nathan Syverson
    0

    Thanks a bunch!!!

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Feb 08, 2011 @ 23:09
    Chriztian Steinmeier
    0

    Hi Nathan (+ Patrick)

    Here's a one-liner that does the trick:

    <xsl:value-of select="substring('YES|NO', not($currentPage/data[@alias = $field] = 1) * 4 + 1, 3)" />

    /Chriztian

    EDIT: Updated to fit your initial setup with a macro parameter stating the name of the property to test. You just need to get rid of the apostrophes in your code to make it work.) 

Please Sign in or register to post replies

Write your reply to:

Draft