atom feed - velocity - stumped

Sarah Johnson Li's Avatar

Sarah Johnson Li

09 Jan, 2015 07:29 PM

Hello,

I've tried to get this feed to work for a while, and I'm stumped. I haven't had any trouble with any other type of feed except this one. It's part of a page that uses velocity. Is it just me?

https://www.rollinsjobs.com/all_jobs.atom

I tried a simple script based on post in http://help.hannonhill.com/discussions/velocity-formats/96-velocity...

#set ( $feed = $_XPathTool.selectSingleNode($contentRoot, "feed") )
#set ( $entries = $_XPathTool.selectNodes($feed, "entry") )
<!-- $feed.getName() -->
<!-- #set ( $entries = $feed.getChildren() ) -->
<!-- $entries.size() -->

<div class="contentBox from-format">

#if ( $entries.size() > 0 )
    <ul>
        #foreach ( $item in $entries )
            #if ( $item.getName() == "entry" )
                #set ( $elements = $item.getChildren() )
                #foreach ( $e in $elements )
                    #if ( $e.getName() == "title" ) 
                        #set ( $title = $e )
                    #end 
                #end
                <li>$_EscapeTool.xml($title.value)</li>
            #end
        #end
    </ul>
#end

</div>
  1. 1 Posted by Wing Ming Chan on 09 Jan, 2015 08:20 PM

    Wing Ming Chan's Avatar

    Hi, Sarah,

    "feed" is the root element. Therefore, you need to start your code like this:

    #set ( $feed = $_XPathTool.selectSingleNode( $contentRoot, "/feed" ) )
    

    I tested the code and it works.

    Wing

  2. 2 Posted by Sarah Johnson L... on 09 Jan, 2015 09:15 PM

    Sarah Johnson Li's Avatar

    Hi Wing!

    I made that little change, but it's still not coming through for me when I preview it while editing the format. I'm currently on v7.10.2 of cascade server. Not sure if that makes a difference or not.

    Under Preview Options, I chose the feed block containing the feed above(https://www.rollinsjobs.com/all_jobs.atom), which shows perfectly fine

    In the Velocity, I have this script with your change:

    #set ( $feed = $_XPathTool.selectSingleNode( $contentRoot, "/feed" ) )
    #set ( $entries = $_XPathTool.selectNodes($feed, "entry") )
    <!-- $feed.getName() -->
    <!-- #set ( $entries = $feed.getChildren() ) -->
    <!-- $entries.size() -->
    
    <div class="contentBox from-format">
    
    #if ( $entries.size() > 0 )
        <ul>
            #foreach ( $item in $entries )
                #if ( $item.getName() == "entry" )
                    #set ( $elements = $item.getChildren() )
                    #foreach ( $e in $elements )
                        #if ( $e.getName() == "title" ) 
                            #set ( $title = $e )
                        #end 
                    #end
                    <li>$_EscapeTool.xml($title.value)</li>
                #end
            #end
        </ul>
    #end
    
    </div>
    

    In the format window after I hit Test Format, I still get this:
    <!-- $feed.getName() -->
    <!-- -->
    <!-- 0 -->

  3. 3 Posted by Wing Ming Chan on 09 Jan, 2015 09:19 PM

    Wing Ming Chan's Avatar

    Sarah,

    I am not into the habit of testing code like this. Instead, I just plug the block and format to a page region and view the page instead. I can see this result:

    <div class="contentBox from-format">
    <ul>
    <li>Visiting Assistant Professor, Chemistry</li>
    <li>Behavioral Psychologist/Therapist (Associate or Assistant Professor)</li>
    <li>Applied Behavior Analysis (Program Director; Associate or Full Professor)</li>
    <li>International Student Advisor </li>
    <li>Visiting Assistant Professor, Biostatistics and Genetics</li>
    <li>Visiting Assistant Professor, Environmental Studies</li>
    <li>Visiting Artist-in-Residence or Lecturer, Theatre </li>
    <li>Graduate Assistant: Office of Community Standards & Responsibility</li>
    <li>Associate/Full Professor, Operations Management</li>
    <li>I.T. Inventory Student Assistant</li>
    <li>Work Study - Florida Literacy Coalition Student Assistant</li>
    <li>Work Study - I.T. User Services </li>
    <li>Work Study - Theater: Costumes</li>
    <li>Bookstore Sales Associate</li>
    <li>Work Study- Theatre Lighting Production Assistant</li>
    <li>Student Resident Assistant (RA) - 2015/2016 Academic Year</li>
    <li>Work Study - External/Competitive Scholarships Office</li>
    <li>Summer Event Coordinator</li>
    <li>Work Study - Provost's Office Student Assistant</li>
    <li>Work Study - Theater Crowd Control</li>
    <li>Work Study - Child Development Center</li>
    <li>Sandspur - Page Designer</li>
    <li>Sandspur - Section Editor</li>
    <li>Painter</li>
    <li>Work Study - Jewish Studies</li>
    <li>Work Study - Theater Scene Shop</li>
    </ul>
    </div>
    

    Wing

  4. 4 Posted by Ryan Griffith on 12 Jan, 2015 02:08 PM

    Ryan Griffith's Avatar

    Hi Sarah,

    It sounds like you're running into an issue with the namespace that is being added to the feed, which is causing issues when you attempt to access the elements by their name. From what I am seeing when dumping $contentRoot.getContent() is an Atom namespace without a prefix, which can be a pain to work with.

    Here is what I would do:

    #set ( $entries = $_XPathTool.selectNodes($contentRoot, "//*[local-name() = 'entry']") )
    
    <div class="contentBox from-format">
    
    #if ( $entries.size() > 0 )
        <ul>
            #foreach ( $item in $entries )
                #set ( $ns = $item.getNamespace() )
                #set ( $title = $item.getChild("title", $ns) )
                <li>$_EscapeTool.xml($title.value)</li>
            #end
        </ul>
    #end
    
    </div>
    

    This will obtain all Elements with the local name of entry (so without the namespace here); this also saves you from needing the extra loop to traverse the children. To summarize, in the loop we're using the usual getChild method, but with the namespace of the entry Element so we can access it by just title. More information about these Element methods can be found here.

    Another way to write this using the XPath Tool would be:

    #foreach ( $item in $entries )
        #set ( $title = $_XPathTool.selectSingleNode($item, "*[local-name() = 'title']") )
        <li>$_EscapeTool.xml($title.value)</li>
    #end
    

    Please let me know if you have any questions.

    Thanks!

  5. 5 Posted by Ryan Griffith on 12 Jan, 2015 02:11 PM

    Ryan Griffith's Avatar

    Also, here is a slightly more efficient version of the first example:

    #set ( $entries = $_XPathTool.selectNodes($contentRoot, "//*[local-name() = 'entry']") )
    
    <div class="contentBox from-format">
    
    #if ( $entries.size() > 0 )
        #set ( $ns = $entries.get(0).getNamespace() )
        <ul>
            #foreach ( $item in $entries )
                #set ( $title = $item.getChild("title", $ns) )
                <li>$_EscapeTool.xml($title.value)</li>
            #end
        </ul>
    #end
    
    </div>
    

    Since all entries have the same namespace, we only need to save it once.

  6. 6 Posted by Sarah Johnson L... on 12 Jan, 2015 07:04 PM

    Sarah Johnson Li's Avatar

    Thank you Ryan! I knew there had to be something off with the feed. I NEVER had a problem before. I'll give these solutions a try and report back if it works.

  7. 7 Posted by Sarah Johnson L... on 12 Jan, 2015 07:12 PM

    Sarah Johnson Li's Avatar

    Ryan, it worked! All I had to do to get the entries was...

    #set ( $entries = $_XPathTool.selectNodes($contentRoot, "//*[local-name() = 'entry']") )
    

    That's all it took!!! THANK YOU!!!

    This feed comes from PeopleAdmin, which I know other schools use for hiring and recruitment. I'm sure not I'm not the first or last to deal with feeds from them.

  8. 8 Posted by Ryan Griffith on 12 Jan, 2015 08:16 PM

    Ryan Griffith's Avatar

    Thank you for following up, Sarah. I am glad to hear the adjustment did the trick.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  9. Ryan Griffith closed this discussion on 12 Jan, 2015 08:16 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

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM