sql - 为什么在 SQL Server 中修改 XML 属性时会删除回车符?

标签 sql sql-server carriage-return sqlxml

在 SQL Server 2014 中,我尝试使用 XML 数据类型的“修改”方法添加一个带有属性(包含回车符)的 XML 元素。 回车被删除 - 这是为什么?

例子:

declare @xmldata xml

select 
    @xmldata = '<root><child myattr="carriage returns &#xD;&#xA; are not a problem"></child></root>'

set 
   @xmldata.modify('insert <child>modifying text with carriage returns works&#xD;&#xA;ok</child> after (//child)[1]')

set 
   @xmldata.modify('insert <child myattr="but not&#xD;&#xA;attribute values... why is that?"></child> after (//child)[2]')

select @xmldata

结果:

<root>
  <child myattr="carriage returns &#xD;&#xA; are not a problem" />
  <child>modifying text with carriage returns works
ok</child>
  <child myattr="but not attribute values... why is that?" />
</root>

最佳答案

空白字符可以被解析器规范化。

比照http://www.w3.org/TR/1998/REC-xml-19980210#AVNormalize

虽然您的 XML 是有效的,但具体如何呈现空白取决于实现。如您所见,crlf 已替换为一个空格。

Please note

In general XML works different with Content and Structural/Meta Data

Attribute values are considered structure and data between tags is considered content.

In the design of XML it was never expected that attributes would be displayed on end-user devices, I would suggest you just make another tag for this end user content.

关于sql - 为什么在 SQL Server 中修改 XML 属性时会删除回车符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33829745/

相关文章:

PHP/MYSQL 在数据库上存储大型数据集

sql - 动态调整一个变量保存多个值的存储过程

python - SQL 中的 KD 树实现

Python:write.csv 添加额外的回车符

c++ - 如何正确理解回车符(又名\r)?

c# - 如何通过 LINQ-to-SQL 和普通 SQL-Server 比较查询的性能

sql - 将星期几存储在数据库中

sql - 在整个数据库中进行字符串替换的最用户友好的方法?

sql-server - 服务器对象在 SQL Server Management Studio 中不可用

javascript - 如何使用 jquery 在跨度中显示多行字符串文本?