Solved

problem with quotes

Posted on 2003-10-29
3
142 Views
Last Modified: 2010-03-05
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
Comment
Question by:GessWurker
3 Comments
 
LVL 20

Accepted Solution

by:
jmcg earned 500 total points
ID: 9647526
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
 
LVL 8

Expert Comment

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

hec",)
0
 

Author Comment

by:GessWurker
ID: 9651614
jmcg:

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

--Timothy
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now