Copied to clipboard

Flag this post as spam?

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


  • Ralph van Vugt 57 posts 101 karma points
    Jan 19, 2011 @ 19:00
    Ralph van Vugt
    0

    if then statement in XSLT?

    I made an XSLT which shows a different docType for every day of the week. I have set the weekdays:

    <xsl:variable name="today" select="Exslt.ExsltDatesAndTimes:dayinweek()"/>
    ...
    <xsl:if test="$today = '2'">
      call docType "Monday"
    </xsl:if>
    <xsl:if test="$today = '3'">
        call DocType "Tuesday"
    </xsl:if>

    Now I copy the same piece of XSLT for the seven weekdays. Is is possible to set a string depending on the outcome of another string?

    Something like if $today = "4" then $docType = "Wednesday"?

    Thanks,
    Ralph

  • Eran Meir 401 posts 543 karma points
    Jan 19, 2011 @ 19:03
  • Ralph van Vugt 57 posts 101 karma points
    Jan 19, 2011 @ 19:11
    Ralph van Vugt
    0

    Hi Eran,

    Thanks for your quick reply!

    Can I use <xsl:choose> to set a <xsl:variable> ?

     

    <xsl:choose>
        <xsl:when test="$today = '4'">
           <xsl:variable name="docType" select="Wednesday"/>
        </xsl:when>
        <xsl:when test="$today = '5'">
           <xsl:variable name="docType" select="Friday"/>
        </xsl:when>
        <xsl:otherwise></xsl:otherwise>
    </xsl:choose>

     

  • Eran Meir 401 posts 543 karma points
    Jan 19, 2011 @ 19:16
    Eran Meir
    0

    <xsl:variable name="day">
    <xsl:choose>
       
    <xsl:when test="$today = '4'">
           
    <xsl:value-of select="Wednesday"/>
       
    </xsl:when>
       
    <xsl:when test="$today = '5'">
           
    <xsl:value-of select="Friday"/>
       
    </xsl:when>
       
    <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jan 19, 2011 @ 21:32
    Chriztian Steinmeier
    1

    Hi Ralph,

    I'll suggest an entirely different solution just for inspiration; I don't exactly know what you mean by docType, but I'm assuming you have a container document of some sorts, containing seven documents named after the weekdays - whether they're the same docType with different names (@nodeName) or 7 unique docTypes ("Monday" docType, "Tuesday" docType etc.) doesn't matter - this would work:

    <?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"
        xmlns:date="urn:Exslt.ExsltDatesAndTimes"
        xmlns:make="urn:schemas-microsoft-com:xslt"
        exclude-result-prefixes="umb date make"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" select="/root/Website/Textpage[@id]" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
        <!-- Grab the container of the Weekday docs -->
        <xsl:variable name="weekdayContainer" select="$siteRoot/Weekdays" />
    
        <!-- XML variable for easy lookup -->
        <xsl:variable name="weekdays-mapper">
            <day id="1">Sunday</day>
            <day id="2">Monday</day>
            <day id="3">Tuesday</day>
            <day id="4">Wednesday</day>
            <day id="5">Thursday</day>
            <day id="6">Friday</day>
            <day id="7">Saturday</day>
        </xsl:variable>
        <xsl:variable name="weekdays" select="make:node-set($weekdays-mapper)" />
    
        <!-- Get the day of the week -->
        <xsl:variable name="today" select="date:dayinweek()" />
    
        <xsl:template match="/">
    
            <!-- (1) If your docTypes are of the same type (e.g. "Weekday") and just named after the day, use @nodeName -->
            <xsl:apply-templates select="$weekdayContainer/Weekday[@nodeName = $weekdays/day[@id = $today]]" />
    
            <!-- (2) If you have 7 different docTypes named "Sunday", "Monday" etc. use name() -->
            <xsl:apply-templates select="$weekdayContainer/*[name() = $weekdays/day[@id = $today]]" />
    
        </xsl:template>
    
    
    <!-- Template for a Weekday - change to your actual docType name (1) -->
        <xsl:template match="Weekday">
            <p>
                Hey this is <xsl:value-of select="@nodeName" />.
            </p>
        </xsl:template>
    
        <!-- = = = = = = = = = = -->
    
    <!-- Templates for the daily doctypes (2) -->
        <xsl:template match="Monday">
            <p>So this is Monday, huh?</p>
        </xsl:template>
    
        <xsl:template match="Tuesday">
            <p>Aha - Tuesday!</p>
        </xsl:template>
    
        <xsl:template match="Wednesday">
            <p>Yay - it's Wednesday!</p>
        </xsl:template>
    
        <!-- Etc... -->
    
    </xsl:stylesheet>
    
    Hope this helps you - otherwise, I should probably issue a refund for the time you spent reading it :-) 

    /Chriztian

  • Ralph van Vugt 57 posts 101 karma points
    Jan 20, 2011 @ 00:31
    Ralph van Vugt
    0

    Wow, Chriztian, thanks! Well worth the time reading it ;-)

    I think I have option 2. My tree looks like this:

    listpage
       - bar 1
    - monday
    - tuesday
    - wednesday
    - etc
       - bar 2
    - monday
    - etc.
      - bar 3

    Depending on the day of the week I the listpage shows a list of what all bars have to offer that day. The XSLT for displaying the items is exactly the same for every day, except the xpath is different.

    <xsl:if test="$weekday = '4'">
       <xsl:for-each select="$currentPage/bar/wednesday [@isDoc]">
           show some stuff from the wednesday pages
       </xsl:for-each>
    </xsl:if>
    <xsl:if test="$weekday = '5'">
        <xsl:for-each select="$currentPage/bar/thursday [@isDoc]">
          show some stuff from the thursday pages 
    </xsl:for-each> 
    </xsl:if>

    f I understand correctly I still have to make a template for every day of the week. My thoughts were to check which day it is, depending on that set a variable and pass that to the xpath. I do not know if that can be done. I tried, but then I get lots of errors ;-)

    What's the best thing to do?

    Thanks for the help!

    Ralph

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jan 20, 2011 @ 01:19
    Chriztian Steinmeier
    0

    Hi Ralph,

    If I understand correctly, you don't actually need separate templates in the XSLT - you just need to select only "mondays", "wednesdays" etc. when you apply them. And you can do this using the name() function:

    <xsl:apply-templates select="$currentPage/bar/*[name() = $weekdays/day[@id = $today]]" />

    (Remember to lowercase the weekdays in the XML variable, if needed - your sample seems to indicate that...)

    Then create a single template for display - have it match any node with a <bar> parent:

    <xsl:template match="bar/*">
        <!-- $today was created at the root level and thus global -->
        <h2><xsl:value-of select="$today" /></h2>
        <p>
            <!-- do stuff -->
        </p>
    </xsl:template>

    The apply-templates instruction makes sure to select the right nodes, and since $today was created outside of the templates, it's global and can be used inside any template.

    /Chriztian

  • Ralph van Vugt 57 posts 101 karma points
    Jan 20, 2011 @ 10:03
    Ralph van Vugt
    0

    Chriztian, 

    You truly are the XSLT king! Thanks a million!

    Ralph

  • Joshua 6 posts 76 karma points
    Nov 08, 2016 @ 10:43
    Joshua
    0

    Hi,

    I want my logo to print only in first and second pages and not in rest of the pages

    I have used the below codes but its printing in first and also in the rest of the pages enter image description here

    enter image description here

    enter image description here

    Could anyone help me to reach out this..

Please Sign in or register to post replies

Write your reply to:

Draft