Solved

problem with quotes

Posted on 2003-10-29
3
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

707 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