matching system-page/title to page variable to pull data from index block

lauren.fraser's Avatar

lauren.fraser

04 Jun, 2015 03:48 PM

We have a content index block that indexes all of our locations.

On our individual service line pages, we'd like to use that index block to pull in the links to the locations those services are available at.

I believe the issue with my format is the way I'm setting the $locs variable - specifically, how I'm referencing the title.

#set ( $locs = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/system-page[('title') and value='${locMatch}']") ) I've also tried:
#set ( $locs = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[('title') and value='${locMatch}']") )

We have a similar format that uses our dynamic metadata to match providers to a location/service line and those are working. This is the way we are setting the $loc variable on those pages:
#set ( $locs = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[dynamic-metadata[(name='primary-location' or name='secondary-location') and value='${locMatch}']]") )

I've attached the full format and a sample of our index block xml.

Thanks in advance for the help.

  1. 1 Posted by Ryan Griffith on 08 Jun, 2015 01:25 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    You have the right idea here, but the XPath syntax to filter by value for a static metadata field is slightly different than that of a dynamic metadata field.

    When you have a moment, give the following a try and let me know how it works out:

    #set ( $locs = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/system-page[title = '${locMatch}']") )
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by lauren.fraser on 08 Jun, 2015 03:11 PM

    lauren.fraser's Avatar

    Ryan,
    I'm still not getting any results. I added a line to escape the $locMatch variable just to make sure I had that correct, but I'm still not getting any results with my $locs "match."
    Thanks,
    Lauren

  3. 3 Posted by Ryan Griffith on 08 Jun, 2015 07:47 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    I just noticed you are most likely trying to return multiple locations, but you are using selectSingleNode. Have you tried the following by chance?

    #set ( $locs = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[title = '${locMatch}']") )
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by lauren.fraser on 08 Jun, 2015 08:11 PM

    lauren.fraser's Avatar

    That got it. I’d tried SelectNodes before – but I wasn’t calling the title correctly.

  5. 5 Posted by Ryan Griffith on 08 Jun, 2015 08:25 PM

    Ryan Griffith's Avatar

    Thank you for following up, Lauren. 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!

  6. Ryan Griffith closed this discussion on 08 Jun, 2015 08:25 PM.

  7. lauren.fraser re-opened this discussion on 24 Jun, 2015 08:57 PM

  8. 6 Posted by lauren.fraser on 24 Jun, 2015 09:05 PM

    lauren.fraser's Avatar

    Ryan,
    I'm trying to get the page to pull multiple locations.

    In our data definition, I set the location-match field to allow multiples and entered a second location on a test page. This is the xml

    <xml>
    <system-data-structure>
    <location-match>Ash Grove Family Medical Center</location-match>
    <location-match>Bolivar Family Care Center</location-match>
    <title>Family Medicine Locations</title>
    <image type="file">
    <content/>
    <path>/_common/images/specialties/family-medicine/family-medicine-4.png</path>
    <link>site://1.citizensmemorial.com/_common/images/specialties/family-medicine/family-medicine-4.png</link>
    <site>1.citizensmemorial.com</site>
    <name>family-medicine-4.png</name>
    </image>
    <intro>Family medicine services are provided at the following location(s)</intro>
    </system-data-structure>
    </xml>
    
    I'm not seeing the second location on our page. This is how I've set the $locs variable:
    #set ( $locMatch = $page.getStructuredDataNode("location-match").textValue )
    #set ( $locs = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[title = '${locMatch}']") )
    
    I'm assuming I need to change something with my $locs variable. Possibly adding in a #foreach statement, since the $locMatch variable has multiple entries/values, that need to be matched with the correct data in the index block. (Instead of one $locMatch value that is matched with multiple items in the index block).

    I'm guessing I could manually build out separate location match fields in our data definition (location-match2, location-match3), then check if those are empty and adjust the velocity as needed.

    If there's a way though, I'd rather keep the single location-match field in our data definition and allow multiples, so the data definition and format don't need to be adjusted if we add a new location.

  9. 7 Posted by Ryan Griffith on 25 Jun, 2015 12:33 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    You are correct. The best way to accomplish this would be to loop over each $locMatch and build out an XPath statement to be used for your $locs variable. You will also need to use the getStructuredDataNodes method since you are returning multiple fields instead of just one.

    I didn't have a chance to test this, but something like the following for starters:

    #set ( $locsXPath = "" )
    #set ( $locMatch = $page.getStructuredDataNodes("location-match") )
    
    #foreach ($lm in $locMatch)
        #set ( $locsXPath = "${locsXPath}title = '${lm.textValue}'" )
        #if ( $foreach.hasNext ) #set ( $locsXPath = "${locsXPath} or " ) #end
    #end
    
    #set ( $locs = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[${locsXPath}]") )
    

    Please let me know if you have any questions.

    Thanks!

  10. 8 Posted by lauren.fraser on 25 Jun, 2015 02:21 PM

    lauren.fraser's Avatar

    Ryan,
    That worked great! A quick semi-related question. Further in my down in my velocity script I have the for each loop (#foreach $loc in $locs)the displays the location information. I added this code to the end of it:
      #if ($locs.size() > 1)
    <div class="divider-mobile"></div>
                    #end

    It works exactly like I’d expect, adding the div tag in each instance. How can I check to for the last $loc and not output the divider-mobile div there?

    Thanks,
    Lauren

  11. 9 Posted by Ryan Griffith on 25 Jun, 2015 06:01 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    I believe the following should do the trick:

    #if ($locs.size() > 1 && $foreach.hasNext)
    

    For additional information about the $foreach variable, feel free to check out this page on our Knowledge Base (under the Velocity 1.7 improvements section).

    Please let me know if you have any questions.

    Thanks!

  12. 10 Posted by lauren.fraser on 26 Jun, 2015 07:30 PM

    lauren.fraser's Avatar

    Ryan,
    That worked great. Thanks!

  13. 11 Posted by Ryan Griffith on 26 Jun, 2015 08:29 PM

    Ryan Griffith's Avatar

    Not a problem at all, Lauren, I am glad to hear the proposed change 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!

  14. Ryan Griffith closed this discussion on 26 Jun, 2015 08:29 PM.

  15. lauren.fraser re-opened this discussion on 10 Jul, 2015 04:17 PM

  16. 12 Posted by lauren.fraser on 10 Jul, 2015 04:20 PM

    lauren.fraser's Avatar

    Ryan,
    We've been asked to implement multiple locations on a page that matches the location-match field to dynamic metadata fields.

    I tried editing the code you provided, but I'm not getting something right in the way I'm defining the $locsXPath. I commented out the way it was defined when we were matching it to a system meta data field. Then tried using the XPath for when we were just matching on location-match field, but I'm not getting any results.

    #set ( $locsXPath = "" )
    #set ( $locMatch = $page.getStructuredDataNodes("location-match") )
    #foreach ($lm in $locMatch)
        ##set ( $locsXPath = "${locsXPath}title = '${lm.textValue}'" )
        #set ($locsXPath = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[dynamic-metadata[(name='primary-location' or name='secondary-location') and value='${locMatch}']]") )
        #if ( $foreach.hasNext ) #set ( $locsXPath = "${locsXPath} or " ) #end
    #end
    #set ( $locs = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[${locsXPath}]") )
    

    Thanks in advance for your help.

  17. 13 Posted by Ryan Griffith on 13 Jul, 2015 12:43 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    To confirm, are you trying to match on the primary-location and secondary-location dynamic metadata fields instead of title? If so, you would want something similar to the original code, but instead of title, you need to match on the value of the dynamic fields.

    When you have a moment, let's give the following a try and see how it works out:

    #set ( $locsXPath = "" )
    #set ( $locMatch = $page.getStructuredDataNodes("location-match") )
    #foreach ($lm in $locMatch)
        ##set ( $locsXPath = "${locsXPath}value = '${lm.textValue}'" )
        #if ( $foreach.hasNext ) #set ( $locsXPath = "${locsXPath} or " ) #end
    #end
    #set ( $locs = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[dynamic-metadata[(name='primary-location' or name='secondary-location') and (${locsXPath})]]") )
    

    Please let me know if you have any questions.

    Thanks!

  18. 14 Posted by lauren.fraser on 13 Jul, 2015 04:49 PM

    lauren.fraser's Avatar

    I got this:

    An error occurred while rendering asset preview: org.apache.velocity.exception.MethodInvocationException: Invocation of method 'selectNodes' in  class org.jdom.xpath.JaxenXPath threw exception org.jdom.JDOMException: Invalid XPath expression: "/system-index-block/system-page[dynamic-metadata[(name='primary-location' or name='secondary-location') and ( or  or  or  or  or  or  or  or  or  or )]]": Unexpected ')' at velocityTransform-1436805348341[line 9, column 28]
    

    Attached are sample xml from the index block, the page xml and the complete format.

  19. 15 Posted by lauren.fraser on 13 Jul, 2015 04:51 PM

    lauren.fraser's Avatar

    Sorry, I posted the wrong Format. Attached is the updated version

  20. 16 Posted by Ryan Griffith on 13 Jul, 2015 06:30 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    My apologies, I forgot to uncomment the setting of $locsXPath in my code snippet based on your provided code. When you have a moment, please try the following:

    #set ( $locsXPath = "" )
    #set ( $locMatch = $page.getStructuredDataNodes("location-match") )
    #foreach ($lm in $locMatch)
        #set ( $locsXPath = "${locsXPath}value = '${lm.textValue}'" )
        #if ( $foreach.hasNext ) #set ( $locsXPath = "${locsXPath} or " ) #end
    #end
    #set ( $locs = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[dynamic-metadata[(name='primary-location' or name='secondary-location') and (${locsXPath})]]") )
    

    Please let me know if you have any questions.

    Thanks!

  21. Tim closed this discussion on 11 Aug, 2015 07:07 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