Solved

problem with quotes

Posted on 2003-10-29
3
146 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

828 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