XML varible gets none xml value

  • Can anyone explain how come the script bellow runs without a runtime error?

    declare @xml xml

    set @xml = 'this is not a valid XML'

    select @xml

    I would expect this script to fail with a runtime error, because AFAIK "this is not a valid XML" is really not a valid xml.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I suppose it's just a choice by Microsoft, which I am okay with

    If you want to enforce valid XML, either throw in a tag or add a schema (CREATE XML SCHEMA COLLECTION)

    Funny, this generates an error

    declare @xml xml

    set @xml = ' this is not a valid XML'

    select @xml

    Msg 9400, Level 16, State 1, Line 2

    XML parsing: line 1, character 29, unexpected end of input

    Books Online

    http://msdn.microsoft.com/en-us/library/ms184277.aspx

    Well-formed XML and the xml Data Type

    The xml data type implements the ISO standard xml data type. Therefore, it can store well-formed XML version 1.0 documents and also so-called XML content fragments with text nodes and an arbitrary number of top-level elements in an untyped XML column. The system checks that the data is well-formed, does not require the column to be bound to XML schemas, and rejects data that is not well-formed in the extended sense. This is true also of untyped XML variables and parameters.

    I don't know if the above string is "well-formed" though

    SQLServerNewbieMCITP: Database Administrator SQL Server 2005
  • Jerry Hung (10/29/2008)


    I suppose it's just a choice by Microsoft, which I am okay with

    If you want to enforce valid XML, either throw in a tag or add a schema (CREATE XML SCHEMA COLLECTION)

    Funny, this generates an error

    declare @xml xml

    set @xml = ' this is not a valid XML'

    select @xml

    Msg 9400, Level 16, State 1, Line 2

    XML parsing: line 1, character 29, unexpected end of input

    Books Online

    http://msdn.microsoft.com/en-us/library/ms184277.aspx

    Well-formed XML and the xml Data Type

    The xml data type implements the ISO standard xml data type. Therefore, it can store well-formed XML version 1.0 documents and also so-called XML content fragments with text nodes and an arbitrary number of top-level elements in an untyped XML column. The system checks that the data is well-formed, does not require the column to be bound to XML schemas, and rejects data that is not well-formed in the extended sense. This is true also of untyped XML variables and parameters.

    I don't know if the above string is "well-formed" though

    Thank you for your response. I agree that it is a choice that was taken by Microsoft, but unlike you, I don’t think that this is a good choice:-). If a variable is declared as an XML, I expect it to be able to store only XML data type and not other data types. I know that I can use a typed XML and then make sure how the XML looks like, but I think that if I was told that there is a data type that can store an untyped XML, and that I’m guaranteed that it won’t store something that is not an XML, this is exactly what it should do. I don't think that XML data type does that (but I admit that I'm not an expert on XML).

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Adi: Which rule of Well-Formed XML do you think that 'this is not a valid XML' violates?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • rbarryyoung (10/29/2008)


    Adi: Which rule of Well-Formed XML do you think that 'this is not a valid XML' violates?

    I think that an XML should have at least one element. My string has no elements at all.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I am not aware of such a rule.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • The fallowing is taken from the XML specification

    [Definition: Each XML document contains one or more elements, the boundaries of which are either delimited by start-tags and end-tags, or, for empty elements, by an empty-element tag. Each element has a type, identified by name, sometimes called its "generic identifier" (GI), and may have a set of attribute specifications.] Each attribute specification has a name and a value.

    I looked at the specification that are published at W3C (http://www.w3.org/TR/xml/)

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Yes, but "Well-Formed" XML includes both Documents and Fragments.

    If this is really important to you, I think that you could just test whether the first character is an "&lt" or not.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 8 posts - 1 through 7 (of 7 total)

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