[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

problem with quotes

I've got some code that pulls data from an xml database and loads it into a request form for users. Works great in most cases, but I run intro trouble if any of the data I'm pulling has quotes in it. Only happens with article titles, but it DOES happen. Here's a code snippet:

use LWP::Simple;
$xml = get $queryStr;
use XML::LibXML;

my $p = XML::LibXML->new;
my $root = $p->parse_string($xml);
 
foreach my $rec ($root->findnodes('/inm:Results/inm:Recordset/*')) {
 local $" = ', ';
 my @authors = map { $_->to_literal } $rec->findnodes('inm:AUTHORS/text()');
 print $ContentType;
 print $Head;
 print "<table><tr><td width=80 valign=top><b>Reference: </b></td><td width=600>@authors: <INPUT NAME=AUTHORS TYPE=hidden VALUE=\"@authors\">";
 
  if ($rec->findvalue('inm:TITLE/text()')){ print $rec->findvalue('inm:TITLE/text()'), ". <INPUT NAME=TITLE TYPE=hidden VALUE=\"", $rec->findvalue('inm:TITLE/text()'),"\">"};
 
  print "<tr><td></td><td></td></tr>\n</table>\n";
}

Specifically, here's where I run into trouble once in a while:

<INPUT NAME=TITLE TYPE=hidden VALUE=\"", $rec->findvalue('inm:TITLE/text()'),"\">"

The TITLE field value gets truncated if the title data I'm pulling has any quotes in it. Can't use single quotes instead because they're a bit more common (in my titles) than the double quotes.

Please advise.

Thanks,

Timothy
0
GessWurker
Asked:
GessWurker
1 Solution
 
jmcgOwnerCommented:
On this line:

if ($rec->findvalue('inm:TITLE/text()')){ print $rec->findvalue('inm:TITLE/text()'), ". <INPUT NAME=TITLE TYPE=hidden VALUE=\"", $rec->findvalue('inm:TITLE/text()'),"\">"};
 
Try doing it like this, instead:

if(  my $title = $rec->findvalue('inm:TITLE/text()')) {
        print $title,
             ". <INPUT NAME=TITLE TYPE=hidden VALUE=\",
             uri_escape( $title),
             "\">"
       };

You'll probably find you need to add a line:

use URI::Escape qw(uri_escape);

at the top of your script.

What this does is encode the double quote as %22 so that it is not misinterpreted as ending the VALUE= attribute.

0
 
harris_cCommented:
remove the quotes first ?
$tmp = $rec->findvalue('inm:TITLE/text()') =~ s/'//g;

hec",)
0
 
GessWurkerAuthor Commented:
jmcg:

Perfect! Thanks. I'll be using this a lot!

--Timothy
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now