Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1282 posts 2739 karma points
    May 24, 2010 @ 19:02
    Amir Khan
    0

    xsl if array?

    Is there a shorter way of doing if statements than actually doing xsl:if for each one? I want to attach a file to certain pages with a macro based on their node id, it seems like doing an if statement for each one could get a bit sloppy as i continue to add pages / attachments. Is there some other way of approaching this?

  • Tommy Poulsen 514 posts 708 karma points
    May 24, 2010 @ 19:33
    Tommy Poulsen
    0

    I agree - it does not sound like you want to maintain a lot of similar if's. Is it possible for you to add specific id's, properties or doctypes to the individual pages/files to distinguish them from each other.

    >Tommy

  • Amir Khan 1282 posts 2739 karma points
    May 24, 2010 @ 19:54
    Amir Khan
    0

    Is it possible to update an xsl variable from an if statement? That would at least clean up the "business" side from the macro and the if statements could just change the variable...

  • Tommy Poulsen 514 posts 708 karma points
    May 24, 2010 @ 20:02
    Tommy Poulsen
    0

    nope, you cannot change a variable value. But if possible you can make a variable with the if's embedded inside:

     

    <xsl:variable name="myvar">
      <xsl:choose>
        <xsl:when test="blabla"> ... </xsl:when>
        ...
        <xsl:otherwise>...</xsl:otherwise>
    </xsl:variable>

     

    As an arlternative to modify variables you often can do specific calls to tamples (using xsl:call-template) and then pass the new variable value as parameter.

     

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    May 24, 2010 @ 20:51
    Lee Kelleher
    0

    Hi Amir,

    You could try reversing the logic, by adding a new property to the document-type called "hasAttachment" (as a true/false checkbox), then in your XSLT, check if has been set, i.e.

    <xsl:if test="string(data[@alias='hasAttachment']) = '1'">
        ...
    </xsl:if>

    Cheers, Lee.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    May 24, 2010 @ 21:02
    Chriztian Steinmeier
    0

    Hi Amir,

    You can use an XML variable structured to your own liking, and create the necessary output for them, like this:

       <!-- List files to include for specific pages -->
        <xsl:variable name="file-mapper">
            <page id="31415">
                <css>pi.css</css>
                <script>math.js</script>
            </page>
            <page id="1984">
                <css>bigbrother.css</css>
            </page>
        </xsl:variable>
        <!-- Make XPath-navigable node-set from the above variable -->
        <xsl:variable name="files" select="msxml:node-set($file-mapper)" />
    
        <xsl:template match="/">
            <head>
                <xsl:apply-templates select="$files/page[@id = $currentPage/@id]/*" />
            </head>
        </xsl:template>
    
        <xsl:template match="css">
            <link rel="stylesheet" type="text/css" href="{.}" />
        </xsl:template>
    
        <xsl:template match="script">
            <script type="text/javascript"><xsl:comment /></script>
            <!-- (Comment prevents self-closing script tag in output when using the "xml" output method)  -->
        </xsl:template>

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft