scripting help

steelej's Avatar

steelej

Aug 26, 2011 @ 03:03 PM

I have a data definition which allows input of listings of professional experience. I'm accessing it thusly:

#set ($experiences = $_XPathTool.selectNodes($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/experience/listing"))

And with entries it displays nicely in format. What I want to do is, if no listings are entered, to have no display at all. What I'm trying is this:

#if ($experiences.size() > 0) <h3>Professional Experience</h3>

  `  <ul>`

#foreach ($experience in $experiences)

 ` <li>$_SerializerTool.serialize($experience, true)`
 ` </li>`
  `#end`

</ul> #else #end

What I find is if no listing is made, it still prints the H3 heading with an empty bullet below. An example xml page is attached.

What have I missed?

  1. 1 Posted by Shiv on Aug 26, 2011 @ 03:21 PM

    Shiv's Avatar

    Well, $experiences.size is greater than zero -- there is one /system-index-block/calling-page/system-page/system-data-structure/experience/listing node, even though it's empty. So, $experiences.size() = 1.

    You could first check the value of the first found node. If it's empty, do nothing.

    #set ( $firstListing = $_XPathTool.selectSingleNode( $contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/experience[1]/listing" ).text )
    
    #if ( $firstListing )
        #set ( $experiences = $_XPathTool.selectNodes( $contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/experience/listing" ) )
        <h3>Professional Experience</h3>
        <ul>
            #foreach ( $experience in $experiences )
                <li>$_SerializerTool.serialize( $experience, true )</li>
            #end
        </ul>
    #end
    
  2. 2 Posted by Charlie Holder on Aug 26, 2011 @ 03:31 PM

    Charlie Holder's Avatar

    Alternatively you could add a filter to your XPath statement to only select non-empty listing nodes.

    #set ($experiences = $_XPathTool.selectNodes($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/experience[listing!='']/listing"))

  3. 3 Posted by Shiv on Aug 26, 2011 @ 03:34 PM

    Shiv's Avatar

    Charlie's answer is better than mine.

  4. 4 Posted by steelej on Aug 26, 2011 @ 03:40 PM

    steelej's Avatar

    I knew I was picking up an empty node but didn't know the syntax to restrict to non-empty.

    Your fix worked great Charlie. Thanks.

  5. steelej closed this discussion on Aug 26, 2011 @ 03:40 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac