Selecting nodes from a data definition
I am learning Velocity and having a tough time understanding the methods used to select xml nodes, traversing and iterating over a group within fields created within a Data Definition. I am trying to display the value of "student-year" and "student-term" and iterate through the courses and credits within the group "student-term".
Here is my Data Definition:
<system-data-structure>
<text identifier="program-name" label="Program Name" required="true"/>
<text multi-line="true" identifier="program-description" label="Program Description" required="true"/>
<group identifier="course-sequence" label="Course Sequence">
<group identifier="student-year" label="Student Year" multiple="true" maximum-number="4">
<text type="multi-selector" identifier="year" label="Year">
<selector-item value="Freshman" selected="true"/>
<selector-item value="Sophomore"/>
<selector-item value="Junior"/>
<selector-item value="Senior"/>
</text>
<group identifier="student-term" label="Student Term" multiple="true" maximum-number="3">
<text type="multi-selector" identifier="term" label="Term">
<selector-item value="Fall" selected="true"/>
<selector-item value="Winter"/>
<selector-item value="Spring"/>
</text>
<text identifier="course-1" label="Course 1" required="true"/>
<text identifier="course-1-credit" label="Course 1 Credit" required="true"/>
<text identifier="course-2" label="Course 2" required="true"/>
<text identifier="course-2-credit" label="Course 2 Credit" required="true"/>
<text identifier="course-3" label="Course 3" required="true"/>
<text identifier="course-3-credit" label="Course 3 Credit" required="true"/>
<text identifier="course-4" label="Course 4" required="true"/>
<text identifier="course-4-credit" label="Course 4 Credit" required="true"/>
<text identifier="course-5" label="Course 5" required="true"/>
<text identifier="course-5-credit" label="Course 5 Credit" required="true"/>
</group>
</group>
</group>
</system-data-structure>
And here is my Velocity format:
#set ( $page = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page") ) #set ( $data = $page.getChild("system-data-structure") )
#set ( $progName = $data.getChild("program-name") ) #set ( $progDesc = $data.getChild("program-description") ) #set ( $courseSequ = $_XPathTool.selectNodes($data,"course-sequence") )
#set ( $year = $_XPathTool.selectSingleNode($data, "year/value") ) #set ( $term = $_XPathTool.selectSingleNode($data, "term/value") ) #set ( $course = $_XPathTool.selectNodes($data, "course[.!='']") )
#courseSequence ( $year, $term )
#macro ( courseSequence $yr $trm )
<ul>
#foreach ( $crs in $trm )
#set ( $courseName = $crs.getChild("course-1") )
<li>$courseName.value</li>
#end
</ul>
#end
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
1 Posted by Wing Ming Chan on 17 Jun, 2016 03:23 PM
Hi,
Before I try to offer help, I want one thing clarified. About the data definition you provided, is it a data definition associated with a content type, or is it used by data definition blocks? it does not look like one for pages, because it is too simple. It does not look like one for data definition blocks, because in your Velocity code you dealt with
calling-page
. How should I understand your data definition?Wing
2 Posted by voltmer1 on 17 Jun, 2016 03:31 PM
As it stands now, the data definition is not associated with a content type, but I didn't think it had to be when I am just trying to learn. It is tied to a calling page block on a section of a page template.
Eventually, I would like the macro to iterate over each student-year group and display each student-term group with it's containing courses. Just trying to get anything to display now.
3 Posted by Wing Ming Chan on 17 Jun, 2016 03:35 PM
I assume that by "on a section of a page template", you mean that the block is attached to a region at the page level. Correct me if I am wrong.
What do you mean by "It is tied to a calling page block"? Since the calling-page block is an index block, how do you tie a data definition with an index block?
There reason why I ask these questions: I want to duplicate your setup so that I can write the Velocity format.
Wing
4 Posted by voltmer1 on 17 Jun, 2016 03:41 PM
I am not tying the data definition itself to the calling page block. I meant to say that I had a content region with a calling block and my Velocity format on a page template that uses the Data Definition. Does that make sense?
5 Posted by Wing Ming Chan on 17 Jun, 2016 03:51 PM
Let me try this. Go to Cascade. Look at the data definition, and click the Relationships tab on the top. What do you see?
One more thing. On my site, you can find Velocity Tutorial. You might want to try that, though the tutorial goes very deep into Velocity.
Wing
6 Posted by voltmer1 on 17 Jun, 2016 03:59 PM
It shows my content type of "Programs of Study". This has the data definition that the page template is based off of. Am I doing something wrong? There are so many parts to remember.
7 Posted by Wing Ming Chan on 17 Jun, 2016 04:47 PM
OK, try the following:
Wing
8 Posted by voltmer1 on 17 Jun, 2016 06:36 PM
Perfect Wing!
I didn't think about using an array in the foreach either. Is that syntax something specific to VTL? I hadn't seen it in Java or C#.
9 Posted by Wing Ming Chan on 17 Jun, 2016 06:48 PM
[1..5]
is a java.util.ArrayList<E> object. The syntax of#foreach
is similar to Java's counterpartfor( int num : nums )
.Wing
10 Posted by voltmer1 on 17 Jun, 2016 07:37 PM
Thanks Wing!
I've tried to add the functionality to list all the additional terms in the year, but I am not quite getting it. It is pulling only the first term and repeating it for the number of actual "student-term" groups I have entered on my page.
11 Posted by Wing Ming Chan on 17 Jun, 2016 08:04 PM
You are not looping through the correct object:
Wing
12 Posted by voltmer1 on 17 Jun, 2016 08:40 PM
For some reason, this only pulls the first term of each year.
13 Posted by Wing Ming Chan on 17 Jun, 2016 09:03 PM
I forgot that
student-term
is also a multiple field. Then when you retrieve the elements, usegetChildren( "student-term" )
instead. Of course you need to loop through the elements you retrieve. Can I leave this to you?Wing
14 Posted by voltmer1 on 20 Jun, 2016 02:05 PM
Wing, I've tried, but I can't seem to get more than 2 terms to display for some reason. Currently it only iterates through the years, of which I have created 2. So you are right, It needs to iterate through the "student-terms" as well, I just can't get it to happen.
15 Posted by Wing Ming Chan on 20 Jun, 2016 02:16 PM
Try this:
I removed the size test because a multiple field must have at least one instance.
Wing
16 Posted by voltmer1 on 20 Jun, 2016 02:32 PM
Thanks for all your help Wing! It's working correctly now!
I believe I will have to practice writing a few more of these.
Bradley Wagner closed this discussion on 20 Jun, 2016 03:15 PM.
voltmer1 re-opened this discussion on 20 Jun, 2016 08:52 PM
17 Posted by voltmer1 on 20 Jun, 2016 08:52 PM
I decided that my field "program-description" needed to be a WYSIWYG, but none of the formatting is working when it's pulled in. Do I need to change something in my format?
18 Posted by Ryan Griffith on 22 Jun, 2016 12:53 PM
Hi,
I noticed your latest comment is a duplicate of this discussion you opened up recently. To help avoid confusion, I am going to go ahead and close this one out in favor of the other.
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 about your initial problem.
Have a great day!
Ryan Griffith closed this discussion on 22 Jun, 2016 12:53 PM.