Apostrophe woes

steelej's Avatar

steelej

20 Sep, 2012 06:38 PM

My faculty experts guide site was just ready to go when I tried to enter in professor O'Brien and found the apostrophe is killing me. Most of what happens here is keyed off of the last name.

When the photos are uploaded they are named for the last name, so O'Brien comes up as obrien.jpg--thus broken paths to the photo.

The page also errors out when loading due to the apostrophe. I've tried ${_EscapeTool.xml($lname)} but no luck of yet. I've attached the xml feed I'm working from. Can someone wiser than I suggest a solution? I'm after a way for all the references in the page to be to obrien but the Last name display as O'Brien.

  1. 1 Posted by steelej on 20 Sep, 2012 06:41 PM

    steelej's Avatar

    Added the script to the portion of the page that displays the name.

  2. 2 Posted by Ryan Griffith on 20 Sep, 2012 08:00 PM

    Ryan Griffith's Avatar

    Hi,

    It sounds as though the apostrophe is breaking your XPath due to the conflicting single quotes.

    Let's try passing the id variable through the $_EscapeTool.javascript method to escape the quotes in the string:

    #set ($exp = $_XPathTool.selectSingleNode($contentRoot, "//expertfeed/content/system-page/expert[lname='${_EscapeTool.javascript(id.value)}']"))
    

    Please let me know if you have any questions.

    Thanks.

  3. 3 Posted by steelej on 20 Sep, 2012 09:04 PM

    steelej's Avatar

    The immediate problem was a missing $ in the id.value. That corrected, it is still kicking me the same error, presumably in the display of the name.

  4. 4 Posted by steelej on 20 Sep, 2012 09:10 PM

    steelej's Avatar

    Ryan,
    I'm trying this but am getting: Velocity content invalid: Failed to parse String literal at templateValidation[line 2, column 57]

  5. 5 Posted by Ryan Griffith on 21 Sep, 2012 12:48 PM

    Ryan Griffith's Avatar

    Ah, sorry about the typo in the snippet. The issue is definitely the single quote, so here is a version that is comparing two XPath nodes, which will not rely on comparing a node with a string. This should work:

    #set ($idXPath = "/system-index-block/calling-page/system-page/system-data-structure/lname") 
    
    #set ($exp = $_XPathTool.selectSingleNode($contentRoot, "//expertfeed/content/system-page/expert[lname = ${idXPath}]"))
    
    #set ($fname = $exp.getChild("fname").value) 
    #set ($lname = $exp.getChild("lname").value) 
    #set ($title = $exp.getChild("title").value) 
    #set ($dept = $exp.getChild("dept").value)
    
    <div class="article-header"><!--start article-header div-->
    ##  <h1 class="expert-name">${fname} ${_EscapeTool.xml($lname)}</h1>
      <p class="italic">${title}<br />
      ${dept}</p>
    
      <div class="addthis_toolbox addthis_default_style addthis_16x16_style" id="addthis"><!-- Start AddThis Button  -->
        <a class="addthis_button_preferred_1" ></a>
        <a class="addthis_button_preferred_2" ></a>
        <a class="addthis_button_preferred_3" ></a>
        <a class="addthis_button_preferred_4" ></a>
        <a class="addthis_button_preferred_5" ></a>
        <a class="addthis_button_compact" ></a>
      </div>
      <!-- End AddThis Button -->
    </div><!--end article-header div-->
    
  6. 6 Posted by steelej on 21 Sep, 2012 01:32 PM

    steelej's Avatar

    Ryan, thanks. One thing is I realized I had five or six other scripts on this page all using $lname. I tried this article header script will the others turned off and now have O'brien clear as day. I'll adapt the others along this line and should have that working. Now...another problem I'm working on. I create paths to the uploaded photos and to the page using the professor's last name. When the photos are uploaded and the page automatically named they come out as "obrien." I'm trying to make a new variable I can use in that path based on the last name. I tried .replaceAll() to strip the sinigle-quote but it seems to require a replacement character. Do you know of another way I can strip the single-quote (if one exists?)

  7. 7 Posted by steelej on 21 Sep, 2012 01:45 PM

    steelej's Avatar

    I find too, that now that Mr. O'brien's page is operating properly, the two pages that index my expert pages for differing displays are now choking on their scripts--due to his last name. I've tried severl iterations to correct this but am having no luck. If you can give it a look, I'd be much obliged. I've attached the scripts.

  8. 8 Posted by Ryan Griffith on 21 Sep, 2012 01:58 PM

    Ryan Griffith's Avatar

    Hm, to replace the quotes and turn the string to lower-case, you could try something like the following:

    $lname.replaceAll("/[\'|\"]/", "").toLowerCase()

    This should match occurrences of single or double quotes and replace them with nothing, then convert the string to all lower-case.

    One thing to note is that you could potentially run into other characters that may need to be removed. So you could adjust the regular expression in the replaceAll() to maybe match everything that is not an allowed character. The following snippet will match and replace any non-word character (ie parens, quotes, dashes, etc) and then convert the string to lower-case:

    $lname.replaceAll("/\W/", "").toLowerCase()

    You mentioned in a previous discussion that this XML is generated dynamically. An alternative that may save you some time down the road is you could add to your XML something like a slug or identifier. That would provide you with something consistent to match against within your Formats.

  9. 9 Posted by steelej on 24 Sep, 2012 02:45 PM

    steelej's Avatar

    I have retooled this to use a reference # to pull information and name the file.

    I think I have it in hand now. Thanks.

  10. steelej closed this discussion on 24 Sep, 2012 02:45 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