开发者

Escape an apostrophe from a data binding's XML

开发者 https://www.devze.com 2023-01-30 07:50 出处:网络
I have a string from xml with an apostrophe that should be escaped to &apos; and it is not. <city place=\"park\'s place\"/>

I have a string from xml with an apostrophe that should be escaped to &apos; and it is not.

<city place="park's place"/>

In html I am grabbing the value.

<span datafld="place"></span>

I need the value in place to be "park&apos;s place" and not park's place.开发者_运维百科 Currently it shows "park's place".

I have spent a good amount of time trying to find an answer and can't seem to find one.

This code example is badly hacked together since I am not allowed to show any original code.

Thanks.

Edit: This is on a xhtml page using javascript.


In the XML "data model" all values are unescaped. So whether your attribute was specified as:

place="park's place"

or:

place="park&apos;s place"

or:

place="park&#39;s place"

when you use an XML parser (or the DOM) you'll get "park's place". (Things like "innerHTML" are an exception to this general rule.)

If you need to compare that to some other string that has a different level of escaping then you either need to escape the string you get from the DOM, or you need to unescape the other string. It's a lot like if you were going to compare a measurement in meters to one in feet: you need to convert to a common unit-of-measurement/level-of-escaping.

I'd go with the unescaping approach if you can. If that isn't possible then you'll need to make sure that you escape in a consistent way everywhere, which can be difficult. Note that I've shown you three different ways of legally escaping that particular string -- and there are many many more.

0

精彩评论

暂无评论...
验证码 换一张
取 消