469,946 Members | 1,958 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

escaping in attributes

I need to escape e.g. & within an attribute, as in:

<foo bar="[ ,\&]+$"/>

Since this is a regular expression and will not be validated as a
normal xml file, i cannot use &amp; for &. CDATA does not seem to work
in an attribute either. Any suggestions on how I can get around this?

-Gisle-

Apr 21 '06 #1
11 1541
* gi*****@gmail.com wrote in comp.text.xml:
I need to escape e.g. & within an attribute, as in:

<foo bar="[ ,\&]+$"/>

Since this is a regular expression and will not be validated as a
normal xml file, i cannot use &amp; for &. CDATA does not seem to work
in an attribute either. Any suggestions on how I can get around this?


You can use &amp; and such; if you really cannot use &amp; then you've
not provided information to determine what you could use instead. In any
case your question would not be an XML question, since with XML &amp; is
guranteed to work.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Apr 21 '06 #2

gi*****@gmail.com wrote:
I need to escape e.g. & within an attribute, as in:

<foo bar="[ ,\&]+$"/>

Since this is a regular expression and will not be validated as a
normal xml file, i cannot use &amp; for &.


You can't store "regular expressions" in attributes. What you can
store is "a regular expression encoded in XML". The way you do this is
to encode & as &amp; It might be hard to figure out the mechanics of
just how you encode this, but it _is_ simple and easy to explain the
rule for doing it (the practice of doing it manually is as perverse
and error-prone as ever).

If this freaks you, try storing it through a DOM and then getting the
DOM to serialise it. Entity encoding as &amp; is a serialisation
factor, not something you're exposed to when working directly with
Infoset or the DOM properties.

Apr 21 '06 #3
gi*****@gmail.com wrote:
Since this is a regular expression and will not be validated as a
normal xml file, i cannot use &amp; for &.


Wrong assumption. Any XML API you use to read the document should
convert &amp; back to the & character, which means that by the time you
see the RE it will be correct.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Apr 21 '06 #4
Well, I just wanted to make sure there wasn't any way one could use
CDATA or something similar in a xml file in order to preserve the
original syntax, but still make it validate.

I think that is a xml question.

Apr 21 '06 #5
gi*****@gmail.com wrote:
Well, I just wanted to make sure there wasn't any way one could use
CDATA or something similar in a xml file in order to preserve the
original syntax, but still make it validate.


<![CDATA[]]> sections are an alternative to character-by-character
escaping, but they really don't have any advantage except for humans
cut-and-pasting data into XML source. As I said, any XML-aware tool will
take care of converting between & and &amp; for you.
--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Apr 21 '06 #6
gisl...@gmail.com wrote:
Since this is a regular expression and will not be validated as a
normal xml file, i cannot use &amp; for &.

Wrong assumption. Any XML API you use to read the document should
convert &amp; back to the & character, which means that by the time you

see the RE it will be correct
----
Well, yes. But the document won't be read by a xml api.

Apr 21 '06 #7
gi*****@gmail.com wrote:
Well, yes. But the document won't be read by a xml api.


Then either fix that, or make the tool which reads it aware of XML
escaping, or -- last resort -- invent your own escaping syntax which the
tool will be aware of.

XML is XML, and should be allowed to be XML.

Or maybe you don't want XML...?

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Apr 21 '06 #8
To wrap it up if i get it correct:

There is no way of preserving a attribute value like :

<foo bar="[ ,\&]+$"/>

by using a cdata or any other tools which can encapsulate the regular
expression, and not change its internal syntax?

-Gisle-

Apr 21 '06 #9
gi*****@gmail.com wrote:
There is no way of preserving a attribute value like :
<foo bar="[ ,\&]+$"/>
by using a cdata or any other tools which can encapsulate the regular
expression, and not change its internal syntax?


That is correct.
--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Apr 21 '06 #10
gi*****@gmail.com wrote:
gisl...@gmail.com wrote:
Since this is a regular expression and will not be validated as a
normal xml file, i cannot use &amp; for &.

Wrong assumption. Any XML API you use to read the document should
convert &amp; back to the & character, which means that by the time you

see the RE it will be correct
----
Well, yes. But the document won't be read by a xml api.


So why are you encoding it in XML?

///Peter
Apr 23 '06 #11
gi*****@gmail.com wrote:
To wrap it up if i get it correct:

There is no way of preserving a attribute value like :

<foo bar="[ ,\&]+$"/>

by using a cdata or any other tools which can encapsulate the regular
expression, and not change its internal syntax?


No. A CDATA section is for character data content (of elements), not
for attribute values. If you were using character data content you
could of course use

<foo><![CDATA[[ ,\&]+$]]></foo>

but you would need to maintain a check that your RE *never* used the
string "]]>" as those three characters (it would have to be ]]&gt;)
because they are required as the literal which terminates the CDATA
section markup.

But as other posters have already said, just make & into &amp; and it
will work in XML. If you're not using XML to handle the result, don't
encode it in XML in the first place.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Apr 23 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dave Moore | last post: by
5 posts views Thread by bobbyballgame | last post: by
4 posts views Thread by sankofa | last post: by
5 posts views Thread by Lucian Sandor | last post: by
11 posts views Thread by Geoff Caplan | last post: by
3 posts views Thread by Taras_96 | last post: by
3 posts views Thread by Nathan Sokalski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.