Re: Can FOR XML PATH create truly _empty_ elements? -
12-05-2008
, 07:01 AM
Thanks for the reply Martin.
I was aware of xsi:nil, but it's a workaround and I want to be sure there
isn't a proper way to do it before I do something like that. It is surprising
to me that FOR XML PATH is capable of generating all manner of complex xml
but not it seems a simple empty tag!
I have also discovered another workaround, which is to set the mixed
attribute to true on the tags associated type. ie:
create XML SCHEMA COLLECTION MySchema As
'<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tag">
<xs:complexType mixed="true">
<xs:sequence/>
</xs:complexType>
</xs:element>
</xs:schema>
'
now if I do
declare @xml xml(MySchema)
set @xml = (select '' as 'tag' for xml path (''), type)
it returns me a genuinely empty tag.
Still not perfect because these tags are now not statically enforced to have
no text value but at least they can't have attributes or sub-elements so it's
getting close. |