如何在XML属性中保存换行符? [英] How to save newlines in XML attribute?

查看:289
本文介绍了如何在XML属性中保存换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将包含换行符的内容保存在某些XML属性中,而不是文本中. 应该选择该方法,以便我能够在XSLT 1.0/ESXLT/XSLT 2.0中对其进行解码

I need to save content that containing newlines in some XML attributes, not text. The method should be picked so that I am able to decode it in XSLT 1.0/ESXLT/XSLT 2.0

最好的编码方法是什么?

What is the best encoding method?

请提出/提出一些想法.

Please suggest/give some ideas.

推荐答案

在兼容的DOM API中,您无需执行任何操作.只需将实际的换行符保存到属性中,API就会自行对其正确编码(请参见

In a compliant DOM API there is nothing you need to do. Simply save actual newline characters to the attribute, the API will encode them correctly on its own (see Canonical XML spec, section 5.2).

如果您进行自己的编码(即在保存属性值之前用
替换\n),则API会再次对输入 进行编码,从而在XML文件中生成


If you do your own encoding (i.e. replacing \n with 
 before saving the attribute value), the API will encode your input again, resulting in 
 in the XML file.

最底线是,字符串值按原样保存.您可以放出自己所放的东西,而无需干预.

Bottom line is, the string value is saved verbatim. You get out what you put in, no need to interfere.

但是……某些实施方式不兼容.例如,它们将在属性值中编码&字符,但忘记了换行符或制表符.这使您处于失败的位置,因为您不能简单地预先用
替换换行符.

However… some implementations are not compliant. For example, they will encode & characters in attribute values, but forget about newline characters or tabs. This puts you in a losing position since you can't simply replace newlines with 
 beforehand.

这些实现将保存未编码的换行符,如下所示:

These implementations will save newline characters unencoded, like this:

<xml attribute="line 1
line 2" />

在解析此类文档时,属性中的文字换行符被规范化为一个空格(同样,按照规范),因此它们会丢失.

Upon parsing such a document, literal newlines in attributes are normalized into a single space (again, in accordance to the spec) - and thus they are lost.

在这些实现中,在属性中保存(和保留!)换行符是不可能的.

Saving (and retaining!) newlines in attributes is impossible in these implementations.

这篇关于如何在XML属性中保存换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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