For Each not For Each-ing

Graham Lewis's Avatar

Graham Lewis

13 Aug, 2012 07:12 PM

So question:

I can't seem to my for each to work, when I remove it and just use the variables, the data shows up when I add the for each, it goes away. I am lost as to where to go from here.

Here is my format:

#set ($content = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/general"))
#set ($about = $content.getChild("about") )
#set ($app-materials = $content.getChild("app-materials") )
#set ($admission = $content.getChild("admission") )

#set ($content2 = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/admission"))
#set ($uncond = $content2.getChild("uncond") )
#set ($cond = $content2.getChild("cond") )

#set ($content3 = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/min-req"))
#set ($non-thesis = $content3.getChild("non-thesis") )
#set ($thesis = $content3.getChild("thesis") )

#set ($degree = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/degrees"))
#set ($degree-name = $degree.getChild("degree-name") )
#set ($prefix = $degree.getChild("prefix") )
#set ($courses = $degree.getChild("courses") )



<span class="body_text">
    $_SerializerTool.serialize($about, true)<br /><br />
    
    <h2>Required Application Materials</h2>
    $_SerializerTool.serialize($app-materials, true)<br /><br />
    
    <h2>Admission Requirements</h2>
    $_SerializerTool.serialize($app-materials, true)<br /><br />
    
    <fieldset class="fieldsettype1"><legend class="legendtype1">Unconditional Admission</legend> 
    $_SerializerTool.serialize($uncond, true)
    </fieldset><br /><br />
    
    <fieldset class="fieldsettype1"><legend class="legendtype1">Conditional Admission</legend> 
    $_SerializerTool.serialize($cond, true)
    </fieldset><br /><br />
    
    <h2>Minimum Requirements</h2><br /><br />
    <fieldset class="fieldsettype2"><legend class="legendtype2">Non-Thesis Option</legend> 
     $_SerializerTool.serialize($non-thesis, true)
    </fieldset><br /><br />
    
    <fieldset class="fieldsettype2"><legend class="legendtype2">Thesis Option</legend> 
     $_SerializerTool.serialize($thesis, true)
    </fieldset><br /><br />
    
    <fieldset class="fieldsettype3">
    <h2 class="SectionSubhead"><strong>$_EscapeTool.html($degree-name.value) Courses</strong></h2>
    <p class="SectionSubhead"><strong>Prefix $_EscapeTool.html($prefix.value)</strong></p>
    <p class="SectionSub-Sub-head">&#160;</p>
    <br />
    <table>
        
#foreach ( $course in $courses )   
    #set ($class-number = $course.getChild("number") )
    #set ($class-title = $course.getChild("title") )
    #set ($class-hours = $course.getChild("hours") )
    #set ($class-desc = $course.getChild("desc") )
    <tr>
    <td valign="top"><strong>$_EscapeTool.html($class-number.value).</strong></td>
    <td valign="top"><strong>&#160;&#160;</strong></td>
    <td><strong>$_EscapeTool.html($class-title.value) ($_EscapeTool.html($class-hours.value))</strong>. $_EscapeTool.html($class-desc.value)</td>
    </tr>
      
#end
    </table>
    </fieldset>
</span>

Here is the XML of the page I am trying to reference...

<system-index-block current-time="1344884973949" name="current page" type="folder">
<calling-page>
<system-page current="true" id="20a9c4c17f0000013bbfd45b409d6177">
<name>index2</name>
<is-published>true</is-published>
<last-published-on>1344882738588</last-published-on>
<last-published-by>glewis</last-published-by>
<title>Biology</title>
<display-name>Biology</display-name>
<path>/academics/biology/index2</path>
<site>Graduate Studies 2</site>
<link>site://Graduate Studies 2/academics/biology/index2</link>
<created-by>glewis</created-by>
<created-on>1344872760454</created-on>
<last-modified-by>glewis</last-modified-by>
<last-modified>1344884642842</last-modified>
<system-data-structure definition-path="Graduate Studies - Academic">
<general>
<about>
<p class="NumberedList">The Biology Department offers courses leading to the Master of Science (MS) with a major in biology and supporting courses for the Master of Arts with a major in liberal studies. For students majoring in secondary education with a teaching field in biology or general science, supporting courses are offered for the MS.Ed. and the Ed.S. degrees.</p>
<p class="NumberedList">Students accepted to the MS with a major in biology must meet with the graduate advisor. An Advisory Committee will be selected to assist the student in developing a degree plan that satisfies University and departmental degree requirements.</p>
</about>
<app-materials>
<p align="left">Applicants for the MS with a major in biology may be permitted to enroll for one semester of graduate course work while completing all other general application procedure requirements. </p>
<p align="left">Applicants for the MS with a major in biology must submit the following documentation to the College of Graduate Studies, Jacksonville State University, 700 Pelham Road North, Jacksonville, Alabama 36265-1602, to be considered for admission:</p>
<ol>
<li>Completed 
<a href="http://www.jsu.edu/graduate2/admission/grad_app.html">JSU Graduate Application for Admission</a>
</li>
<li>Non-refundable $30.00 application processing fee</li>
<li>Official transcripts(s) from all postsecondary institutions attended. (Students who have previously attended JSU do not need to request a transcript from the University.)</li>
<li>Official tests scores on the General Test of the GRE or the MAT (please refer to page 15 of this
<em>Bulletin</em>
).</li>
<li>Three "
<a href="http://www.jsu.edu/forms">Graduate Reference Forms</a>
" completed by individuals who can provide qualitative assessment of the applicant’s potential for success in graduate course work.</li>
<li>If English is not the applicant’s native language, the applicant is required to submit an official TOEFL score report, an IELTS score report, or a PTE score report (please refer to page 22 of this 
<em>Bulletin</em>
). </li>
</ol>
</app-materials>
<admission>
<p class="Sectiontext">In addition to meeting the general admission requirements above, applicants for the MS with a major in biology must have an undergraduate major in biology or its equivalent, as determined by the Biology Department, including courses equivalent to Introductory Biology I (BY 101, 103), Introductory Biology II (BY 102, 104), Genetics (BY 322), Ecology (BY 332), and Cell Biology (BY 373). Applicants with majors in other natural sciences or mathematics will be considered for admission; however, additional courses may be required.</p>
<p class="Sectiontext">Applicants must meet one of the following admission formula requirements.  For purposes of computing the undergraduate GPA, a 4.0 grade-point scale is used. The plus (+) and minus (-) grades from undergraduate transcripts are not used in calculating the GPA.</p>
</admission>
</general>
<admission>
<uncond>
<p>To obtain unconditional admission into the MS program with a major in biology, applicants must meet one of the following formula requirements:</p>
<p align="left">(450 x undergraduate GPA) + (total of verbal &amp; quantitative sections of GRE General Test) ≥ 1600 points.</p>
<p align="center" style="text-align: left;">
<strong>OR</strong>
</p>
<p align="center" style="text-align: left;">(15 x undergraduate GPA) + MAT score ≥ 60 points;</p>
</uncond>
<cond>
<p class="Sectiontext">Any applicant failing to meet the requirements for unconditional admission may be conditionally admitted with the recommendation of the graduate faculty in the applicant’s major and approval of the Dean of the College of Graduate Studies.</p>
<p class="Sectiontext">
<strong>Applicants who are granted conditional admission must achieve a GPA of at least 3.0 on the first 12 graduate hours attempted within a twelve-month time frame. Failure to meet these conditions will result in the student being dropped from graduate studies.</strong>
</p>
</cond>
</admission>
<min-req>
<non-thesis>
<p class="Sectiontext">A minimum of 36 graduate semester hours (18 must be at the 500 level), including successful completion of an acceptable original research paper and a comprehensive examination. A minimum GPA of 3.0 on a 4.0 scale in all graduate work in biology is required. The 36 graduate semester hours for the non-thesis option will include the following courses:</p>
<ol>
<li>Four (4) 500 level courses (12 semester hours):  choose from BY 521, 533, 538, 539, 540, 542, or 562;</li>
<li>Two (2) 500 level seminar courses (4 semester hours):  choose from BY 570, 571, 572, 573, 574, 575, 576, or 577</li>
<li>BY 595 Research Project (3 semester hours); and</li>
<li>Seventeen (17) hours of committee approved electives (17 hours).</li>
</ol>
<strong>36 Graduate Semester Hours Required for the Non-Thesis Option</strong>
</non-thesis>
<thesis>
<p class="Sectiontext">A minimum of 30 graduate semester hours (of which half must be at the 500 level), including successful completion of an acceptable thesis and a comprehensive examination. A minimum GPA of 3.0 on a 4.0 scale in all graduate work in biology is required. The 30 graduate semester hours for the thesis option will include the following courses:</p>
<ol>
<li>Two (2) 500 level courses (6 semester hours):  choose from BY 521, 533, 535, 538, 540, 542, 546 or 562;</li>
<li>Two (2) 500 level seminar courses (4 semester  hours): choose from BY 570, 571, 572, 573, 574, 575, 576, or 577;</li>
<li>BY 415G Biometrics (or its equivalent) (3 semester hours);</li>
<li>BY 599 Thesis I and II (6 hours); and</li>
<li>Eleven (11) hours of committee approved electives (11 semester hours).</li>
</ol>
<p align="center" class="Sectiontext" style="text-align: left;">
<strong> </strong>
<strong>30 Graduate Semester Hours Required for the Thesis Option</strong>
</p>
</thesis>
</min-req>
<degrees>
<degree-name>Biology</degree-name>
<prefix>BY</prefix>
<desc/>
<courses>
<number>402G.</number>
<title>Medical Microbiology</title>
<hours>4</hours>
<desc></desc>
</courses>
<courses>
<number>403G</number>
<title>Immunology</title>
<hours>4</hours>
<desc></desc>
</courses>
</degrees>
</system-data-structure>
</system-page>
</calling-page>
</system-index-block>
  1. 1 Posted by Ryan Griffith on 13 Aug, 2012 07:22 PM

    Ryan Griffith's Avatar

    Hi glewis,

    Have you tried using the XPathTool? Something like the following:

    #set ($courses = $_XPathTool.selectNodes($degrees, "courses"))
    

    Let me know if this helps at all.

    Thanks.

  2. 2 Posted by Graham Lewis on 13 Aug, 2012 07:27 PM

    Graham Lewis's Avatar

    I added that in instead of what I had and it didn't work.

  3. 3 Posted by Graham Lewis on 13 Aug, 2012 07:42 PM

    Graham Lewis's Avatar

    This is the URL to the page that is using the format

    http://www.jsu.edu/graduate2/academics/biology/index2.html

  4. 4 Posted by Ryan Griffith on 13 Aug, 2012 07:43 PM

    Ryan Griffith's Avatar

    Small typo in there (used degrees instead of degree, try the following and let me know if that works out:

    #set ($courses = $_XPathTool.selectNodes($degree, "courses"))
    
  5. 5 Posted by Graham Lewis on 13 Aug, 2012 07:46 PM

    Graham Lewis's Avatar

    Perfect! What makes _XPathTool different from EscapeTool?

  6. 6 Posted by Ryan Griffith on 13 Aug, 2012 07:48 PM

    Ryan Griffith's Avatar

    Glad to hear that worked out!

    Do you mean getChild()? XPathTool.selectNodes will provide a node list, whereas I believe getChild() will not if there is more than one node with that name.

    Please let us know if you have any additional questions.

    Have a good one!

  7. Ryan Griffith closed this discussion on 13 Aug, 2012 07:48 PM.

  8. Graham Lewis re-opened this discussion on 13 Aug, 2012 08:11 PM

  9. 7 Posted by Graham Lewis on 13 Aug, 2012 08:11 PM

    Graham Lewis's Avatar

    I guess my question was what was the difference in what I had that wasn't working and what you had that worked.

    Graham Lewis
    Webmaster
    Jacksonville State University

    256.782.5331
    www.jsu.edu

  10. 8 Posted by Graham Lewis on 13 Aug, 2012 08:34 PM

    Graham Lewis's Avatar

    Using the same format and XML I am trying to make it so that the whole chunk with the first "for each" is wrapped in a "for each"

    #foreach ( $degree in $degrees )     
     
        <fieldset class="fieldsettype3">
        <h2 class="SectionSubhead"><strong>$_EscapeTool.html($degree-name.value) Courses</strong></h2>
        <p class="SectionSubhead"><strong>Prefix $_EscapeTool.html($prefix.value)</strong></p>
        <p class="SectionSub-Sub-head">&#160;</p>
        <br />
        <table>
            
    #foreach ( $course in $courses )   
        #set ($class-number = $course.getChild("number") )
        #set ($class-title = $course.getChild("title") )
        #set ($class-hours = $course.getChild("hours") )
        #set ($class-desc = $course.getChild("desc") )
        <tr>
        <td valign="top"><strong>$_EscapeTool.html($class-number.value).</strong></td>
        <td valign="top"><strong>&#160;&#160;</strong></td>
        <td><strong>$_EscapeTool.html($class-title.value) ($_EscapeTool.html($class-hours.value))</strong>. $_EscapeTool.html($class-desc.value)</td>
        </tr>
          
    #end
        </table>
        </fieldset>
    
    #end
    

    When I added the "for each" around everything it disappeared.

  11. 9 Posted by Ryan Griffith on 13 Aug, 2012 08:47 PM

    Ryan Griffith's Avatar

    Hi glewis,

    I guess my question was what was the difference in what I had that wasn't working and what you had that worked.

    I believe the difference is that XPathTool.selectNodes will provide a node list, whereas getChild() will not if there is more than one node with that name (I think it only provides the first child).

    Looking over your Velocity code above, one thing that jumps is the original Velocity does not have a degrees variable set but a degree, so the first foreach will most likely error and not display anything.

    Your sample XML only has one degrees element, but assuming your Data Definition provides the ability to add multiple degrees elements to the page you are applying the Format to and multiple courses per degree, try something like the following:

    #set ($content = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/general"))
    #set ($about = $content.getChild("about") )
    #set ($app-materials = $content.getChild("app-materials") )
    #set ($admission = $content.getChild("admission") )
    
    #set ($content2 = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/admission"))
    #set ($uncond = $content2.getChild("uncond") )
    #set ($cond = $content2.getChild("cond") )
    
    #set ($content3 = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/min-req"))
    #set ($non-thesis = $content3.getChild("non-thesis") )
    #set ($thesis = $content3.getChild("thesis") )
    
    #set ($degrees = $_XPathTool.selectNodes($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/degrees"))
    
    <span class="body_text">
        $_SerializerTool.serialize($about, true)<br /><br />
        
        <h2>Required Application Materials</h2>
        $_SerializerTool.serialize($app-materials, true)<br /><br />
        
        <h2>Admission Requirements</h2>
        $_SerializerTool.serialize($app-materials, true)<br /><br />
        
        <fieldset class="fieldsettype1"><legend class="legendtype1">Unconditional Admission</legend> 
        $_SerializerTool.serialize($uncond, true)
        </fieldset><br /><br />
        
        <fieldset class="fieldsettype1"><legend class="legendtype1">Conditional Admission</legend> 
        $_SerializerTool.serialize($cond, true)
        </fieldset><br /><br />
        
        <h2>Minimum Requirements</h2><br /><br />
        <fieldset class="fieldsettype2"><legend class="legendtype2">Non-Thesis Option</legend> 
         $_SerializerTool.serialize($non-thesis, true)
        </fieldset><br /><br />
        
        <fieldset class="fieldsettype2"><legend class="legendtype2">Thesis Option</legend> 
         $_SerializerTool.serialize($thesis, true)
        </fieldset><br /><br />
        
        #foreach ( $degree in $degrees )     
            #set ($degree-name = $degree.getChild("degree-name") )
            #set ($prefix = $degree.getChild("prefix") )
            #set ($courses = $_XPathTool.selectNodes($degree, "courses") )
            <fieldset class="fieldsettype3">
                <h2 class="SectionSubhead"><strong>$_EscapeTool.html($degree-name.value) Courses</strong></h2>
                <p class="SectionSubhead"><strong>Prefix $_EscapeTool.html($prefix.value)</strong></p>
                <p class="SectionSub-Sub-head">&#160;</p>
                <br />
                <table>
                    
                    #foreach ( $course in $courses )   
                        #set ($class-number = $course.getChild("number") )
                        #set ($class-title = $course.getChild("title") )
                        #set ($class-hours = $course.getChild("hours") )
                        #set ($class-desc = $course.getChild("desc") )
                        <tr>
                        <td valign="top"><strong>$_EscapeTool.html($class-number.value).</strong></td>
                        <td valign="top"><strong>&#160;&#160;</strong></td>
                        <td><strong>$_EscapeTool.html($class-title.value) ($_EscapeTool.html($class-hours.value))</strong>. $_EscapeTool.html($class-desc.value)</td>
                        </tr>
                          
                    #end
                </table>
            </fieldset>
        #end
    </span>
    
  12. Ryan Griffith closed this discussion on 18 Oct, 2012 07:54 PM.

Comments are currently closed for this discussion. You can start a new one.

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