XML Workshop 24 - Modifying XML Documents Using XQuery Part 1

  • I have multiple XML fields with the same schema. Is it possible to "concatenate" them in a select statement to generate a single XML field?

    Table Schema:

    CREATE TABLE Table1 (

    ID INT

    ,XML1 XML

    ,XML2 XML )

    Example:

    XML Field 1:

    <Response>

    <Value>Idaho</Value>

    <Result>ID</Result>

    <Return>50</Return>

    </Response>

    XML Field 2:

    <Response>

    <Value>Texas</Value>

    <Result>TX</Result>

    <Return>50</Return>

    </Response>

    Resulting XML Field:

    <Response>

    <Value>Texas</Value>

    <Result>TX</Result>

    <Return>50</Return>

    </Response>

    <Response>

    <Value>Texas</Value>

    <Result>TX</Result>

    <Return>50</Return>

    </Response>

  • Is it what you are looking for?

    SELECT (

    SELECT XML1, XML2

    FROM table1

    FOR XML PATH(''), TYPE

    ).query('/*/Response')

    /*

    <Response>

    <Value>Idaho</Value>

    <Result>ID</Result>

    <Return>50</Return>

    </Response>

    <Response>

    <Value>Texas</Value>

    <Result>TX</Result>

    <Return>50</Return>

    </Response>

    */

    .

  • Sorry for the late reply. That worked for me. Thanks.

  • DECLARE @XML XML

    DECLARE @ElementName VARCHAR(20)

    SET @ElementName = 'city'

    SELECT @XML = '

    <employees>

    <employee>

    <name>Jacob</name>

    <city>NY</city>

    <team>SQL Server</team>

    </employee>

    <employee>

    <name>Steve</name>

    <city>NY</city>

    <team>ASP.NET</team>

    </employee>

    </employees>'

    --SET @XML.modify('delete (/employees/employee/city)')

    --SELECT @XML

    But I need to delete all elements with the name equal @ElementName = 'city' and

    the name of the element should be variable.

    Is it possible?

    Thanks, M

  • It seems to me that I found the solution:

    DECLARE @XML XML

    DECLARE @ElementName VARCHAR(20)

    SET @ElementName = 'city'

    SELECT @XML = '

    <employees>

    <employee>

    <name>Jacob</name>

    <city>NY</city>

    <team>SQL Server</team>

    </employee>

    <employee>

    <name>Steve</name>

    <city>NY</city>

    <team>ASP.NET</team>

    </employee>

    </employees>'

    --SET @XML.modify('delete (/employees/employee/*[local-name() = "city"])')

    SET @XML.modify('delete (/employees/employee/*[local-name() = sql:variable("@ElementName")])')

    SELECT @XML

  • Good article. I've noticed that the "modify" function can only contain one operation per "SET". However, I want to delete multiple nodes in a piece of XML and not have to code several "SET @x.modify(...)" statements. SQL Server will not let you do "Set-based" operations multiple times on an XML column. So, maybe you can update your article with a way to delete multiple pieces of XML within the document at one time.

    Use-case:

    A table stores "Address" data in an Xml column called "Data". The xml is untyped but generally follows the following "schema":

    <Data type="address">

    <Line1 />

    <Line2 />

    <Line3 />

    <City />

    <State />

    <PostalCode />

    </Data>

    However, the programmers have the authority to add any other Nodes they'd like in order to store other things about an address (ex. maybe the "Lat" and "Long" or the CASS Result [USPS Coding Accuracy Support System]). Any stored procedures or queries which need to update this XML data should NEVER remove any of the other nodes that they are not responsible for. In other words, do not simply overwrite the XML data, but rather just manipulate the nodes that are specific to the process at hand.

    Solution:

    The solution to this (because one developer from another will never know ALL the nodes that will exist in the data at any time) is to simply "delete" all the nodes that that developer knows pertains to his process, and then "insert" them back in. This way, if the nodes didn't exist to begin with, then there will be no harm. This will be much cleaner than using "replace value of" functions which rely on the nodes existing up-front (which may not be the case as time goes on and more is added to the XML).

    -- Get initial data from record

    DECLARE @data xml

    SET @data = '<Data type="address">

    <Line1>123 Seseame St</Line1>

    <Line2 />

    <Line3 />

    <City>New York</City>

    <Region>NY</Region>

    <PostalCode>12345</PostalCode>

    <PostalExtension>6789</PostalExtension>

    <OtherData>My other data</OtherData>

    <Lat>42</Lat>

    <Long>42</Long>

    </Data>'

    SELECT @data

    -- Remove known nodes

    SET @data.modify('

    delete (//*[local-name()=("Line1", "Line2", "Line3", "City", "Region", "PostalCode", "PostalExtension")])

    ')

    -- Insert known nodes with updated data

    DECLARE

    @Line1 nvarchar(50) = '999 Somewhere Ave',

    @Line2 nvarchar(50) = NULL,

    @Line3 nvarchar(50) = NULL,

    @City nvarchar(50) = 'Walawala',

    @Region nvarchar(50) = 'WA',

    @PostalCode nvarchar(50) = '99999'

    DECLARE @Nodes xml

    SET @Nodes = ISNULL('<Line1>' + @Line1 + '</Line1>', '')

    + ISNULL('<Line2>' + @Line2 + '</Line2>', '')

    + ISNULL('<Line3>' + @Line3 + '</Line3>', '')

    + ISNULL('<City>' + @City + '</City>', '')

    + ISNULL('<Region>' + @Region + '</Region>', '')

    + ISNULL('<PostalCode>' + @PostalCode + '</PostalCode>', '')

    SET @data.modify('

    insert sql:variable("@Nodes")

    as first

    into (/Data)[1]

    ')

    SELECT @data

  • I was working on a more "set-based" approach to my above comment. However, the FLWOR statements in SQL don't seem to be working correctly.

    For example, this works:

    SELECT TOP 1

    ID

    ,Data.query('

    for $n in //address/*

    where (local-name($n) = ("Line1", "Line2"))

    return $n

    ')

    FROM Addresses

    However, the "not equal" does NOT work. Instead, IT RETURNS ALL NODES!?

    SELECT TOP 1

    ID

    ,Data.query('

    for $n in //address/*

    where (local-name($n) != ("Line1", "Line2"))

    return $n

    ')

    FROM Addresses

    Basically, can anyone tell me how to make the "where" clause so that it DOES NOT return nodes with a name of "Line1" and "Line2"?

Viewing 7 posts - 16 through 21 (of 21 total)

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