473,383 Members | 1,861 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,383 software developers and data experts.

XML Brainteaser

Preface to the is problem: I am coding in a framework that blends jsp
and xml in a heathen manner. Meaning I have to nearly sacrifice a
virgin to get anything to work.

I want to load the character '<' into a java string. Only problem is
that that string is inside a bit of XML. And to compound the problem
the string is sent to a browser as part of an html page. The line
something looks like: (with a '_' in where the '<' needs be)

out.print("_title>Hello, World_/title>");

We were aiming to have the title of the page be "Hello, World". The
problem is that if the blank is filled with '<' the servlet fails to
compile because there is an XML error and nothing is output where this
should be in the html source. If the blank is filled with "&ltf;" then
java interprets this as 5 characters. This outputs to the screen the
phrase:

<title>Hello, World</title>

and the source of the html page looks exactly like the Java string:

&ltf;title>Hello, World&ltf;/title>

Any idea how to get a '<' into a java string inside XML?

(The solution that I am using involved virgin sacrifice, we used some
custom tags from the framework. Somthing along the lines of <c:title
value="Hello, World"> )

Mar 30 '06 #1
6 1345
> Any idea how to get a '<' into a java string inside XML?

The right answer would simply be &lt; or < or &#x3c;, all of which
are ways that XML escapes the < character. If you're accessing your XML
through any of the standard tools/APIs, that does the Right Thing.

If you really insist on needlessly sacrificing virgins -- if you won't
accept anything but a dumb cut-and-paste of the Java directly into the
XML at a textual level -- well, that's a Bad Idea, but that's what CDATA
Sections are for. The XML fragment
<foo><![CDATA[out.print("<title>Hello, World</title>");]]></foo>
is semantically identical to
<foo>out.print("&lt;title>Hello, World&lt;/title>");</foo>

Of course it's up to your system to manage the CDATA section delimiters.
And there are limitations; if your Java code ever contains the ]]>
sequence the workarounds are arguably uglier than individually escaping
the < characters would have been.
custom tags from the framework. Somthing along the lines of <c:title
value="Hello, World"> )


That isn't necessarily inferior, depending on what you're doing.

For a completely different approach, you might want to take a look at
IBM's patent describing the BML Scripting Pages concept, which embeds
scripts written in the Bean Markup Language into documents written in
other XML-based languages. There are some significant advantages to
having both the surrounding markup and the script be written in XML;
among other things, it means one pass through a parser can deal with both.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 30 '06 #2
in message <wa********************@comcast.com>, Joe Kesselman
('k*************@comcast.net') wrote:
Any idea how to get a '<' into a java string inside XML?


The right answer would simply be &lt; or < or &#x3c;, all of which
are ways that XML escapes the < character. If you're accessing your XML
through any of the standard tools/APIs, that does the Right Thing.

If you really insist on needlessly sacrificing virgins -- if you won't
accept anything but a dumb cut-and-paste of the Java directly into the
XML at a textual level -- well, that's a Bad Idea, but that's what
CDATA Sections are for. The XML fragment
<foo><![CDATA[out.print("<title>Hello, World</title>");]]></foo>
is semantically identical to
<foo>out.print("&lt;title>Hello, World&lt;/title>");</foo>


Strictly,

<foo>out.print("&lt;title&gt;Hello, World&lt;/title&gt;");</foo>

The unescaped right angle bracket is just as verboten as the unescaped
left angle bracket.

--
si***@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

;; All in all you're just another nick in the ball
-- Think Droid

Mar 30 '06 #3
Simon Brooke wrote:
The unescaped right angle bracket is just as verboten as the unescaped
left angle bracket.


Actually, no. XML can easily distinguish whether > is intended to be
part of a tag or part of text, by simply looking at whether it is
currently in the middle of reading a tag. It is *common* to escape text
instances as &gt; so human readers can determine this more easily, but
never necessary... and there is a "for compatibility" requirement that
text instances of ]]> be escaped so there's no risk that an SGML
processor would misread them as ending CDATA Sections.

See http://www.w3.org/TR/2004/REC-xml11-20040204/#syntax

Note also that inside attribute values < does not need to be escaped,
because the fact that it's a quoted string means there's no risk of it
being misinterpreted. But there's no harm in escaping it, and once again
you may want to do so if you expect humans to be reading your XML document.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 30 '06 #4
In article <Fa********************@comcast.com>,
Joe Kesselman <ke************@comcast.net> wrote:
Note also that inside attribute values < does not need to be escaped,
because the fact that it's a quoted string means there's no risk of it
being misinterpreted.


Actually they *must* be escaped, even though there is no possibility
of misinterpretation:

http://www.w3.org/TR/2004/REC-xml11-...#CleanAttrVals

One practical advantage of this is that missing close-quotes on
attribute values are often detected nearer to the location of the
error.

-- Richard
Mar 30 '06 #5
Richard Tobin wrote:
Actually they *must* be escaped, even though there is no possibility
of misinterpretation:

http://www.w3.org/TR/2004/REC-xml11-...#CleanAttrVals


.... True. And the same appears in XML 1.0. OK, that's tie score again.
<grin/>

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Mar 30 '06 #6
Thanks for your help! The application was frozen today for demoing. So
I try these out next week and let you know how they worked.

Mar 30 '06 #7

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

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
23
by: Mark (News) | last post by:
I'm not really sure where to post this question as it covers so many platforms, but as the platform isn't relevant, here goes... I'm trying to (pulling my hair out more like) construct a regular...
1
by: WillWeGetOurFreedomBack | last post by:
When I try to run the query below, I get a warning that says Access could not insert ANY of the records due to Validation rule violations. Access gives me NO hint WHICH fields are violating the...
7
by: worldbadger | last post by:
Hello, We had a competition of guessing the M&M's in 25 different jars. The database has a name, guess, date and time field in it (as well as the ID field). What I need to do now is get the...
34
by: Christopher Benson-Manica | last post by:
I'm trying to compute the absolute value of an integer using only bitwise operators... int x; sscanf( "%d", &x ); printf( "%d\n", (x^((~((x>>31)&1))+1)) + ((x>>31)&1) ); That works, but it...
5
by: John | last post by:
Hi, I can't find a simple example for a simple(?) problem. I am working on an application with a variable in form1, that variable is needed in form2 for a calculation but i can't get that...
3
by: Robby | last post by:
Here's a brainteaser: I have some legacy C code that I cannot modify for various reasons. I would like to be able to wrap this code into a C++ class so that I can create multiple objects to...
13
by: Walton | last post by:
I'm wondering if anyone knows or knows where to find a collection of javascript "brain teasers" for lack of a better description. Companies use these types of tests for programmers all the time...
4
by: Rasmus Kromann-Larsen | last post by:
As written previously, I'm currently writing a master thesis on JavaScript - hence running into weird stuff in JS. I'm sure the task will be easy for most regulars, but it's slightly tricky...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.