﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Jacob Sebastian / Article Discussions / Article Discussions by Author  / XML Workshop 24 - Modifying XML Documents Using XQuery Part 1 / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 18:31:38 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>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:[code="sql"]SELECT TOP 1 	ID,	Data.query('	for $n in //address/*	where (local-name($n) = ("Line1", "Line2"))	return $n	')FROM Addresses[/code]However, the "not equal" does NOT work.  Instead, IT RETURNS ALL NODES!?[code="sql"]SELECT TOP 1 	ID,	Data.query('	for $n in //address/*	where (local-name($n) != ("Line1", "Line2"))	return $n	')FROM Addresses[/code]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"?</description><pubDate>Fri, 17 Aug 2012 09:33:03 GMT</pubDate><dc:creator>tymberwyld</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>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.[b]Use-case:[/b]A table stores "Address" data in an Xml column called "Data".  The xml is untyped but generally follows the following "schema":[code="xml"]&amp;lt;Data type="address"&amp;gt;    &amp;lt;Line1 /&amp;gt;    &amp;lt;Line2 /&amp;gt;    &amp;lt;Line3 /&amp;gt;    &amp;lt;City /&amp;gt;    &amp;lt;State /&amp;gt;    &amp;lt;PostalCode /&amp;gt;&amp;lt;/Data&amp;gt;[/code]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.[b]Solution:[/b]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).[code="sql"]-- Get initial data from recordDECLARE @Data xmlSET @Data = '&amp;lt;Data type="address"&amp;gt;	&amp;lt;Line1&amp;gt;123 Seseame St&amp;lt;/Line1&amp;gt;	&amp;lt;Line2 /&amp;gt;	&amp;lt;Line3 /&amp;gt;	&amp;lt;City&amp;gt;New York&amp;lt;/City&amp;gt;	&amp;lt;Region&amp;gt;NY&amp;lt;/Region&amp;gt;	&amp;lt;PostalCode&amp;gt;12345&amp;lt;/PostalCode&amp;gt;	&amp;lt;PostalExtension&amp;gt;6789&amp;lt;/PostalExtension&amp;gt;	&amp;lt;OtherData&amp;gt;My other data&amp;lt;/OtherData&amp;gt;	&amp;lt;Lat&amp;gt;42&amp;lt;/Lat&amp;gt;	&amp;lt;Long&amp;gt;42&amp;lt;/Long&amp;gt;&amp;lt;/Data&amp;gt;'SELECT @Data-- Remove known nodes SET @Data.modify('	delete (//*[local-name()=("Line1", "Line2", "Line3", "City", "Region", "PostalCode", "PostalExtension")])	')	-- Insert known nodes with updated dataDECLARE 	@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 xmlSET @Nodes = ISNULL('&amp;lt;Line1&amp;gt;' + @Line1 + '&amp;lt;/Line1&amp;gt;', '')	+ ISNULL('&amp;lt;Line2&amp;gt;' + @Line2 + '&amp;lt;/Line2&amp;gt;', '')	+ ISNULL('&amp;lt;Line3&amp;gt;' + @Line3 + '&amp;lt;/Line3&amp;gt;', '')	+ ISNULL('&amp;lt;City&amp;gt;' + @City + '&amp;lt;/City&amp;gt;', '')	+ ISNULL('&amp;lt;Region&amp;gt;' + @Region + '&amp;lt;/Region&amp;gt;', '')	+ ISNULL('&amp;lt;PostalCode&amp;gt;' + @PostalCode + '&amp;lt;/PostalCode&amp;gt;', '')	SET @Data.modify('	insert sql:variable("@Nodes")	as first	into (/Data)[1]	')SELECT @Data[/code]</description><pubDate>Fri, 17 Aug 2012 08:38:31 GMT</pubDate><dc:creator>tymberwyld</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>It seems to me that I found the solution:DECLARE @XML XMLDECLARE @ElementName VARCHAR(20)SET @ElementName = 'city'SELECT @XML = '&amp;lt;employees&amp;gt;  &amp;lt;employee&amp;gt;    &amp;lt;name&amp;gt;Jacob&amp;lt;/name&amp;gt;    &amp;lt;city&amp;gt;NY&amp;lt;/city&amp;gt;    &amp;lt;team&amp;gt;SQL Server&amp;lt;/team&amp;gt;  &amp;lt;/employee&amp;gt;  &amp;lt;employee&amp;gt;    &amp;lt;name&amp;gt;Steve&amp;lt;/name&amp;gt;    &amp;lt;city&amp;gt;NY&amp;lt;/city&amp;gt;    &amp;lt;team&amp;gt;ASP.NET&amp;lt;/team&amp;gt;  &amp;lt;/employee&amp;gt;&amp;lt;/employees&amp;gt;'--SET @XML.modify('delete (/employees/employee/*[local-name() = "city"])')SET @XML.modify('delete (/employees/employee/*[local-name() = sql:variable("@ElementName")])')SELECT @XML</description><pubDate>Thu, 04 Mar 2010 05:34:10 GMT</pubDate><dc:creator>mypoints01</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>DECLARE @XML XMLDECLARE @ElementName VARCHAR(20)SET @ElementName = 'city'SELECT @XML = '&amp;lt;employees&amp;gt;  &amp;lt;employee&amp;gt;    &amp;lt;name&amp;gt;Jacob&amp;lt;/name&amp;gt;    &amp;lt;city&amp;gt;NY&amp;lt;/city&amp;gt;    &amp;lt;team&amp;gt;SQL Server&amp;lt;/team&amp;gt;  &amp;lt;/employee&amp;gt;  &amp;lt;employee&amp;gt;    &amp;lt;name&amp;gt;Steve&amp;lt;/name&amp;gt;    &amp;lt;city&amp;gt;NY&amp;lt;/city&amp;gt;    &amp;lt;team&amp;gt;ASP.NET&amp;lt;/team&amp;gt;  &amp;lt;/employee&amp;gt;&amp;lt;/employees&amp;gt;'--SET @XML.modify('delete (/employees/employee/city)')--SELECT @XMLBut 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</description><pubDate>Thu, 04 Mar 2010 05:16:37 GMT</pubDate><dc:creator>mypoints01</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Sorry for the late reply.  That worked for me.  Thanks.</description><pubDate>Thu, 18 Feb 2010 13:18:54 GMT</pubDate><dc:creator>Sinh Dinh</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Is it what you are looking for?[code]SELECT (	SELECT XML1, XML2	FROM table1 	FOR XML PATH(''), TYPE).query('/*/Response')/*&amp;lt;Response&amp;gt;  &amp;lt;Value&amp;gt;Idaho&amp;lt;/Value&amp;gt;  &amp;lt;Result&amp;gt;ID&amp;lt;/Result&amp;gt;  &amp;lt;Return&amp;gt;50&amp;lt;/Return&amp;gt;&amp;lt;/Response&amp;gt;&amp;lt;Response&amp;gt;  &amp;lt;Value&amp;gt;Texas&amp;lt;/Value&amp;gt;  &amp;lt;Result&amp;gt;TX&amp;lt;/Result&amp;gt;  &amp;lt;Return&amp;gt;50&amp;lt;/Return&amp;gt;&amp;lt;/Response&amp;gt;*/[/code]</description><pubDate>Wed, 21 Oct 2009 20:11:14 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>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:[code="sql"]CREATE TABLE Table1 (  ID INT  ,XML1 XML  ,XML2 XML )[/code]Example:XML Field 1:[code="xml"]   &amp;lt;Response&amp;gt;      &amp;lt;Value&amp;gt;Idaho&amp;lt;/Value&amp;gt;      &amp;lt;Result&amp;gt;ID&amp;lt;/Result&amp;gt;      &amp;lt;Return&amp;gt;50&amp;lt;/Return&amp;gt;    &amp;lt;/Response&amp;gt;[/code]XML Field 2:[code="xml"]   &amp;lt;Response&amp;gt;      &amp;lt;Value&amp;gt;Texas&amp;lt;/Value&amp;gt;      &amp;lt;Result&amp;gt;TX&amp;lt;/Result&amp;gt;      &amp;lt;Return&amp;gt;50&amp;lt;/Return&amp;gt;    &amp;lt;/Response&amp;gt;[/code]Resulting XML Field:[code="xml"]   &amp;lt;Response&amp;gt;      &amp;lt;Value&amp;gt;Texas&amp;lt;/Value&amp;gt;      &amp;lt;Result&amp;gt;TX&amp;lt;/Result&amp;gt;      &amp;lt;Return&amp;gt;50&amp;lt;/Return&amp;gt;    &amp;lt;/Response&amp;gt;   &amp;lt;Response&amp;gt;      &amp;lt;Value&amp;gt;Texas&amp;lt;/Value&amp;gt;      &amp;lt;Result&amp;gt;TX&amp;lt;/Result&amp;gt;      &amp;lt;Return&amp;gt;50&amp;lt;/Return&amp;gt;    &amp;lt;/Response&amp;gt;[/code]</description><pubDate>Wed, 21 Oct 2009 14:34:34 GMT</pubDate><dc:creator>Sinh Dinh</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Well, I would suggest using some other tools such as SSIS if you want to modify the file.</description><pubDate>Thu, 23 Jul 2009 07:02:30 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>And it's possible to modify the file it's self using XQuery, that is, updating, deleting, inserting....</description><pubDate>Thu, 23 Jul 2009 06:55:46 GMT</pubDate><dc:creator>ynevet</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>You can load the content of an XML file to an XML variable using OPENROWSET(BULK..). Once the content of the XML file is loaded into the XML variable, you can query it.</description><pubDate>Thu, 23 Jul 2009 06:39:57 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Hi, if your answer is intended for me then I think I didn't clear my self enough.I'm asking if it possible to access XML file from XQuery instead of querying an XML variable.Yair</description><pubDate>Thu, 23 Jul 2009 04:19:37 GMT</pubDate><dc:creator>ynevet</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Yes, it can work on an XML column too. Here is an example[code]DECLARE @t TABLE (id int, data XML)INSERT INTO @t(id, data) SELECT 1, '&lt;Employees&gt;  &lt;Employee name="Jacob" city="NY" Team="SQL Server"/&gt;  &lt;Employee name="Steve" city="FL" Team="SQL Server"/&gt;  &lt;Employee name="Bob" city = "CA" Team="ASP.NET"/&gt;&lt;/Employees&gt;'INSERT INTO @t(id, data) SELECT 2, '&lt;Employees&gt;  &lt;Employee name="Jacob" city="NY" Team="SQL Server"/&gt;  &lt;Employee name="Steve" city="FL" Team="SQL Server"/&gt;  &lt;Employee name="Bob" city = "CA" Team="ASP.NET"/&gt;&lt;/Employees&gt;'UPDATE @tSET data.modify('    delete (Employees/Employee[@Team="SQL Server"]/@city)    ')SELECT * FROM @t [/code]</description><pubDate>Thu, 23 Jul 2009 02:05:17 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Hi Jacob,It did work for me using the SQL query as you advised.Look, according to your example, we are working against an XML variable, what about working against physical XML file?It's possible?Yair</description><pubDate>Thu, 23 Jul 2009 01:23:15 GMT</pubDate><dc:creator>ynevet</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Glad to know it helped.I have written a series of XQuery tutorials at my blog site, in case you need further help.regardsJacob</description><pubDate>Wed, 22 Jul 2009 10:55:16 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Your a star !!!Just realy getting into this XML thing with SQL server, so examples like this are perfect.Andy</description><pubDate>Wed, 22 Jul 2009 10:51:42 GMT</pubDate><dc:creator>andrew.sims</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>[quote][b]andrew.sims (7/22/2009)[/b][hr]Nice article.Would it also be possible to look at the same processes where the XML data is arranged like...[code="xml"]&lt;Employees&gt;  &lt;Employee&gt;	&lt;name&gt;Jacob&lt;/name&gt;	&lt;city&gt;NY&lt;/city&gt;	&lt;Team&gt;SQL Server&lt;/team&gt;  &lt;/Employee&gt;  &lt;Employee&gt;	&lt;name&gt;Steve&lt;/name&gt;	&lt;city/&gt;	&lt;Team&gt;SQL Server&lt;/team&gt;  &lt;/Employee&gt;  &lt;Employee&gt;	&lt;name&gt;Bob&lt;/name&gt;	&lt;city&gt;CA&lt;/city&gt;	&lt;Team/&gt;  &lt;/Employee&gt;&lt;/Employees&gt;[/code]Which include blank elements.Have already been caught out with data like this needing insert/modify depending if a value existsThanksAndy[/quote]Hi Andy,It is possible by modifying the Xquery expression slightly, as given in the following example[code]DECLARE @x XMLSELECT @x = '&lt;employees&gt;  &lt;employee&gt;    &lt;name&gt;Jacob&lt;/name&gt;    &lt;city&gt;NY&lt;/city&gt;    &lt;team&gt;SQL Server&lt;/team&gt;  &lt;/employee&gt;  &lt;employee&gt;    &lt;name&gt;Steve&lt;/name&gt;    &lt;city&gt;NY&lt;/city&gt;    &lt;team&gt;ASP.NET&lt;/team&gt;  &lt;/employee&gt;&lt;/employees&gt;'DECLARE @Team VARCHAR(20)SELECT @Team = 'SQL Server'SET @x.modify('    delete (employees/employee[team=sql:variable("@Team")]/city)    ')SELECT @x /*&lt;employees&gt;  &lt;employee&gt;    &lt;name&gt;Jacob&lt;/name&gt;    &lt;team&gt;SQL Server&lt;/team&gt;  &lt;/employee&gt;  &lt;employee&gt;    &lt;name&gt;Steve&lt;/name&gt;    &lt;city&gt;NY&lt;/city&gt;    &lt;team&gt;ASP.NET&lt;/team&gt;  &lt;/employee&gt;&lt;/employees&gt;*/[/code]</description><pubDate>Wed, 22 Jul 2009 10:10:09 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>[quote][b]mg (7/22/2009)[/b][hr]In example 10 where you delete the “city” attribute where the “team” is “SQL Server” is there any way to pass in the “SQL Server” as a variable instead of using a constant?  Thanks,Marlin[/quote]Marlin,You can use a variable as given in the following example[code]DECLARE @x XMLSELECT @x = '&lt;Employees&gt;  &lt;Employee name="Jacob" city="NY" Team="SQL Server"/&gt;  &lt;Employee name="Steve" city="FL" Team="SQL Server"/&gt;  &lt;Employee name="Bob" city = "CA" Team="ASP.NET"/&gt;&lt;/Employees&gt;'DECLARE @Team VARCHAR(20)SELECT @Team = 'SQL Server'SET @x.modify('    delete (Employees/Employee[@Team=sql:variable("@Team")]/@city)    ')SELECT @x /*&lt;Employees&gt;  &lt;Employee name="Jacob" Team="SQL Server" /&gt;  &lt;Employee name="Steve" Team="SQL Server" /&gt;  &lt;Employee name="Bob" city="CA" Team="ASP.NET" /&gt;&lt;/Employees&gt;*/[/code]</description><pubDate>Wed, 22 Jul 2009 10:05:00 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>In example 10 where you delete the “city” attribute where the “team” is “SQL Server” is there any way to pass in the “SQL Server” as a variable instead of using a constant?  Thanks,Marlin</description><pubDate>Wed, 22 Jul 2009 09:25:27 GMT</pubDate><dc:creator>mg-885861</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Nice article.Would it also be possible to look at the same processes where the XML data is arranged like...[code="xml"]&lt;Employees&gt;  &lt;Employee&gt;	&lt;name&gt;Jacob&lt;/name&gt;	&lt;city&gt;NY&lt;/city&gt;	&lt;Team&gt;SQL Server&lt;/team&gt;  &lt;/Employee&gt;  &lt;Employee&gt;	&lt;name&gt;Steve&lt;/name&gt;	&lt;city/&gt;	&lt;Team&gt;SQL Server&lt;/team&gt;  &lt;/Employee&gt;  &lt;Employee&gt;	&lt;name&gt;Bob&lt;/name&gt;	&lt;city&gt;CA&lt;/city&gt;	&lt;Team/&gt;  &lt;/Employee&gt;&lt;/Employees&gt;[/code]Which include blank elements.Have already been caught out with data like this needing insert/modify depending if a value existsThanksAndy</description><pubDate>Wed, 22 Jul 2009 09:02:37 GMT</pubDate><dc:creator>andrew.sims</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Hi Yair,All these are TSQL examples and hence you can run them in SQL Server Management Studio.regardsJacob</description><pubDate>Wed, 22 Jul 2009 07:17:36 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Hi Jacob,this is really a good article, very clear and friendly one.Please tell me, where should I write this code to get the required results from XQuery?In XML file? in some editor? processor?Just for example, in order to run C# code I should write it in .cs file extenstion and build it with .NET compiler.... or JavaScript in .js file extention and run it in some browser... etc.Thank you,Yair</description><pubDate>Wed, 22 Jul 2009 06:08:50 GMT</pubDate><dc:creator>ynevet</dc:creator></item><item><title>XML Workshop 24 - Modifying XML Documents Using XQuery Part 1</title><link>http://www.sqlservercentral.com/Forums/Topic757167-356-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/articles/XML/67548/"&gt;XML Workshop 24 - Modifying XML Documents Using XQuery Part 1&lt;/A&gt;[/B]</description><pubDate>Wed, 22 Jul 2009 00:38:36 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item></channel></rss>