473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

XML String Literals

Hello

I am wondering if anyone knows if there is a way to store string
literals within an XML tag.

For instance I would like to store HTML formatting data for an
attribute but it keeps getting picked up as a tag by the XML parser.

eg...
<name>John</name>
<prettyName><HTML><BR>John</BR></HTML><prettyName>

The second line causes the parser to think <HTML> and <BR> are XML
tags when they are not. I have tried
<prettyName>"<HTML><BR>John</BR></HTML>"<prettyName> and
<prettyName>'<HTML><BR>John</BR>'</HTML><prettyName> with no success.

prettyData should be = <HTML><BR>John</BR></HTML>

not returned as prettyData
<HTML>
<BR> = John </BR>
</HTML>
Does anyone know how to achieve my desired effect without having to
declre tags manually for the html properties that I need, then
reassemble into HTML later?

Kind Regards
A.Hulley
Jul 20 '05 #1
3 8823
Austin wrote:
Hello

I am wondering if anyone knows if there is a way to store string
literals within an XML tag.

For instance I would like to store HTML formatting data for an
attribute but it keeps getting picked up as a tag by the XML parser.

eg...
<name>John</name>
<prettyName><HTML><BR>John</BR></HTML><prettyName>

The second line causes the parser to think <HTML> and <BR> are XML
tags when they are not. I have tried
<prettyName>"<HTML><BR>John</BR></HTML>"<prettyName> and
<prettyName>'<HTML><BR>John</BR>'</HTML><prettyName> with no success.

prettyData should be = <HTML><BR>John</BR></HTML>

not returned as prettyData
<HTML>
<BR> = John </BR>
</HTML>
Does anyone know how to achieve my desired effect without having to
declre tags manually for the html properties that I need, then
reassemble into HTML later?

Kind Regards
A.Hulley


Hi,

as your html-like substructure seems to be well-formed, i suggest to
keep these datas as tags, because many tools will allow to copy the
structure on an output stream. Furthermore, you also will be able to
extract the content data. However, i don't think that html tags should
be present in such structures; it may be useful for documentary sections
of xml documents to use xhtml tags, but useless in your case; many
technologies, such as XSLT will allow you to produce the expected output
like this <HTML><BR>John</BR></HTML> from the input data like this
<prettyName>John<prettyName> with a simple xslt template.

however, < and > may be escaped with &lt; and &gt;
when many escaping is needed use
<![CDATA[ <HTML><BR>John</BR></HTML> ]]> instead
--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------

Jul 20 '05 #2
On 7 Nov 2003 07:11:26 -0800, Au*******@bigfoot.com (Austin) wrote:
I am wondering if anyone knows if there is a way to store string
literals within an XML tag.
Yes, but the definition of "string" has issues with angle brackets.
For instance I would like to store HTML formatting data for an
attribute but it keeps getting picked up as a tag by the XML parser.


There are three ways; namespacing, entity encoding and CDATA sections.
I'd do it by entity encoding.

Namespacing is the easiest and "cleanest" in an XML sense. It's
particularly good for mixing XML elements from multiple schemas. It's
also quite easy to work with from XSLT.

Some people, mainly old SGML hands, have arguments against
namespacing. Try Googling comp.infosystems.www.authoring.html for
comments from Arjun Ray.

The biggest problem with namespacing is that it requires all
components to be well-formed XML. Fragments must also be balanced
fragments. This is no problem with XHTML, but it's a minor hassle
with HTML and it can be very difficult if you have to accept any HTML
(which can be badly malformed) from other sources.

Entity encoding is how it's done in RSS. You would probably find
looking at RSS useful here. Entities which are awkward as "string"
characters in XML [<, >, &] are represented by their entity
equivalents

Your example would look like this:
<name>John</name>
<prettyName>&lt;HTML&gt;&lt;BR&gt;John &amp;
Jane&lt;/BR&gt;&lt;/HTML&gt;<prettyName>

The main advantage of entity encoding is that it's simple to do,
although it requires some string-handling tools, like regexes. You
can't do this in XSLT (practically) but you can do it easily by
calling JavaScript extensions from within XSLT.

Be careful to track what is encoded and what isn't. You can safely
double-encode HTML (ampersands simply expand to "&amp;amp;") but you
must de-encode it afterwards by the _same_ number of operations.
CDATA sections are perhaps "The SGMLWay", but personally I find entity
encoding easier to work with. You wrap your literal string in a
wrapper that says "This is not XML, just treat it literally"
Your example would look like this:
<name>John</name>
<prettyName><![CDATA[ <HTML><BR>John</BR></HTML>]]><prettyName>

Remember to also replace the sequence "]]>" inside the string with
"]]>]]&gt;<![CDATA[ " . You can't "escape" this sequence, but you
can concatenate two CDATA sections around it. It's a rare problem to
encounter, but if you ever handle the content of comp.text.xml through
XML tools, then you're going to meet it !

--
Die Gotterspammerung - Junkmail of the Gods
Jul 20 '05 #3
Thank you to everyone who replied. :-).
You information was most helpful.

Kind Regards
A.Hulley
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
17
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
8
by: junky_fellow | last post by:
what would be the output for the following piece of code ? if ( "hello" == "hello" ) printf("True\n"); else printf("False\n"); What is the reason for that ?
6
by: copx | last post by:
Can you / are you supposed to free() string literals which are no longer needed? In my case I've menu construction code that looks like this: menu_items = list_new(); list_add(menu_items,...
41
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
8
by: arnuld | last post by:
i tried to output these 2 to the std. output: const std::string hello = "Hello"; const std::string message = hello + ", world" + "!"; const std::string exclam = "!"; const std::string...
4
by: =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire | last post by:
Hello all, I'm learning C and I still am struggling to understand some basic concepts. For example, I read in the standard that with functions such as strcpy, 'If copying takes place between...
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
7
by: lithiumcat | last post by:
Hi, I'm not yet very confident in my use of standard terminology, so please be kind if I'm mis-calling something, I will do my best no to make it again once pointed out. I'm wondering what is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.