是“不好的做法"吗?对XML文档中的换行敏感? [英] Is it "bad practice" to be sensitive to linebreaks in XML documents?

查看:96
本文介绍了是“不好的做法"吗?对XML文档中的换行敏感?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一些XML文档,当涉及到地址部分时,我会看到如下片段:

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>

我将其转换为XHTML的XSLT具有一些时髦的递归模板,可以将字符串中的换行符转换为< br/>.标签.

The XSLT that I have for converting this to XHTML has some funky recursive template to convert newline characters within strings to <br/> tags.

一切正常.但是依靠XML文档中的换行符被认为是不好的做法"吗?如果是这样,建议我改为这样做吗?

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

推荐答案

通常认为依靠换行符是不好的做法,因为这是区分数据的脆弱方法.尽管大多数XML处理器会保留您放入XML中的所有空格,但并不能保证.

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.

真正的问题是,大多数将XML输出为可读格式的应用程序都考虑XML内所有可互换的空白,并且可能会将这些换行符折叠到一个空格中.这就是为什么您的XSLT必须跳过这种箍以正确呈现数据的原因.使用"br"标签将极大地简化转换.

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.

另一个潜在的问题是,如果您在XML编辑器中打开XML文档并进行漂亮打印,则很可能会丢失这些换行符.

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.

如果您确实继续使用换行符,请确保在地址"中添加一个xml:space ="preserve"属性. (如果您正在使用DTD,则可以在DTD中执行此操作.)

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

一些建议阅读的内容

  • XML.com上的文章说,以下:
  • An article from XML.com says the following:

XML应用程序似乎经常需要花费大量时间. 骑士对空白的态度 因为关于地方的规则 一个XML文档,其中空格 有时候给这些都没关系 应用程序免费添加或 删除某些地方的空白.

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.

  • 有关空白的XSL列表帖子的集合.
    • A collection of XSL-list posts regarding whitespace.
    • 这篇关于是“不好的做法"吗?对XML文档中的换行敏感?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆