From: Dr J R Stockton on
In comp.lang.javascript message <66446dfb-e9cb-4d82-8146-377423e48aa8(a)k4
g2000prh.googlegroups.com>, Tue, 23 Mar 2010 16:32:24, RobG
<rgqld(a)iinet.net.au> posted:
>
>Modifying the attribute value means that the result will be wrong. Is
>there a regular expression that will only modify slashes outside
>square brackets?
>
>An alternative is to fix the namespace when building the expression,
>which is less elegant than conditionally modifying the expression in
>the evaluator function.

If you in fact only need to modify / where not somewhere preceded by [,
you could probably split on [, RegExp-alter [0], and rejoin.

expr = '/LandXML/Parcels/Parcel[@name="79a/SP199095"]'
S = expr.split("[")
S[0] = S[0].replace(/(\/+)/g,'$1xx:');
expr = S.join("[")

gave '/xx:LandXML/xx:Parcels/xx:Parcel[@name="79a/SP199095"]'.

Or you could use, which might be more flexible,

expr = '/LandXML/Parcels/Parcel[@name="79a/SP199095"]'
S = "" ; X = 0
for (J=0 ; J<expr.length ; J++) { C = expr.charAt(J) ; S += C
if (C == "[") X++ ;
else if (C == "]") X-- ;
else if (C == "/" && X<=0) S += "xx:" }
expr = S

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE8 FF3 Op10 Sf4 Cr4
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
From: Antony Scriven on
On Mar 24, 3:51pm, ne...(a)no-log.org (denisb) wrote:

> Antony Scriven <adscri...(a)gmail.com> wrote:
>
> [...]
>
> little typo ('+' missing) :
> expr = expr.replace(/(?![^[]*\])\/+/g, '$&xx:');
> ....................................^

Ah yes, I missed that part of the Rob's specification,
thanks. --Antony
From: RobG on
On Mar 25, 1:09 am, Antony Scriven <adscri...(a)gmail.com> wrote:
> On Mar 23, 11:32pm, RobG wrote:
>
>  > [... ] I need to modify part of the expression to include
>  > a random namespace, e.g. change:
>  >
>  > /LandXML/Parcels/Parcel
>  >
>  > into something like:
>  >
>  > /xx:LandXML/xx:Parcels/xx:Parcel
>  >
>  > Sometimes the expression starts with // so I've been using the
>  > following regular expression:
>  >
>  >  expr = expr.replace(/(\/+)/g,'$1xx:');
>  >
>  > which works fine in most cases. However, sometimes the expression
>  > includes an attribute value that has slashes. In that case, I don't
>  > want to modify the attribute value's slash. e.g. at the moment,
>  >
>  > /LandXML/Parcels/Parcel[@name="79a/SP199095"]
>  >
>  > is converted to:
>  >
>  > /xx:LandXML/xx:Parcels/xx:Parcel[@name="79a/xx:SP199095"]
>  >
>  > Modifying the attribute value means that the result will be wrong.
> Is
>  > there a regular expression that will only modify slashes outside
>  > square brackets?
>
> expr = expr.replace(/(?![^[]*\])\//g, '$&xx:');
>
> Can your attribute value contain square brackets or escaped
> quotation marks? --Antony

It is for a general XPath expression evaluator, so I don't want to
have any restrictions on attribute vaules other than those specified
by XML.

--
Rob
From: RobG on
On Mar 25, 12:30 am, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk>
wrote:
> In comp.lang.javascript message <66446dfb-e9cb-4d82-8146-377423e48aa8(a)k4
> g2000prh.googlegroups.com>, Tue, 23 Mar 2010 16:32:24, RobG
> <rg...(a)iinet.net.au> posted:
>
>
>
> >Modifying the attribute value means that the result will be wrong. Is
> >there a regular expression that will only modify slashes outside
> >square brackets?
>
> >An alternative is to fix the namespace when building the expression,
> >which is less elegant than conditionally modifying the expression in
> >the evaluator function.
>
> If you in fact only need to modify / where not somewhere preceded by [,
> you could probably split on [, RegExp-alter [0], and rejoin.
>
>         expr = '/LandXML/Parcels/Parcel[@name="79a/SP199095"]'
>         S = expr.split("[")
>         S[0] = S[0].replace(/(\/+)/g,'$1xx:');
>         expr = S.join("[")
>
> gave '/xx:LandXML/xx:Parcels/xx:Parcel[@name="79a/SP199095"]'.
>
> Or you could use, which might be more flexible,
>
>         expr = '/LandXML/Parcels/Parcel[@name="79a/SP199095"]'
>         S = "" ; X = 0
>         for (J=0 ; J<expr.length ; J++) { C = expr.charAt(J) ; S += C
>           if        (C == "[") X++ ;
>             else if (C == "]") X-- ;
>             else if (C == "/" && X<=0) S += "xx:" }
>         expr = S

I'd though of something along those lines, however I think I'll deal
with it at the expression builder stage, then pass a variable to
indicate whether a default namespace is being used or not.

It doesn't matter for IE (as it doesn't know what namespaces are
anyway), but Firefox is another matte.

--
Rob
From: Antony Scriven on
On Mar 24, 10:38 pm, RobG wrote:

> On Mar 25, 1:09 am, Antony Scriven <adscri...(a)gmail.com> wrote:
>
> > On Mar 23, 11:32pm, RobG wrote:
>
> > > [...]
> > >
> > > expr = expr.replace(/(\/+)/g,'$1xx:');
> > >
> > > [...]
> > >
> > > /LandXML/Parcels/Parcel[@name="79a/SP199095"]
> > >
> > > is converted to:
> > >
> > > /xx:LandXML/xx:Parcels/xx:Parcel[@name="79a/xx:SP199095"]
> > >
> > > Modifying the attribute value means that the result
> > > will be wrong. Is there a regular expression that
> > > will only modify slashes outside square brackets?
>
> > expr = expr.replace(/(?![^[]*\])\//g, '$&xx:');
>
> > Can your attribute value contain square brackets or
> > escaped quotation marks? --Antony
>
> It is for a general XPath expression evaluator, so
> I don't want to have any restrictions on attribute vaules
> other than those specified by XML.

Now you're moving the goalposts. I've no idea what you're
really trying to accomplish, but it now sounds that you
might be better off writing a proper parser rather than
messing about with regexps. --Antony