From: RobG on

I'm working with XML files that sometimes use a default namespace,
unfortunately there doesn't seem to be an elegant way of dealing with
them. In some cases 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?

An alternative is to fix the namespace when building the expression,
which is less elegant than conditionally modifying the expression in
the evaluator function.


--
Rob
From: Ken Snyder on
On Mar 23, 5:32 pm, RobG <rg...(a)iinet.net.au> wrote:
> ...
> ... Is
> there a regular expression that will only modify slashes outside
> square brackets?
> ...

I'm pretty sure a single regular expression can't do that. You could
try splitting the expression at 2 double quotes and replacing slashes
in every other array part. Capturing the split characters is not
supported across browsers, so you would need to use a function from a
library like Prototype that normalizes it.

The splitting expression might look something like this: /("["]+")/

- Ken
From: denisb on
RobG <rgqld(a)iinet.net.au> 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"]
> Is
> there a regular expression that will only modify slashes outside
> square brackets?


Perhaps it would be possible to try:
expr = expr.replace(/((\[.+\/+.+\])?\/+)/g, '$1xx:');



--
@@@@@
E -00 comme on est very beaux dis !
' `) /
|\_ =="
From: Antony Scriven on
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
From: denisb on
Antony Scriven <adscriven(a)gmail.com> wrote:
> expr = expr.replace(/(?![^[]*\])\//g, '$&xx:');

better than mine !

little typo ('+' missing) :
expr = expr.replace(/(?![^[]*\])\/+/g, '$&xx:');
.....................................^



--
@@@@@
E -00 comme on est very beaux dis !
' `) /
|\_ =="