在 SQL 2005 上使用 FOR XML PATH 时如何保留与号 (&) [英] How to preserve an ampersand (&) while using FOR XML PATH on SQL 2005

查看:35
本文介绍了在 SQL 2005 上使用 FOR XML PATH 时如何保留与号 (&)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何技巧可以防止 SQL Server 实体化 &、< 和 > 等字符?我试图在我的 XML 文件中输出一个 URL,但 SQL 想要替换任何&"使用 '&amp;'

Are there any tricks for preventing SQL Server from entitizing chars like &, <, and >? I'm trying to output a URL in my XML file but SQL wants to replace any '&' with '&amp;'

进行以下查询:

SELECT 'http://foosite.com/' + RTRIM(li.imageStore)
        + '/ImageStore.dll?id=' + RTRIM(li.imageID)
        + '&raw=1&rev=' + RTRIM(li.imageVersion) AS imageUrl
FROM ListingImages li
FOR XML PATH ('image'), ROOT ('images'), TYPE

我得到的输出是这样的(&s是实体化的):

The output I get is like this (&s are entitized):

<images>
  <image>
    <imageUrl>http://foosite.com/pics4/ImageStore.dll?id=7E92BA08829F6847&amp;raw=1&amp;rev=0</imageUrl>
  </image>
</images>

我想要的是这个(&s 没有实体化):

What I'd like is this (&s are not entitized):

<images>
  <image>
    <imageUrl>http://foosite.com/pics4/ImageStore.dll?id=7E92BA08829F6847&raw=1&rev=0</imageUrl>
  </image>
</images>

如何防止 SQL Server 将&"实体化为&amp;"?

How does one prevent SQL server from entitizing the '&'s into '&amp;'?

推荐答案

SQL Server 生成的内容是正确的.您希望看到的不是格式良好的 XML.原因是 & 字符表示实体引用的开始,例如 &amp;.有关详细信息,请参阅 XML 规范.

What SQL Server generates is correct. What you expect to see is not well-formed XML. The reason is that & character signifies the start of an entity reference, such as &amp;. See the XML specification for more information.

当您的 XML 解析器从 XML 中解析出这个字符串时,它将理解 &amp; 实体引用并以您想要的形式返回文本.因此,XML 文件中的内部格式应该不会给您带来问题,除非您使用有问题的 XML 解析器,或者尝试手动解析它(在这种情况下,您当前的解析器代码目前就 XML 而言实际上是有问题的)规范).

When your XML parser parses this string out of XML, it will understand the &amp; entity references and return the text back in the form you want. So the internal format in the XML file should not cause a problem to you unless you're using a buggy XML parser, or trying to parse it manually (in which case your current parser code is effectively buggy at the moment with respect to the XML specification).

这篇关于在 SQL 2005 上使用 FOR XML PATH 时如何保留与号 (&amp;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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