OK, what happened to my whitespace?

  • I have a problem with XML handling.  I'm sure there's a reason for it, but understanding it might point to a workaround for me.

    I've read a bit about how the various document processors treat insignificant whitespace (including CR, LF) but the documented issues seem to be about "insignificant", semantically neutral whitespace - say inserted between elements for readability - not within the element value.

    Consider the following :

    declare @dd varchar(50), @doc int

    select @dd = '<root><myelement>' + char(13) + char(10) + 'some text' + char(13) + char(10) + '</myelement></root>'

    select

    @dd -- OK, CRLF is definitely there either side of the text

    exec

    sp_xml_preparedocument @doc output, @dd

    select

    '*' + nodeval +'*' -- visible characters around what's being returned

    from

    OPENXML

    (@doc,'/',3)

    with

    (NodeVal varchar(100) '.')

    Now I was expecting to get  '*  some text  *' but instead I get '*some text*'

    Is there some explanation for this?  Is there any way to preserve the CRLF (or other leading or trailing whitespace)?

     

     

  • This was removed by the editor as SPAM

  • Sad state of affairs - MS stole them . . .

    refer to http://msdn2.microsoft.com/en-us/library/ms187367.aspx

    Their reasoning - "For backwards compatibility, sp_xml_preparedocument collapses the CR (char(13)) and LF (char(10)) characters in attributes even if these characters are entitized."

    Screwing with my data now and looking for an answer

  • You could wrap this as CDATA which throws out all formatting (note the <![[CDATA[  closed by ]]>.

    declare @dd varchar(300), @doc int

    select @dd = '<root ><myelement ><![CDATA[' + char(13) + char(10) + 'some text' + char(13) + char(10) + ']]></myelement></root>'

    select @dd -- OK, CRLF is definitely there either side of the text

    exec sp_xml_preparedocument @doc output, @dd

    select '*' + nodeval +'*' -- visible characters around what's being returned

    from

    OPENXML (@doc,'/',3)

    with (NodeVal varchar(100) '.')

    exec sp_xml_removedocument @doc

    Russel Loski, MCSE Business Intelligence, Data Platform

  • I was giving up all hope, and now 2 responses in a day.

    Thanks to both respondents.  Yeah, I'm having to do the CDATA wrap, but that now means more handling in the client code, which IMNSHO just shouldn't need to be done. 

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply