I'm generating some XML documents and when it comes to the address part I have fragments that look like this:
<Address>15 Sample St
Example Bay
Some Country</Address>
The XSLT that I have for converting this to XHTML has some funky recursive template to convert newline characters within strings to <br/> tags.
This is all working fine; but is it considered "bad practice" to rely on linebreaks within XML documents? If so, is it recommended that I do this instead?
<Address><Line>15 Sample St</Line>
<Line>Example Bay</Line>
<Line>Some Country</Line></Address>
Seems like it'd be really awkward to wrap every place where my text may be multiple lines with tags like that..
What you really should be doing is converting your XML to a format that preserves white-space.
So rather than seek to replace \n with <br /> you should wrap the whole block in a <pre>
That way, your address is functionally preserved (whether you include line breaks or not) and the XSTL can choose whether to preserve white-space in the result.
Yes, I think using a CDATA block would protect the whitespace. Although some parser APIs allow you to preserve whitespace.
I think the only real problem is that it makes the XML harder to read. e.g.
If pretty XML isn't a concern, I'd probably not worry about it, so long as it's working. If pretty XML is a concern, I'd convert the explicit newlines into
<br />
tags or\n
before embedding them in the XML.What about using attributes to store the data, rather than text nodes:
I know the use of attributes vs. text nodes is an often debated subject, but I've stuck with attributes 95% of the time, and haven't had any troubles because of it.
If you need your linebreaks preserved, use a CDATA block, as tweakt said
Otherwise beware. Most of the time, the linebreaks will be preserved by XML software, but sometimes they won't, and you really don't want to be relying on things which only work by coincidence
It's generally considered bad practice to rely on linebreaks, since it's a fragile way to differentiate data. While most XML processors will preserve any whitespace you put in your XML, it's not guaranteed.
The real problem is that most applications that output your XML into a readable format consider all whitespace in an XML interchangable, and might collapse those linebreaks into a single space. That's why your XSLT has to jump through such hoops to render the data properly. Using a "br" tag would vastly simplify the transform.
Another potential problem is that if you open up your XML document in an XML editor and pretty-print it, you're likely to lose those line breaks.
If you do keep using linebreaks, make sure add an xml:space="preserve" attribute to "address." (You can do this in your DTD, if you're using one.)
Some suggested reading