How do I use getStructuredDataNode() ?

espanae's Avatar

espanae

04 May, 2015 06:14 PM

I can't seem to access the first text field named "link" in the attached data definition, using $currentPage.getStructuredDataNode("newsandevent/item[1]/headline").

Originally, we had this code with a "current page XML" index block:

#set ($link = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/newsandevents/item[1]/link"))
<a href src="${link.value}">some text</a>

But when I try this code, Cascade returns link: ${link.value}:

#set ($link= $currentPage.getStructuredDataNode("newsandevent/item[1]/link") )
link: ${link.value}

Similarly, these tests return The object is null.

$_PropertyTool.outputProperties($currentPage.getStructuredDataNode("newsandevent/item[1]/headline") )
`$_PropertyTool.outputProperties($currentPage.getStructuredDataNode("newsandevent/item[1]/headline").textValue )

I'm currently on Cascade 7.12.4.

  1. 1 Posted by Ryan Griffith on 04 May, 2015 06:52 PM

    Ryan Griffith's Avatar

    Hi Erik,

    Although the getStructuredDataNodes method will accept an XPath-like syntax, you won't be able to use item[1] to filter down to a specific position like you would with XPath.

    Instead, you would want to use something like the following to get the link field from the first newsandevents/item group:

    #set ($link= $currentPage.getStructuredDataNodes("newsandevents/item").get(0).getChild("link") )
    $link.textValue
    

    Note: The node path you used in your sample snippet was slightly off for the newsandevents group identifier and you need to use getStructuredDataNodes when obtaining nodes set to multiple.

    Alternatively, you can also use the bracket notation when accessing the array of item nodes:

    #set ($link= $currentPage.getStructuredDataNodes("newsandevents/item")[0].getChild("link") )
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by espanae on 05 May, 2015 02:48 PM

    espanae's Avatar

    Thanks Ryan. That did the trick.

    I'd been following the example in Cascade Server Hidden Gems Series: The Velocity Tool Edition, where it states that the getStructuredDataNode methods expect an XPath expression for which Structured Data Node(s) you want to reference. Perhaps that should say path instead of XPath to avoid confusion.

    Also, could you provide a brief example of how to use the class method or property returned by:

    $_PropertyTool.outputProperties( $currentPage.getStructuredDataNodes("newsandevents/item") )
    
    Object type: [Lcom.hannonhill.cascade.api.asset.common.StructuredDataNode;
    Properties:
     - class: Class
    
  3. 3 Posted by Ryan Griffith on 05 May, 2015 02:58 PM

    Ryan Griffith's Avatar

    Hi Erik,

    Definitely a good point there on the wording. I'll see if we can make an adjustment to the blog post.

    Regarding the property Class, I don't think there is anything truly useful there. Because the returned value of getStructuredDataNodes is an array of StructuredDataNode objects, you would want to output the properties of one of the array's elements:

    $_PropertyTool.outputProperties( $currentPage.getStructuredDataNodes("newsandevents/item").get(0) )
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by Wing Ming Chan on 05 May, 2015 03:05 PM

    Wing Ming Chan's Avatar

    Hi Erik,

    The Java Class class is related to Java reflection. In fact, this is the key to Java programming in Velocity. If you are interested in this topic, see Java Programming in Velocity.

    Wing

  5. 5 Posted by Wing Ming Chan on 05 May, 2015 03:29 PM

    Wing Ming Chan's Avatar

    Erik,

    I also want to bring your attention to the similarity between web services and Velocity in this area. I have been talking about fully qualified identifiers in a structured data object. When you access a node using web services, you plug in such an identifier. In Velocity, it is almost exactly the same. Every time when you see ";0" or ";1" in an identifier, you need to used get(0) or get(1) to get the node. I hope that you know what I am talking about.

    Wing

  6. 6 Posted by espanae on 05 May, 2015 05:31 PM

    espanae's Avatar

    Guys, thanks for clarifying about Class.

    Wing, I do remember you talking about fully qualified identifiers but ";0" or ";1" doesn't ring a bell. Are those web services references?

  7. 7 Posted by Wing Ming Chan on 05 May, 2015 05:43 PM

    Wing Ming Chan's Avatar

    Erik,

    The notion of fully qualified identifier is one of my inventions in my web service library used to deal with data definition and structured data. Such an identifier can be used to uniquely identify a field in a data definition or a node in a structured data. The digital part of such an identifier signifies the absolute position of a node in a set of nodes in a structured data, instantiating the same field in the corresponding data definition. For detailed discussion, see DataDefinition, and Working With Fully Qualified Identifiers.

    Wing

  8. 8 Posted by espanae on 05 May, 2015 07:32 PM

    espanae's Avatar

    Thanks for the links, Wing. I'll check them out.

  9. espanae closed this discussion on 05 May, 2015 07:32 PM.

  10. Wing Ming Chan re-opened this discussion on 06 May, 2015 03:09 PM

  11. 9 Posted by Wing Ming Chan on 06 May, 2015 03:09 PM

    Wing Ming Chan's Avatar

    Here are two pages documenting structured data and metadata with respect to Velocity, with class API and example code:

    Structured Data
    Metadata

    Wing

  12. espanae closed this discussion on 11 May, 2015 03:32 PM.

  13. espanae re-opened this discussion on 12 May, 2015 01:59 PM

  14. Ryan Griffith closed this discussion on 12 May, 2015 02:11 PM.

  15. espanae re-opened this discussion on 12 May, 2015 04:15 PM

  16. 10 Posted by espanae on 12 May, 2015 04:15 PM

    espanae's Avatar

    I can't figure out the syntax to read the value of a checkbox-item (same data definition). I got as far as the parent node but don't know what to append to that?

    #set ( $announcement = $currentPage.getStructuredDataNode("callout") )
    #if ( $announcement.getChild("active").getChild("value") == 'Yes' )
                ${callout.getChild("title").textValue}
                ${callout.getChild("content").textValue}
    #end
    

    I also tried:
    #if ( $announcement.getChild("active").value == 'Yes' ) and that doesn't recognize when the box is checked either.

    Here's the page XML:

    <system-data-structure definition-path="Homepage/Home Page Layout 1.4">
        <callout>
            <active>
                <value>Yes</value>
            </active>
            <title>Upcoming Events</title>
                <content>
                    <p>
                        <strong>Union on Tour</strong>
                    </p>
                    <p>
                        <a href="http://www.union.edu">Test and sample text goes here.</a>
                    </p>
                </content>
        </callout>
    </system-data-structure>
    

    Any help would be much appreciated!

  17. 11 Posted by Wing Ming Chan on 12 May, 2015 05:30 PM

    Wing Ming Chan's Avatar

    Hi Erik,

    Since you are working with a checkbox, textValue won't work and value is not even an identifier. You have to use textValues, which is a String array. You need to check if the array contains elements, and if it does, whether it contains "Yes". Of course you can also check the size of the returned array. If the size is larger than 0, then it must contain "Yes" (because this is the only option). Note that when "Yes" is checked, the size is actually 2, not 1. (textValues[0] stores an empty String).

    So you want something like this:

    #set ( $announcement = $currentPage.getStructuredDataNode("callout") )
    #if ( $announcement.getChild("active").textValues.size() > 0 )
        ## proceed
    #end
    

    To actually get at the value, you need something like $announcement.getChild("active").textValues[1], assuming the array is not empty.

    Wing

  18. 12 Posted by Wing Ming Chan on 12 May, 2015 08:01 PM

    Wing Ming Chan's Avatar

    Erik,

    I have been reading the posts in Velocity and it suddenly dawns on me that you are mixing two things:

    1. Velocity can be used to transform block data (page data is from the calling-page index block). This assumes that we are dealing with block XML directly (through the JDOM object $contentRoot) and not using locator tool or $currentPage. Here you will see elements like <value> (as shown in you page XML). This value element has nothing to do with $currentPage. Here we are dealing with raw XML. The methods you can use are those provided in org.jdom.Element. For example, Element.getValue().

    2. Velocity can be used alone without any block data. In this case, we can use the $_ (the LocatorTool object) to look for a page or a block, or use $currentPage to access the page. When $currentPage is used, you are dealing with a Java object, specifically, a PageAPIAdapter object. When you deal with Java objects and call getStructuredDataNode, you need to stick with the API.

    I assume that you are not working with a block. But when you post the page XML, you are still mixing the two, because the page XML does not help at all when dealing with getStructuredDataNode. The raw XML is not available to the Java object. And if you check the API documentation, there is no getValue() method or value property in StructuredDataNode.

    Your data definition does help though. I looked at the definition and figured out the structure of the structured data object.

    Wing

  19. Ryan Griffith closed this discussion on 03 Jun, 2015 05:53 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