Is it “bad practice” to be sensitive to linebreaks

2019-04-23 12:13发布

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..

12条回答
甜甜的少女心
2楼-- · 2019-04-23 12:36

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.

查看更多
Evening l夕情丶
3楼-- · 2019-04-23 12:37

Yes, I think using a CDATA block would protect the whitespace. Although some parser APIs allow you to preserve whitespace.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-04-23 12:38

I think the only real problem is that it makes the XML harder to read. e.g.

<Something>
    <Contains>
        <An>
            <Address>15 Sample St
Example Bay
Some Country</Address>
        </An>
    </Contains>
</Something>

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.

查看更多
放我归山
5楼-- · 2019-04-23 12:42

What about using attributes to store the data, rather than text nodes:

<Address Street="15 Sample St" City="Example Bay" State="" Country="Some Country"/>

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.

查看更多
Root(大扎)
6楼-- · 2019-04-23 12:44

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

查看更多
别忘想泡老子
7楼-- · 2019-04-23 12:45

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

XML applications often seem to take a cavalier attitude toward whitespace because the rules about the places in an XML document where whitespace doesn't matter sometimes give these applications free rein to add or remove whitespace in certain places.

查看更多
登录 后发表回答