Number Sign (#) in xml variable

  • Is there a way to use the number sign (#) in an XML element name?

    declare @doc xml

    set @doc = '<root><node#>some_value</node#></root>'

    gives:

    Msg 9455, Level 16, State 1, Line 2

    XML parsing: line 1, character 26, illegal qualified name character

    I've read about escape characters like _x0035_, <node_x0035_></node_x0035_>, but that is not working.

  • Your decimal to hex conversion needs some work 😉

    x35 = decimal 53 = the number 5 on the ascii map

    x23 = decimal 35 = the hash sign on the ascii map

    This worked on SQL 2005 and SQL2008 for me:

    declare @doc xml

    set @doc = '<root><node_x0023_>some_value</node_x0023_></root>'

    As an aside this also worked so not sure what you were experiencing:

    declare @doc xml

    set @doc = '<root><node_x0035_>some_value</node_x0035_></root>'

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • .

  • When I execute these statements:

    declare @doc xml

    set @doc = '<root><node_x0023_>some_value</node_x0023_></root>'

    select @doc

    I get the following result as the contents of the XML variable:

    <root>

    <node_x0023_>some_value</node_x0023_>

    </root>

    The conversion is not made. I was expecting

    <root>

    <node#>some_value</node#>

    </root>

    Am I wrong?

  • Unfortunately you will not be able to have an actual "#" appear in your element name. It is not supported in the SQL Server implementation of XML...i.e. it must be escaped.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Thank you for your help on this topic.

  • You're very welcome.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

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

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