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 ‘&‘
Take the following query:
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
The output I get is like this (&s are entitized):
<images> <image> <imageUrl>http://foosite.com/pics4/ImageStore.dll?id=7E92BA08829F6847&raw=1&rev=0</imageUrl> </image> </images>
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>
How does one prevent SQL server from entitizing the ‘&’s into ‘&‘?
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&. See the XML specification for more information.When your XML parser parses this string out of XML, it will understand the
&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).