Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ampersands in an element 2

Status
Not open for further replies.

RoadRacer

Programmer
Dec 18, 2000
61
0
0
US
What can possibly be done when an element contains an ampersand in it such as this:

<description>Tom & Jerry Cartoon</description>

I'm getting an error from the parser and I can't seem to find a way around it aside from replacing the & with &amp; which is not an option because I can't edit the XML's source ... any ideas? Ed (RoadRacer) Holguin

&quot;I Hate Computers!&quot;
 
Sorry, the html filter killed it. When i showed & the second time i meant to have amp; right after it.

Thanks Ed (RoadRacer) Holguin

&quot;I Hate Computers!&quot;
 
RoadRacer --

I could be mistaken on this, but I believe you can use

<xsl:eek:utput cdata-section-elements='description' blah blah blah>

to solve the problem. If I understand the syntax correctly, that should result in treating all &quot;description&quot; elements in the source data as being CDATA blocks and should protect the &

Perhaps someone else will expand on that.

FWIW -- from the XSLT standard...

<xsl:eek:utput cdata-section-elements=&quot;example&quot;/>
would cause a literal result element written in the stylesheet as

<example><foo></example>
or as

<example><![CDATA[<foo>]]></example>
to be output as

<example><![CDATA[<foo>]]></example>
If the text node contains the sequence of characters ]]>, then the currently open CDATA section should be closed following the ]] and a new CDATA section opened before the >. For example, a literal result element written in the stylesheet as

<example>]]></example>
would be output as

<example><![CDATA[]]]]><![CDATA[>]]></example>
 
Excellent! That was the ticket!

Thanks a ton! I told the mainframe programmer to change the dialogue for the fields who could potentially have controversial characters and add the !CDATA thing-a-ma-bobber and it's set!

Thanks again! Ed (RoadRacer) Holguin

&quot;I Hate Computers!&quot;
 
I think this will help me solve a problem I am having as well...

A further question, as I have to go through my database and find where the suspect characters are likley to lurk, what other characters may give a problem on display if contained as the text of an XML element?

For example, I know that < and > will also need to be treated this way....
 
How are you (or the other programmer) constructing the XML? If you're using string concatenation, you'll have this problem because the ampersands, less-than and greater-than symbols, etc haven't been escaped.

Easiest way is to create your XML via the Node, Element, and Attribute objects your parser provides. All that mess gets taken care of for you then.

Chip H.
 
Hm, I'm doing just that in my VB component which retreives the stuff from the database. e.g.

OAddNode.getElementsByTagName(&quot;Organisatino_name&quot;).Item(0).nodeTypedValue = &quot;Gerry & Mary&quot;

This bit is working fine.

It's when I take the XML output by the component and try to display it in a browser (IE5) I get an error about &quot;A name was started with an invalid character. &quot; also been getting errors about whitespace?
 

Hi

And what if the '&' appears in an attribute?

<Elment value=&quot;bla&bla&quot;/>
 
: can give you some probs...use the ascii - &quot;Oops! I've joined a club that'll have me as a member?&quot; -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top