sql - 在 SQL 中,如何形成 XML 输出,其中属性值之一为 XML,无需 <,> 编码

标签 sql xml

我正在尝试返回 XML 输出的查询,其中属性值之一为 XML,没有 <,/> 编码。

示例:

<Event EventLogId="124018" EventCategoryCode="ABC" 
       EventTypeCode="ERROR" 
       xmlObject="<attributes><attribute>A1</attribute></attributes>" />

<Event EventLogId="124019" EventCategoryCode="DEF" 
       EventTypeCode="Warning" 
       xmlObject="<attributes><attribute>A2</attribute></attributes>" />

我尝试了这个查询

SELECT  
    EventLogId  AS EventLogId 
    ,EventCategoryCode AS EventCategoryCode        
    ,EventTypeCode AS EventTypeCode      
    ,CAST(Event.xmlObject AS NVARCHAR(MAX)) as xmlObject
FROM 
    EventLog Event (NOLOCK) 
FOR XML AUTO

但是我得到了这个输出

<Event EventLogId="124018" EventCategoryCode="ABC" 
       EventTypeCode="ERROR" 
       xmlObject="&lt;attributes&gt;&lt;attribute&gt;A1&lt;/attribute&gt;&lt;/attributes&gt;" />

<Event EventLogId="124019" EventCategoryCode="DEF" 
       EventTypeCode="Warning" 
       xmlObject="&lt;attributes&gt;&lt;attribute&gt;A2&lt;/attribute&lt;&lt;/attributes&gt;" />

我要< , />代替&lt;&gt;

最佳答案

It is not valid to have non-escaped (i.e. not entity encoded) characters in attributes or anywhere in XML.

为了 100% 准确,在我收到大量评论之前,您可以使用特殊的 DATA 语法,但这在属性中是不合法的。而且这个功能很少被使用。

您可以在此处阅读该标准 ( http://www.w3.org/TR/xml/ ) 基本 XML 标准实际上非常短,阅读起来应该不到一个小时。

关于sql - 在 SQL 中,如何形成 XML 输出,其中属性值之一为 XML,无需 <,> 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32503320/

相关文章:

java - XPath:查找具有两个条件的节点

c++ - 在嵌入式系统中使用 xml 作为数据库有什么好处?

java - 就模式类型“DateTimeType”而言,构面无效

php - 它没有在数据库中插入值

android - XML 中 EditText 的提示大小

python - 使用 Python 向 Plist 中的深层节点添加数据

mysql - 在另一个sql文件中调用一个sql文件

sql - 使用 ADF 和 Oracle SQL 执行缓慢的 View

java - Hibernate 可以将 Java Calendar 对象映射到 SQL TIMESTAMP 列吗?

MySQL - 事务和锁中的互斥?