﻿<?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  / Advanced XML Processing / 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>Fri, 24 May 2013 16:04:36 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>[quote][b]yazalpizar_ (12/23/2011)[/b][hr]One question, the series of posts "XML Workbench..." jumps from II to IV and then to X. I'm missing some post in the middle? I can't find the III, V,VI,VII,VII and IX posts, maybe just not exists?[/quote]I answer myself...I found all the XML series... and surprise, still more than 10 posts to read and test, got some homework for these days :-P</description><pubDate>Fri, 23 Dec 2011 08:25:37 GMT</pubDate><dc:creator>yazalpizar_</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>In case anyone mayneeded, here is the same post, but with comments in spanish, enjoy[code="sql"]CREATE TABLE [dbo].[OrderDetails](			[OrderDetailID] [int] IDENTITY(1,1) NOT NULL,[OrderNumber] VARCHAR(10) NOT NULL,			[ItemNumber] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,[Qty] [int] NULL,[Rate] FLOAT NULL,[QtyPicked] INT NULL) ON [PRIMARY]INSERT INTO OrderDetails (OrderNumber, ItemNumber, Qty, Rate, QtyPicked)    SELECT '00001', 'A001', 10, 11.25, 0    UNION SELECT '00001', 'A002', 20, 15, 0    UNION SELECT '00001', 'A003', 30, 23.75, 0--al usar FOR XML AUTO se genera un XMLSELECT OrderNumber, ItemNumber, Qty FROM OrderDetails FOR XML AUTO--pero el XML generado en realidad es un varchar, nos interesa generar un tipo XML--para esto usamos TYPESELECT OrderNumber, ItemNumber, Qty FROM OrderDetails FOR XML AUTO, TYPE--por defecto el SQL devuelve los valores en atributos, para añadirlos como tags independientes o nodos usamos ELEMENTSSELECT OrderNumber, ItemNumber, Qty FROM OrderDetails FOR XML AUTO, TYPE, ELEMENTS--pero no tenemos un elemento root, añadimos ROOT al finalSELECT OrderNumber, ItemNumber, Qty FROM OrderDetails FOR XML AUTO, TYPE, ELEMENTS, ROOT--ya tenemos el elemento root, pero tiene como nombre "root", cambiemos el nombre y pongamos "orderInfo"SELECT OrderNumber, ItemNumber, Qty FROM OrderDetails FOR XML AUTO, TYPE, ELEMENTS, ROOT('orderInfo')--por defecto los nodos generados tienen de nombre las columnas de la tabla, podemos renonbrarloSELECT OrderNumber as 'OrderNum', ItemNumber as ItemCode, Qty as Quantity  FROM OrderDetails FOR XML AUTO, TYPE, ELEMENTS, ROOT('itemInfo')  --ya renombramos el root y las columnas, pero vamos a renombrar lo que vendría siendo cada fila--para esto usamos la palabra reservada AUTO y un alias para la tablaSELECT OrderNumber, ItemNumber, Qty	FROM OrderDetails itemInfo FOR XML AUTO, TYPE, ELEMENTS, ROOT('orderInfo')--pero a veces usar un alias para el nombre de la tabla puede confundirnos en el caso de que la query sea complicada--usemos la palabra reservada RAWSELECT OrderNumber, ItemNumber, Qty	FROM OrderDetails FOR XML RAW('itemInfo'), TYPE, ELEMENTS, ROOT('orderInfo')--por último veremos que pasa con los elementos nulos--usaremos el modificador --por defecto el SQL no los carga en el XML, pero si luego este XML lo queremos procesar puede fallarnos--para que se cargue el elemento null añadimos XSINILUPDATE OrderDetails SET Qty = NULL WHERE OrderDetailID = 3SELECT OrderNumber,ItemNumber,Qty FROM OrderDetails FOR XML RAW('itemInfo'), TYPE, ELEMENTS XSINIL, ROOT('orderInfo')[/code]</description><pubDate>Fri, 23 Dec 2011 03:14:10 GMT</pubDate><dc:creator>yazalpizar_</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>I will be forever thankfull for this post. I have gone through all the previous posts about XML handling and this one is exactly what I was looking for. Nevertheless will also take a look on the next posts of the advanced XML processing.One question, the series of posts "XML Workbench..." jumps from II to IV and then to X. I'm missing some post in the middle? I can't find the III, V,VI,VII,VII and IX posts, maybe just not exists?</description><pubDate>Fri, 23 Dec 2011 02:30:02 GMT</pubDate><dc:creator>yazalpizar_</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Thanks!</description><pubDate>Fri, 09 Jul 2010 08:19:05 GMT</pubDate><dc:creator>Kiat Tang</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>You will need to build a dynamic query and execute it (to have the value of a column transformed as an element name).</description><pubDate>Fri, 09 Jul 2010 00:56:43 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Hi, I'm wondering if it's possible to create a xml structure where one column as the element name, another as the element value and another column as the attribute value?</description><pubDate>Thu, 08 Jul 2010 16:53:26 GMT</pubDate><dc:creator>Kiat Tang</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Great article!!!! Very simple and well written!!! :)</description><pubDate>Mon, 05 May 2008 05:52:26 GMT</pubDate><dc:creator>Anipaul</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Excellent!Ryan's code is much simpler than the two options I presented. This is really good and easy to understand.one small note: we need to make sure that the OrderHeader will come before OrderDetails. Currently it does. But if the query results try to use an ORDER BY clause, it is possible that the OrderHeader will be placed somewhere in the middle or at the end of the XML structure. But that is not likely to happen often. Just wanted to bring this to notice.Jacob</description><pubDate>Fri, 26 Oct 2007 04:11:16 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Nice work Jacob.Here's a 3rd way, this time using XML PATH...SELECT    OrderHeader.Name AS 'OrderHeader/Name',    OrderHeader.Address AS 'OrderHeader/Address',    OrderHeader.Code AS 'OrderHeader/Code',    OrderDetails.OrderNumber AS 'OrderDetails/OrderNumber',    OrderDetails.ItemNumber AS 'OrderDetails/ItemNumber',    OrderDetails.Qty AS 'OrderDetails/Qty'FROM    OrderDetails LEFT OUTER JOIN    (SELECT 'Jacob' AS 'Name', '401, TIME SQUARE' AS 'Address', '007' AS 'Code') OrderHeader        ON OrderDetails.ItemNumber = 'A001'FOR XML PATH (''), ROOT ('OrderInfo')</description><pubDate>Fri, 26 Oct 2007 03:41:39 GMT</pubDate><dc:creator>RyanRandall</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>[quote][b]flurk118 (10/25/2007)[/b][hr]Nice article.Has anyone got any examples of similar XML output but with header information included.Following on from Jacobs article and presuming the existence of an OrderHeader table, the output would look something like attachedTa[/quote]A dirty quick way of doing this is as follows:SELECT CAST(' ' + (SELECT 		'Jacob' AS 'Name',		'401, TIME SQUARE' AS 'Address',		'007' AS 'Code'		FOR XML PATH('OrderHeader') ) + (SELECT 	OrderNumber, 	ItemNumber, 	Qty 	FROM OrderDetails FOR XML AUTO, ELEMENTS)+' ' AS XML)However, a sweet and gentle way of doing this is by using the EXPLICIT keyword.SELECT    1 AS Tag,    NULL AS Parent,    NULL AS 'OrderInfo!1',    NULL AS 'OrderHeader!2!Name!element',	NULL AS 'OrderHeader!2!Address!element',	NULL AS 'OrderHeader!2!Code!element',	NULL AS 'OrderDetails!3!OrderNumber!element',	NULL AS 'OrderDetails!3!ItemNumber!element',	NULL AS 'OrderDetails!3!Qty!element'UNIONSELECT	2 AS Tag,	1 AS Parent,	NULL,	'Jacob',	'401, Time Square',	'999',	NULL,	NULL,	NULLUNION SELECT	3 AS Tag,	1 AS Parent,	NULL,	NULL,	NULL,	NULL,	OrderNumber,	ItemNumber,	QtyFROM 	OrderDetailsFOR XML EXPLICIT Both of the above queries give the same output that you asked.</description><pubDate>Thu, 25 Oct 2007 22:42:17 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Nice article.Has anyone got any examples of similar XML output but with header information included.Following on from Jacobs article and presuming the existence of an OrderHeader table, the output would look something like attachedTa</description><pubDate>Thu, 25 Oct 2007 09:17:59 GMT</pubDate><dc:creator>flurk118</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Excellent Job! Very easy to follow!</description><pubDate>Wed, 08 Aug 2007 09:19:00 GMT</pubDate><dc:creator>Paul Manzano</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>&lt;P&gt;That's great. It helped me out a lot.&lt;/P&gt;&lt;P&gt;Does anyone have any info/styles/code/opinions on how to retrieve on the other end?&lt;/P&gt;&lt;P&gt;Specifically how to parse this file into variables in VB.Net?&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Tue, 26 Jun 2007 07:22:00 GMT</pubDate><dc:creator>d_george</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>&lt;P&gt;Its very &lt;FONT face="Times New Roman" size=3&gt;straightforward&lt;/FONT&gt;. also very practicle.&lt;/P&gt;&lt;P&gt;keep it up......!!! &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;</description><pubDate>Thu, 17 May 2007 09:27:00 GMT</pubDate><dc:creator>Krupali Mehta</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>thanks for a well balanced document that gives concise information and syntax to a required information subject</description><pubDate>Mon, 14 May 2007 23:14:00 GMT</pubDate><dc:creator>norman harley</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>nice and simple</description><pubDate>Thu, 10 May 2007 20:05:00 GMT</pubDate><dc:creator>Suman Punukollu</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>&lt;P&gt;OH MY GIDDY AUNT!!!&lt;/P&gt;&lt;P&gt;This article is HUGE!!&lt;/P&gt;&lt;P&gt;Resultset &amp;gt; .NET Serialization &amp;gt; direct Object usage&lt;/P&gt;&lt;P&gt;You can autopopulate an objects list properties with a deserializing use of the resultset.&lt;/P&gt;&lt;P&gt;Have a CurrentOrder object? Derialize the resultset in to it.  I realize I make it sound simple, but if you are creating a system or can fit in some design tweaks/adjustments to the system, then the ability to autopopulate an Order object directly from a result set without need to 'read' the result and load the properies manually?  THAT'S HUGE !!&lt;/P&gt;</description><pubDate>Wed, 09 May 2007 10:36:00 GMT</pubDate><dc:creator>Ivan Weaver</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>&lt;P&gt;Very concise article.  I spent a couple days figuring this out.  Why is there nothing in BOL as simple as this?  &lt;/P&gt;&lt;P&gt;You might note that to assign the results of the xml-generating query to a variable declared as XML, you need to surround the query in parentheses.&lt;/P&gt;&lt;P&gt;declare @xmlparm xml&lt;/P&gt;&lt;P&gt;select @xmlparm = (select orderid from order for xml auto, type)&lt;/P&gt;</description><pubDate>Wed, 09 May 2007 10:28:00 GMT</pubDate><dc:creator>stevemc</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>&lt;P&gt;Do you have an example that shows your specific problem? I will try to help you out then.&lt;/P&gt;</description><pubDate>Wed, 09 May 2007 08:55:00 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>You can just cast the nvarchar column to xml, but this obviously incurs the overhead of parsing the content and would error out if the column contains invalid xml.use tempdb;create table t (i int, x nvarchar(100));insert t values (1, '&amp;lt;abc&amp;gt;def&amp;lt;/abc&amp;gt;');goselect i, cast(x as xml) from t for xml autogo</description><pubDate>Wed, 09 May 2007 08:26:00 GMT</pubDate><dc:creator>Adrian Hains</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Great Article! Simple through examples and very clear. Keep'em comming &lt;img src='images/emotions/wink.gif' height='20' width='20' border='0' title='Wink' align='absmiddle'&gt; Cheers,</description><pubDate>Wed, 09 May 2007 08:02:00 GMT</pubDate><dc:creator>noeld</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Thanks! I needed That &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;</description><pubDate>Wed, 09 May 2007 07:40:00 GMT</pubDate><dc:creator>tbredeme</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>&lt;P&gt;Very nicely-presented article - well done Jacob!&lt;/P&gt;&lt;P&gt;My only suggestion would be to rename this particular article to 'Simple XML Processing', as some people might be unnecessarily scared off &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt;I look forward to the next article...&lt;/P&gt;</description><pubDate>Wed, 09 May 2007 07:12:00 GMT</pubDate><dc:creator>RyanRandall</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Very helpful, thanks. If you have xml already in an nvarchar column, is there any way to have it not be escaped in the for xml output? I'm thinking I might be able to write a query that would insert that nvarchar column into a temp table with an xml datatype. Then I would be able to query inside the xml document itself while joining with other data. Right now we are stuck with LIKE queries.</description><pubDate>Wed, 09 May 2007 06:53:00 GMT</pubDate><dc:creator>Philip Nelson</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>I agree, that was a great article.  Very simple and very worthwhile.</description><pubDate>Wed, 09 May 2007 05:59:00 GMT</pubDate><dc:creator>Brad Allison</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>great article! I never realised how simple it actually is to get XML out of 2005.</description><pubDate>Wed, 09 May 2007 05:06:00 GMT</pubDate><dc:creator>Rob-242869</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>I found article very usefull. Thank you.</description><pubDate>Wed, 09 May 2007 04:52:00 GMT</pubDate><dc:creator>agrinberg</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>&lt;P&gt;Nicely done... simple and easy language.&lt;/P&gt;&lt;P&gt;Keep it up!!!&lt;/P&gt;</description><pubDate>Wed, 09 May 2007 01:26:00 GMT</pubDate><dc:creator>Vasant Raj</dc:creator></item><item><title>RE: Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Excellent. Now we're cookin' with gas! Thanks.</description><pubDate>Tue, 08 May 2007 22:52:00 GMT</pubDate><dc:creator>Ted Cooper-351490</dc:creator></item><item><title>Advanced XML Processing</title><link>http://www.sqlservercentral.com/Forums/Topic362813-356-1.aspx</link><description>Comments posted here are about the content posted at &lt;A HREF="http://www.sqlservercentral.com/columnists/jSebastian/2982.asp"&gt;http://www.sqlservercentral.com/columnists/jSebastian/2982.asp&lt;/A&gt;</description><pubDate>Wed, 02 May 2007 16:40:00 GMT</pubDate><dc:creator>jacob sebastian</dc:creator></item></channel></rss>