[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Xoath quote escaping

Subject: Re: Xoath quote escaping
From: "Duane Hitz" <dhitz@xxxxxxxxxxxxxxxx>
Date: Sat, 28 Apr 2007 14:57:48 -0600
Re:  Xoath quote escaping
Brilliant! Thanks!

To share:

function escapeXPathQuotes(str) {

       // returns either a double-quoted string or an XPath
       // concat expression with double quotes single-quoted.
       //
 var arr = str.split('"');
       var arr2 = new Array();
 for (var i = 0; i < arr.length; i++) {
           if (arr[i]) {
               arr2.push('"' + arr[i] + '"');
               if (i < arr.length-1) {
                   arr2.push("'\"'");
               }
           } else {
               if (i < arr.length - 1) {
                   arr2.push("'\"'");
               }
           }
    }
 if (arr.length == 1) {
  return arr[0];
 } else {
  return  'concat(' + arr2.join(",") + ")";
 }
}

I'm sure there's a more elegant way of doing that... but it works for a quick fix (tested it only in JScript)...

Per my example below, the expression now becomes:

"//node[" + subNodeName + " = " + escapeXPathQuotes(subNodeValue) + "]"

~Duane

P.S. Sorry 'bout the typo in the subject...


----- Original Message ----- From: "Michael Kay" <mike@xxxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Saturday, April 28, 2007 11:02 AM
Subject: RE: Xoath quote escaping



In XPath 1.0, without variables, you're basically stuck with the XPath
expression

concat("This is some ", '"', "quoted", '"', " content of Ted's")

and then of course you have to backslash-escape the quotes to get them
through Javascript.

Michael Kay
http://www.saxonica.com/


-----Original Message-----
From: Duane Hitz [mailto:dhitz@xxxxxxxxxxxxxxxx]
Sent: 28 April 2007 15:27
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  Xoath quote escaping

All,

I have a situation where I am building an XPath expression in
JavaScript to select nodes from an XML document.  The content
of the nodes could contain double quotes, single quotes or
both at the same time.  It is user-entered data, so I have no
control ultimately over what the content contains.

The content might be (brackets excluded): [This is some
"quoted" content of Ted's]

The dynamically built xpath expression might look like (where
subNodeValue is the above content):

1) "//node[" + subNodeName + " = '" + subNodeValue + "']"

... except for the single quote problem... or:

2) "//node[" + subNodeName + " = \"" + subNodeValue + "\"]"

... except for the double quote problem... or even:

3) "//node[" + subNodeName + " = &quot;" + subNodeValue + "&quot;]"

...except that this just doesn't work.

Any thoughts on how to make this work? Is it possible?

I've read everything I can find on quote escaping (on this list and
elsewhere) and there doesn't appear to be a solution that
covers this case (I know XPath 2 is supposed to allow ""
escaping - unfortunately, we're stuck on IE6+ & MSXML3).

We are dynamically displaying rows in a table that meet the
xpath criteria... setting a @showing attribute and then
re-transforming... so it really has to be in JavaScript.

One option would be to replace (s/"/'/g or s/"/''/g) all
double quotes on the server side when building the XML - and
then use option 2 above.  I don't really want to change
user-entered data. It is, sadly, what I've come down to.  I'm
hoping there's a more elegant solution to this.

Thanks for any help...

Duane

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.