<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>codegumbo &#187; XML</title>
	<atom:link href="http://codegumbo.com/index.php/category/development/code/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://codegumbo.com</link>
	<description>Laissez Les Bon Code Roulez!</description>
	<lastBuildDate>Fri, 03 Feb 2012 00:43:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Something new for 2011: XML &amp; XSD, part 2</title>
		<link>http://codegumbo.com/index.php/2011/01/31/something-new-for-2011-xml-xsd-part-2/</link>
		<comments>http://codegumbo.com/index.php/2011/01/31/something-new-for-2011-xml-xsd-part-2/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 15:16:00 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[Something New]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://codegumbo.com/?p=481</guid>
		<description><![CDATA[I’m continuing my study of XML and XSD’s for January, and I realize that I ended my last post a bit abruptly.&#160; I explained that I can cast an XML datatype to a SQL Server datatype, without giving a lot of background on WHY that’s important.&#160;&#160; Understanding Types. Without going into too much detail about [...]]]></description>
			<content:encoded><![CDATA[<p>I’m <a href="http://codegumbo.com/index.php/2011/01/10/something-new-for-2011-xml-and-xsd/" target="_blank">continuing my study of XML and XSD’s</a> for January, and I realize that I ended my last post a bit abruptly.&#160; I explained that I can cast an XML datatype to a SQL Server datatype, without giving a lot of background on WHY that’s important.&#160;&#160; </p>
<h2>Understanding Types.</h2>
<p>Without going into too much detail about type, the basic reason for specifying a type for data transformations is validity; if you are expecting integer data, and the XML provides a string, then the basic contract is broken.&#160; An XSD defines a type of data expected, and if some other type is provided, the XML is invalid.</p>
<p>For example, run the following code:</p>
<p> <code style="font-size: 12px"><span style="color: blue">IF </span><span style="color: gray">NOT EXISTS ( </span><span style="color: blue">SELECT&#160; </span><span style="color: gray">*      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">FROM&#160;&#160;&#160; </span><span style="color: black">sys.xml_schema_collections xsc      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">WHERE&#160;&#160; </span><span style="color: black">name </span><span style="color: blue">= </span><span style="color: red">'MismatchDataType' </span><span style="color: gray">)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">CREATE XML SCHEMA </span><span style="color: black">COLLECTION MismatchDataType&#160; </span><span style="color: blue">AS      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: red">'&lt;xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;xsd:element name=&quot;IntValue&quot; type=&quot;xsd:integer&quot;/&gt;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/xsd:schema&gt;'       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: black">GO      </p>
<p></span><span style="color: blue">DECLARE </span><span style="color: #434343">@x </span><span style="color: blue">XML</span><span style="color: gray">(</span><span style="color: black">MismatchDataType</span><span style="color: gray">)      <br /></span><span style="color: blue">SET </span><span style="color: #434343">@x </span><span style="color: blue">= </span><span style="color: red">'&lt;IntValue&gt;100&lt;/IntValue&gt;'      <br /></span><span style="color: green">--SET @x = '&lt;IntValue&gt;String&lt;/IntValue&gt;'      </p>
<p></span><span style="color: blue">DROP XML SCHEMA </span><span style="color: black">COLLECTION MismatchDataType      <br />GO       <br /></span></code>
<p>It runs fine, but if you uncomment the second SET statement (where a string value is specified), you get the following error: </p>
<p><font color="#ff0000" face="cour">Msg 6926, Level 16, State 1, Line 4      <br />XML Validation: Invalid simple type value: &#8216;String&#8217;. Location: /*:IntValue[1]</font></p>
<p>What’s important to remember is that once you specify a type for an element, you may only cast that XML type to a matching SQL Server type (i.e., integer to integer, string to (n)varchar, etc.) when using the XQuery methods in SQL Server (.value(), etc.).&#160; This is easily debuggable to a seasoned database professional; if the XML type is string, and you store a value as 100, you can easily convert that to either an integer or varchar value:</p>
<p> <code style="font-size: 12px"><span style="color: blue">SELECT </span><span style="color: #434343">@x</span><span style="color: black">.value</span><span style="color: gray">(</span><span style="color: red">'IntValue[1]'</span><span style="color: gray">, </span><span style="color: red">'integer'</span><span style="color: gray">), </span><span style="color: #434343">@x</span><span style="color: black">.value</span><span style="color: gray">(</span><span style="color: red">'IntValue[1]'</span><span style="color: gray">, </span><span style="color: red">'varchar(3)'</span><span style="color: gray">)</span></code>
<p>&#160;</p>
<p>If you don’t specify a type, SQL Server can make certain assumptions regarding type conversion; however, typing your XML is one of those basic “good habits” that is foundational to application design.&#160; Knowing what to expect from your data, regardless of whether or not it’s stored in XML or a database makes troubleshooting a lot easier in the future.</p>
<h2>Complex vs. Simple Types</h2>
<p>The examples I’ve used so far all rely on what is known as a simple type in XML; a simple type contains no sub-elements or attributes.&#160; A complex element can&#160; contain either sub-elements or attributes.&#160; An XSD collection is especially useful when defining complex elements; the XSD allows database professionals to enforce validity in the shape of their XML, including which elements are required or not.</p>
<p>Most of the examples I’ve used so far have been simple elements, but a complex element enforced via an XSD would look something like&#160; (apologies for the formatting):</p>
<p> <code style="font-size: 12px"><span style="color: black"></span><span style="color: blue">CREATE XML SCHEMA </span><span style="color: black">COLLECTION XMLSample&#160; </span><span style="color: blue">AS      <br /></span><span style="color: red">'&lt;xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;      <br />&lt;xsd:element name=&quot;Parent&quot;&gt;       <br />&lt;xsd:complexType&gt;       <br />&lt;xsd:sequence&gt;       <br />&lt;xsd:element name=&quot;Child&quot; type=&quot;xsd:string&quot;/&gt;       <br />&lt;/xsd:sequence&gt;       <br />&lt;/xsd:complexType&gt;       <br />&lt;/xsd:element&gt;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/xsd:schema&gt;'       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: black">GO      <br /></span></code>
<p>&#160;</p>
<p>In essence, a complex type is the heart of a strongly-typed XML document;&#160; one of the major benefits of XML is the ability to encapsulate hierarchical data, and a complex type enforces the relationship between the elements (and attributes) encapsulated in that hierarchy much like foreign keys do for a relational database.&#160;&#160; The presence or absence of elements in the data when compared to the XSD validate the nature of the dataset. </p>
<h1></h1>
<h1>A stopping point…</h1>
<p>Unfortunately for you, I need to stop at this point.&#160; I promised myself to learn something new every month, and I feel like I have.&#160; However, there’s so much more to learn about this topic, and I’ve simply run out of time.&#160; I debated about spending a few more weeks on this, but then realized that I need to move on (I can always return to it in a few months) in order to stay energized about learning something new.&#160; When I do return to this topic, I’ll be sure to post a few summary links to keep everything related.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2011/01/31/something-new-for-2011-xml-xsd-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Something new for 2011: XML and XSD</title>
		<link>http://codegumbo.com/index.php/2011/01/10/something-new-for-2011-xml-and-xsd/</link>
		<comments>http://codegumbo.com/index.php/2011/01/10/something-new-for-2011-xml-and-xsd/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 01:19:05 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[Something New]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://codegumbo.com/index.php/2011/01/10/something-new-for-2011-xml-and-xsd/</guid>
		<description><![CDATA[As part of my New Year’s resolution for 2011, I vowed to do a deep-dive on something technical every month; for January, I’m focusing on XML.&#160; I’ve been using XML and XQuery in SQL Server for a while now (even presenting on it), but I still don’t consider myself an expert in the area.&#160; For [...]]]></description>
			<content:encoded><![CDATA[<p>As part of my New Year’s resolution for 2011, I vowed to do a deep-dive on something technical every month; for January, I’m focusing on XML.&#160; I’ve been using XML and XQuery in SQL Server for a while now (even presenting on it), but I still don’t consider myself an expert in the area.&#160; For example, I use a lot of untyped XML to transfer data between databases; I’ve never really tackled XSD (XML Schema Definition Language), and now’s the time.&#160; I’m reading <a href="http://beyondrelational.com/blogs/jacob/archive/2009/04/26/my-latest-book-the-art-of-xsd-sql-server-xml-schema-collections-available-for-free-download.aspx" target="_blank">The Art of XSD</a> by Jacob Sebastian to help get me started. </p>
<p>What’s XSD?&#160; In a nutshell, it’s an XML document which validates the structure of another XML document.&#160; From the perspective of a database developer, an XSD document describes how data should look in a dataset; if the data doesn’t match the description (i.e, if a table is missing a column), that dataset is invalid.&#160; The XSD document can be very precise, or it can offer options for the dataset, but in either case, the point of an XSD is to document the expectations about the dataset.&#160; XML without XSD is untyped; XML with an XSD is typed (although XSD’s do more than just provide information about the data types contained within the XML).</p>
<p>Let’s take a look at an untyped XML statement:</p>
<p> <code style="font-size: 12px"><span style="color: black"></span><span style="color: blue">DECLARE </span><span style="color: #434343">@NoXSD </span><span style="color: blue">XML      <br />SET </span><span style="color: #434343">@NoXSD </span><span style="color: blue">= </span><span style="color: red">'&lt;Test1&gt;Hello World!&lt;/Test1&gt;'      <br /></span><span style="color: blue">SELECT </span><span style="color: #434343">@NoXSD      <br /></span></code>
<p>&#160;</p>
<p>Simple and straightforward; I created an XML variable, and populated it with an XML fragment.&#160; I then pulled the data out of that fragment.&#160; In this example, we have an element named Test1; what happens if we have a typo when we populate the variable?</p>
<p> <code style="font-size: 12px"><span style="color: blue">SET </span><span style="color: #434343">@NoXSD </span><span style="color: blue">= </span><span style="color: red">'&lt;Test2&gt;Hello World!&lt;/Test2&gt;'      <br /></span><span style="color: blue">SELECT </span><span style="color: #434343">@NoXSD </span></code>
<p>&#160;</p>
<p>Nothing happens.&#160; It’s a well-formed XML fragment (no root tag, but it does have starting and ending tags); the XML engine in SQL Server doesn’t know that our fragment is supposed to have an element named Test1, so it accepts the fragment as valid.&#160; This is where an XSD comes in:</p>
<p> <code style="font-size: 12px"><span style="color: blue">IF </span><span style="color: gray">EXISTS(</span> <span style="color: blue">SELECT </span><span style="color: gray">* </span><span style="color: blue">FROM </span><span style="color: black">sys.xml_schema_collections </span><span style="color: blue">WHERE </span><span style="color: black">name</span> <span style="color: blue">=</span> <span style="color: red">'TestSchema'</span> <span style="color: gray">)      <br /></span><span style="color: blue">DROP XML SCHEMA&#160; </span><span style="color: black">COLLECTION TestSchema      <br />GO       </p>
<p></span><span style="color: blue">CREATE XML SCHEMA </span><span style="color: black">COLLECTION TestSchema </span><span style="color: blue">AS      <br /></span><span style="color: red">'&lt;xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;      <br />&lt;xsd:element name=&quot;Test1&quot; /&gt;       <br />&lt;/xsd:schema&gt;'       <br /></span><span style="color: black">GO      </p>
<p></span><span style="color: blue">DECLARE </span><span style="color: #434343">@XSD </span><span style="color: blue">XML</span> <span style="color: gray">(</span> <span style="color: black">TestSchema</span> <span style="color: gray">)&#160; </span><span style="color: green">--use the schema to validate (type) the xml      <br /></span><span style="color: blue">SET </span><span style="color: #434343">@XSD </span><span style="color: blue">= </span><span style="color: red">'&lt;Test1&gt;Hello, World!&lt;/Test1&gt;'      </p>
<p></span><span style="color: blue">SELECT </span><span style="color: #434343">@XSD </span></code>
<p>&#160;</p>
<p>Since the XML fragment matches the XSD,&#160; the assignment of data works; what happens when we assign a fragment that doesn’t match?</p>
<p> <code style="font-size: 12px"><span style="color: blue">SET </span><span style="color: #434343">@XSD </span><span style="color: blue">= </span><span style="color: red">'&lt;Test2&gt;Hello, World!&lt;/Test2&gt;' </span></code>
<p>We get a big fat error message:</p>
<p><font color="#ff0000" face="Courier New">XML Validation: Declaration not found for element &#8216;Test2&#8242;. Location: /*:Test2[1]</font></p>
<p>Straightforward, right?&#160; But now what?&#160; Well, let’s type the data in our schema:</p>
<p> <code style="font-size: 12px"><span style="color: blue">IF </span><span style="color: gray">EXISTS(</span> <span style="color: blue">SELECT </span><span style="color: gray">* </span><span style="color: blue">FROM </span><span style="color: black">sys.xml_schema_collections </span><span style="color: blue">WHERE </span><span style="color: black">name</span> <span style="color: blue">=</span> <span style="color: red">'TestSchema'</span> <span style="color: gray">)      <br /></span><span style="color: blue">DROP XML SCHEMA&#160; </span> <span style="color: black">COLLECTION TestSchema      <br />GO       </p>
<p></span><span style="color: blue">CREATE XML SCHEMA </span><span style="color: black">COLLECTION TestSchema </span><span style="color: blue">AS      <br /></span><span style="color: red">'&lt;xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;      <br />&lt;xsd:element name=&quot;Test1&quot; type=&quot;xsd:string&quot;/&gt;       <br />&lt;/xsd:schema&gt;'       <br /></span><span style="color: black">GO      </p>
<p></span><span style="color: blue">DECLARE </span><span style="color: #434343">@XSD </span><span style="color: blue">XML</span> <span style="color: gray">(</span> <span style="color: black">TestSchema</span> <span style="color: gray">)      <br /></span><span style="color: blue">SET </span><span style="color: #434343">@XSD </span><span style="color: blue">= </span><span style="color: red">'&lt;Test1&gt;Hello, World!&lt;/Test1&gt;' </span></code>
<p>&#160;</p>
<p>So; what does this mean?&#160; It means that we can now use the XQuery methods built into SQL Server to cast the data from the XML datatype to a SQL Server data type.</p>
<p> <code style="font-size: 12px"><span style="color: blue">SELECT </span><span style="color: #434343">@XSD</span> <span style="color: black">.value</span> <span style="color: gray">(</span> <span style="color: red">'(//Test1)[1]'</span> <span style="color: gray">, </span><span style="color: red">'varchar(50)'</span> <span style="color: gray">)</span> </code>
<p>&#160;</p>
<p>More to come, but that’s a good stopping place for now; we’ve built a simple XSD, and validated a simple datatype.&#160; I plan to spend some time learning about optional data elements next.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2011/01/10/something-new-for-2011-xml-and-xsd/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Querying XML in SQL Server 2005+: namespaces</title>
		<link>http://codegumbo.com/index.php/2010/09/01/querying-xml-in-sql-server-2005-namespaces/</link>
		<comments>http://codegumbo.com/index.php/2010/09/01/querying-xml-in-sql-server-2005-namespaces/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 18:21:38 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://codegumbo.com/index.php/2010/09/01/querying-xml-in-sql-server-2005-namespaces/</guid>
		<description><![CDATA[I recently helped a friend solve an XML problem, and thought I would post the solution here.&#160; Although there are lots of notes on how to use XQuery in SQL Server 2005+, this was a real world scenario that was trickier than I expected.&#160; The friend works for an insurance company broker, and in one [...]]]></description>
			<content:encoded><![CDATA[<p>I recently helped a friend solve an XML problem, and thought I would post the solution here.&#160; Although there are lots of notes on how to use XQuery in SQL Server 2005+, this was a real world scenario that was trickier than I expected.&#160; The friend works for an insurance company broker, and in one of their applications, accident questionnaires (and their answers) are stored in XML.&#160; This allows them to treat all questionnaires as the same, regardless of their origin as long as the QuestionCodes are common across vendors.</p>
<p>Below is the sample data that he was asking me about; he needed to get one question and answer per row into a data set:</p>
<p> <code style="font-size: 12px"><span style="color: black">     <br /></span><span style="color: blue">DECLARE </span><span style="color: #434343">@T </span><span style="color: blue">TABLE </span><span style="color: gray">( </span><span style="color: black">RowID </span><span style="color: blue">INT</span><span style="color: gray">, </span><span style="color: black">Fragment </span><span style="color: blue">XML </span><span style="color: gray">)      <br /></span><span style="color: blue">INSERT&#160; INTO </span><span style="color: #434343">@T      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">( </span><span style="color: black">RowID</span><span style="color: gray">, </span><span style="color: black">Fragment </span><span style="color: gray">)      <br /></span><span style="color: blue">VALUES&#160; </span><span style="color: gray">( </span><span style="color: black">1</span><span style="color: gray">, </span><span style="color: red">'&lt;Questionnaire xmlns=&quot;http://www.xxx.com/schemas/xxxXXXXXxxx.0&quot;&gt;      <br />&lt;Question&gt;       <br />&lt;QuestionCode&gt;74&lt;/QuestionCode&gt;       <br />&lt;Question&gt;Why did you wreck your car?&lt;/Question&gt;       <br />&lt;Answer&gt;I was drunk&lt;/Answer&gt;       <br />&lt;Explanation /&gt;       <br />&lt;/Question&gt;       <br />&lt;Question&gt;       <br />&lt;QuestionCode&gt;75&lt;/QuestionCode&gt;       <br />&lt;Question&gt;Why is the rum all gone?&lt;/Question&gt;       <br />&lt;Answer&gt;Because I drank it.&lt;/Answer&gt;       <br />&lt;Explanation /&gt;       <br />&lt;/Question&gt;       <br />&lt;/Questionnaire&gt;' </span><span style="color: gray">)      <br />,&#160;&#160;&#160;&#160;&#160;&#160; ( </span><span style="color: black">2</span><span style="color: gray">, </span><span style="color: red">'&lt;Questionnaire xmlns=&quot;http://www.xxx.com/schemas/xxxXXXXXxxx.0&quot;&gt;      <br />&lt;Question&gt;       <br />&lt;QuestionCode&gt;74&lt;/QuestionCode&gt;       <br />&lt;Question&gt;Why did you wreck your car?&lt;/Question&gt;       <br />&lt;Answer&gt;Stuart was drunk&lt;/Answer&gt;       <br />&lt;Explanation /&gt;       <br />&lt;/Question&gt;       <br />&lt;Question&gt;       <br />&lt;QuestionCode&gt;75&lt;/QuestionCode&gt;       <br />&lt;Question&gt;Why is the rum all gone?&lt;/Question&gt;       <br />&lt;Answer&gt;Because I made mojitos.&lt;/Answer&gt;       <br />&lt;Explanation /&gt;       <br />&lt;/Question&gt;       <br />&lt;/Questionnaire&gt;' </span><span style="color: gray">) </span></code>
<p>I thought it was a simple query; simply use the .nodes() method to rip each of the questions and corresponding answers into their own rows, but for some reason, when I ran the following, I got interesting results:</p>
<p> <code style="font-size: 12px"><span style="color: blue">SELECT&#160; </span><span style="color: black">t.RowID      <br />&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">, </span><span style="color: black">QuestionCode </span><span style="color: blue">= </span><span style="color: black">t1.frag.value</span><span style="color: gray">(</span><span style="color: red">'(QuestionCode)[1]'</span><span style="color: gray">, </span><span style="color: red">'int'</span><span style="color: gray">)      <br />&#160;&#160;&#160;&#160;&#160; , </span><span style="color: black">Question </span><span style="color: blue">= </span><span style="color: black">t1.frag.value</span><span style="color: gray">(</span><span style="color: red">'(Question)[1]'</span><span style="color: gray">, </span><span style="color: red">'varchar(max)'</span><span style="color: gray">)      <br />&#160;&#160;&#160;&#160;&#160; , </span><span style="color: black">Answer </span><span style="color: blue">= </span><span style="color: black">t1.frag.value</span><span style="color: gray">(</span><span style="color: red">'(Answer)[1]'</span><span style="color: gray">, </span><span style="color: red">'varchar(max)'</span><span style="color: gray">)      <br />&#160;&#160;&#160;&#160;&#160; , </span><span style="color: black">Explanation </span><span style="color: blue">= </span><span style="color: black">t1.frag.value</span><span style="color: gray">(</span><span style="color: red">'(Explanation)[1]'</span><span style="color: gray">, </span><span style="color: red">'varchar(max)'</span><span style="color: gray">)      <br /></span><span style="color: blue">FROM&#160;&#160;&#160; </span><span style="color: #434343">@t </span><span style="color: black">t      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">CROSS      <br /></span><span style="color: black">APPLY Fragment.nodes</span><span style="color: gray">(</span><span style="color: red">'//Questionnaire/Question'</span><span style="color: gray">) </span><span style="color: blue">AS </span><span style="color: black">t1 </span><span style="color: gray">( </span><span style="color: black">frag </span><span style="color: gray">)      </p>
<p></span><span style="color: black">RowID&#160; QuestionCode&#160;&#160;&#160; Question&#160;&#160;&#160; Answer&#160; Explanation      </p>
<p></span></code>
<p>That’s right, nothing.&#160; Strange, considering I’ve done variations of this query for a couple of years now to parse out firewall data fragments.&#160; I looked closer, and tried to see what was different about the XML fragment from this example compared to mine, and it was clear: a namespace reference.&#160;&#160; Most of the data I deal with is not true XML, but rather fragments I convert to XML in order to facilitate easy transformations.&#160; To test, I stripped the namespace line (<span style="color: red">xmlns=&quot;http://www.xxx.com/schemas/xxxXXXXXxxx.0&quot;</span> ) out, and voila!&#160; Results!</p>
<blockquote><table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="64">RowID</td>
<td width="64">QuestionCode</td>
<td width="64">Question</td>
<td width="64">Answer</td>
<td width="64">Explanation</td>
</tr>
<tr>
<td>1</td>
<td>74</td>
<td>Why did you wreck your car?</td>
<td>I was drunk</td>
</tr>
<tr>
<td>1</td>
<td>75</td>
<td>Why is the rum all gone?</td>
<td>Because I drank it.</td>
</tr>
<tr>
<td>2</td>
<td>74</td>
<td>Why did you wreck your car?</td>
<td>Stuart was drunk</td>
</tr>
<tr>
<td>2</td>
<td>75</td>
<td>Why is the rum all gone?</td>
<td>Because I made mojitos.</td>
</tr>
</tbody>
</table>
</blockquote>
<p>Well, that was great, because it showed me where the problem was but how do I fix it?&#160; I stumbled upon a solution, but to be honest, I’m not sure it’s the best one.&#160; If I modify my query to refer to any namespace (the old wildcard: *) like so:</p>
<p>&#160;</p>
<p> <code style="font-size: 12px"><span style="color: blue">SELECT&#160; </span><span style="color: black">t.RowID     <br />&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">, </span><span style="color: black">QuestionCode </span><span style="color: blue">= </span><span style="color: black">t1.frag.value</span><span style="color: gray">(</span><span style="color: red">'(*:QuestionCode)[1]'</span><span style="color: gray">, </span><span style="color: red">'int'</span><span style="color: gray">)     <br />&#160;&#160;&#160;&#160;&#160; , </span><span style="color: black">Question </span><span style="color: blue">= </span><span style="color: black">t1.frag.value</span><span style="color: gray">(</span><span style="color: red">'(*:Question)[1]'</span><span style="color: gray">, </span><span style="color: red">'varchar(max)'</span><span style="color: gray">)     <br />&#160;&#160;&#160;&#160;&#160; , </span><span style="color: black">Answer </span><span style="color: blue">= </span><span style="color: black">t1.frag.value</span><span style="color: gray">(</span><span style="color: red">'(*:Answer)[1]'</span><span style="color: gray">, </span><span style="color: red">'varchar(max)'</span><span style="color: gray">)     <br />&#160;&#160;&#160;&#160;&#160; , </span><span style="color: black">Explanation </span><span style="color: blue">= </span><span style="color: black">t1.frag.value</span><span style="color: gray">(</span><span style="color: red">'(*:Explanation)[1]'</span><span style="color: gray">, </span><span style="color: red">'varchar(max)'</span><span style="color: gray">)     <br /></span><span style="color: blue">FROM&#160;&#160;&#160; </span><span style="color: #434343">@t </span><span style="color: black">t     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">CROSS     <br /></span><span style="color: black">APPLY Fragment.nodes</span><span style="color: gray">(</span><span style="color: red">'//*:Questionnaire/*:Question'</span><span style="color: gray">) </span><span style="color: blue">AS </span><span style="color: black">t1 </span><span style="color: gray">( </span><span style="color: black">frag </span><span style="color: gray">)</span></code>
<p>&#160;</p>
<p>I get the correct results.</p>
<blockquote><table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="64">RowID</td>
<td width="64">QuestionCode</td>
<td width="64">Question</td>
<td width="64">Answer</td>
<td width="64">Explanation</td>
</tr>
<tr>
<td>1</td>
<td>74</td>
<td>Why did you wreck your car?</td>
<td>I was drunk</td>
</tr>
<tr>
<td>1</td>
<td>75</td>
<td>Why is the rum all gone?</td>
<td>Because I drank it.</td>
</tr>
<tr>
<td>2</td>
<td>74</td>
<td>Why did you wreck your car?</td>
<td>Stuart was drunk</td>
</tr>
<tr>
<td>2</td>
<td>75</td>
<td>Why is the rum all gone?</td>
<td>Because I made mojitos.</td>
</tr>
</tbody>
</table>
</blockquote>
<p>Here’s the question for any XML guru that stumbles along the way; is there a better way to do this?</p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2010/09/01/querying-xml-in-sql-server-2005-namespaces/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Speaking at Atlanta.MDF on Monday, June 14</title>
		<link>http://codegumbo.com/index.php/2010/06/09/speaking-at-atlanta-mdf-on-monday-june-14/</link>
		<comments>http://codegumbo.com/index.php/2010/06/09/speaking-at-atlanta-mdf-on-monday-june-14/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 23:13:27 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[User Groups]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://codegumbo.com/?p=422</guid>
		<description><![CDATA[Quick  note:  I&#8217;ll be presenting my thoughts on XML and relational design at next week&#8217;s meeting of AtlantaMDF.  If you&#8217;re in the area, please come join us for free pizza and education. http://atlantamdf.com You Got XML In My Database? What&#8217;s Up With That?]]></description>
			<content:encoded><![CDATA[<p>Quick  note:  I&#8217;ll be presenting my thoughts on XML and relational design at next week&#8217;s meeting of AtlantaMDF.  If you&#8217;re in the area, please come join us for free pizza and education.</p>
<p><a href="http://atlantamdf.com">http://atlantamdf.com</a><br />
<strong><span style="color: #000000;">You Got XML In My Database? What&#8217;s Up With That?<br />
</span></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2010/06/09/speaking-at-atlanta-mdf-on-monday-june-14/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Speaking today: PASS AppDev Virtual Chapter</title>
		<link>http://codegumbo.com/index.php/2010/05/11/speaking-today-pass-appdev-virtual-chapter/</link>
		<comments>http://codegumbo.com/index.php/2010/05/11/speaking-today-pass-appdev-virtual-chapter/#comments</comments>
		<pubDate>Tue, 11 May 2010 14:10:30 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[PASS]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://codegumbo.com/?p=414</guid>
		<description><![CDATA[I know it&#8217;s short notice, but to be honest, I totally forgot about this until a couple of weeks ago.  I&#8217;ll be presenting today at noon eastern on a LiveMeeting for the Application Developers Virtual Chapter of PASS.  Deets below: &#8220;You Got XML In My Database? What&#8217;s Up With That?&#8221; May 11th 12:00 PM EDT (GMT [...]]]></description>
			<content:encoded><![CDATA[<p>I know it&#8217;s short notice, but to be honest, I totally forgot about this until a couple of weeks ago.  I&#8217;ll be presenting today at noon eastern on a LiveMeeting for the Application <a href="http://appdev.sqlpass.org" target="_blank">Developers Virtual Chapter of PASS</a>.  Deets below:</p>
<p><strong>&#8220;You Got XML In My Database? What&#8217;s Up With That?&#8221;</strong><br />
<strong>May 11th 12:00 PM EDT (GMT -4)</strong><br />
<a href="http://appdev.sqlpass.org/LinkClick.aspx?fileticket=FN4vpQsMRo8%3d&amp;tabid=2003">Add to Calendar</a><br />
<strong>Presenter: Stuart Ainsworth</strong></p>
<p>A brief presentation exploring the marriage of XML and relational databases, including when it works and when it doesn&#8217;t. Coverage will include various use case scenarios, and some tips on how to improve performance using design techniques.</p>
<p><strong>Stuart Ainsworth</strong></p>
<p>Stuart R Ainsworth, MA, MEd is a Database Architect working in the realm of Financial Information Security; over the last 15 years, he&#8217;s worked as a Research Analyst, a report writer, a DBA, a programmer, and a public speaking professor. He&#8217;s one of the chapter leaders for AtlantaMDF, the Atlanta chapter of PASS. A master of air guitar, he has yet to understand the point of Rock Band (&#8220;You push buttons? What&#8217;s that all about?&#8221;).</p>
<p><strong>How do I view the presentation?<br />
</strong>Attendee URL:  <a href="https://www.livemeeting.com/cc/usergroups/join?id=4ZN5ST&amp;role=attend"><span style="text-decoration: underline;"><span style="color: #0000ff;">Live Meeting link</span></span></a></p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2010/05/11/speaking-today-pass-appdev-virtual-chapter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server &amp; XML: links of interest</title>
		<link>http://codegumbo.com/index.php/2009/05/14/sql-server-xml-links-of-interest/</link>
		<comments>http://codegumbo.com/index.php/2009/05/14/sql-server-xml-links-of-interest/#comments</comments>
		<pubDate>Thu, 14 May 2009 20:24:10 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://codegumbo.com/?p=310</guid>
		<description><![CDATA[I&#8217;ve been working on a project recently that requires using XML in SQL Server, so I&#8217;ve been searching the web to find references. Books Online is unusually obtuse on this subject (the samples are sub-par for this complex subject), so I&#8217;ve been trying to find good examples of how it works. Here&#8217;s some of the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a project recently that requires using XML in SQL Server, so I&#8217;ve been searching the web to find references. Books Online is unusually obtuse on this subject (the samples are sub-par for this complex subject), so I&#8217;ve been trying to find good examples of how it works. Here&#8217;s some of the better ones:</p>
<p><a href="http://weblogs.asp.net/jgalloway/archive/2007/02/16/passing-lists-to-sql-server-2005-with-xml-parameters.aspx">Passing lists to SQL Server 2005 with XML Parameters</a> Jon Galloway gives a short and sweet example of shredding an XML variable into rows.</p>
<p><a href="http://blogs.msdn.com/mrorke/archive/2005/06/01/423965.aspx">XQuery Inside SQL Server 2005</a> A longer set of examples using CROSS APPLY to associate XML data in a column to a value in a standard column. This is probably the most important query I needed, because we have a lot of XML values (config files) that are stored in a table; those config files are specific to particular clients.</p>
<p><a href="http://technet.microsoft.com/en-us/library/ms190798.aspx">SQL Server 2008 Books Online</a> Here&#8217;s the basic data elements; again, the vocabulary is dense, but you at least need to know where it is.</p>
<p>Now, on to FLWOR (which I&#8217;m just beginning to study): <a href="http://www.stylusstudio.com/xquery_flwor.html">Blooming FLWOR &#8211; An Introduction to the XQuery FLWOR Expression</a></p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2009/05/14/sql-server-xml-links-of-interest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>it&#8217;s all in how you look at it&#8230;</title>
		<link>http://codegumbo.com/index.php/2008/12/04/its-all-in-how-you-look-at-it/</link>
		<comments>http://codegumbo.com/index.php/2008/12/04/its-all-in-how-you-look-at-it/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 20:56:27 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://codegumbo.com/?p=17</guid>
		<description><![CDATA[Not sure how to classify this bit of useless knowledge, but I&#8217;m putting it out there in case it sparks some-one&#8217;s creative interest.  Most of my day job entails parsing firewall syslog data; syslog may be a standard method of streaming data from a firewall, but there&#8217;s really very little standard about what is contained [...]]]></description>
			<content:encoded><![CDATA[<p>Not sure how to classify this bit of useless knowledge, but I&#8217;m putting it out there in case it sparks some-one&#8217;s creative interest.  Most of my day job entails parsing firewall syslog data; syslog may be a standard method of streaming data from a firewall, but there&#8217;s really very little standard about what is contained inside the syslog stream.  Each vendor has their own method of recording what events occurred on the firewall; if you&#8217;re trying to compare data between firewall vendors, it gets complicated.</p>
<p>Here&#8217;s a stream from a Watchguard (version 8.x) firewall:</p>
<p><code>firewalld[126]: deny out eth1 293 udp 20 127 192.168.10.211 192.9.202.3 137 137 (default)</code></p>
<p>Here&#8217;s one from a Cisco Pix:</p>
<p><code>%PIX-6-710005: UDP request discarded from 10.1.1.26/137 to inside:10.1.255.255/netbios-ns</code></p>
<p>Now, there are various methods of parsing the data; if you&#8217;ve ever used SQL Server to parse a message string, you know that it can get quite ugly.  Especially if the data is irregular.  Speaking of regularity, you can use regular expressions if you&#8217;re parsing the data with a client (such as VBScript or .NET), but it&#8217;s not always the easiest thing to do.  However, some of the firewall vendors (Watchguard,version 9) have recently become my friends (as in I don&#8217;t really know them, but I like what they&#8217;re doing), and adopted a quote-delimited standard for syslog traffic, like so:</p>
<p><code>disp="Deny"   pri="1" policy="Unhandled Internal Packet-00" src_ip="192.168.16.141" dst_ip="192.168.16.255" pr="netbios-dgm/udp" src_port="138" dst_port="138" src_intf="1-Trusted" dst_intf="Firebox"      rc="101" msg="denied" pckt_len="229" ttl="128"</code></p>
<p>Why do I like this so much?  Parsing in SQL is still a bear, and the streams may still be irregular (different events may have different attributes in different orders), so using VBScript and regular expressions haven&#8217;t really improved.  Look at the sample again; what happens if we slap a little extra information to the beginning and end?</p>
<p><code>&lt;Msg disp="Deny"   pri="1" policy="Unhandled Internal Packet-00" src_ip="192.168.16.141" dst_ip="192.168.16.255" pr="netbios-dgm/udp" src_port="138" dst_port="138" src_intf="1-Trusted" dst_intf="Firebox"      rc="101" msg="denied" pckt_len="229" ttl="128" /&gt;</code></p>
<p>Suddenly, we have an XML fragment, which is MUCH easier to parse and manipulate, even in T-SQL (2005):<br />
<code><br />
<span style="#0000ff;">DECLARE </span>@Msg <span style="color: #0000ff;">XML<br />
SET </span>@Msg <span style="color: #808080;">=</span></code><span style="color: #ff0000;">&#8216;<code>&lt;Msg disp="Deny" pri="1" policy="Unhandled Internal Packet-00" src_ip="192.168.16.141" dst_ip="192.168.16.255" pr="netbios-dgm/udp" src_port="138" dst_port="138" src_intf="1-Trusted" dst_intf="Firebox" rc="101" msg="denied" pckt_len="229" ttl="128" /&gt;</code>&#8216;<br />
</span><br />
<code><br />
<span style="#0000ff;">SELECT </span>@Msg.value (</code><span style="color: #ff0000;">&#8216;<code>(Msg/@disp)[1]</code>&#8216;</span>,<span style="color: #ff0000;">&#8216;<code>VARCHAR(50)</code>&#8216;</span><code>)</code></p>
<p><code><br />
</code>I realize this is a very specific tip for a very select group of people, but I thought the exercise was worth pointing out; raw data may not always be in the format we want, but if it is standard and predictable, there may be simple methods of manipulating it.  XML is where you find it.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2008/12/04/its-all-in-how-you-look-at-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

