Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML and SQL Server Better friends than you thought Matt Hartman.

Similar presentations


Presentation on theme: "XML and SQL Server Better friends than you thought Matt Hartman."— Presentation transcript:

1 XML and SQL Server Better friends than you thought Matt Hartman

2 I’m a DBA not a developer. Why should I give two hoots about XML anyway? 1.XML is widely used for data transfers 2.XML is the Microsoft standard format for Configuration 3.XML data isn’t always worth parsing and storing in relational format 4.Sometimes someone hands you a giant dump of XML and you are told to “deal with it.” 2

3 What this Presentation Covers 1.What is XML and all these other “some other letter(s)”-ML’s? 2.XML in SQL Server: FOR XML, XPath, XQuery and the XML data type 3.A real world example 4.Additional tools that might help with those giant XML dumps 3

4 Built in SQL Server XML Toolset 1.XML Data Type 2.XQuery 3.XPath 4.FOR XML 4

5 What is XML? ML means “Mark-up Language” Normal text is like this. There is nothing that tells the computer what to do. So all you get back is the text without formatting. Mark Up Languages have Tags Marked up language has tags, or a notification to the computer that it should do something with the text. In a mark up language such as HTML, a tag might tell a computer to put some text in bold. Or maybe you want something more complex than that. Either way, the open tag: and closing tag: tell the computer, based on the mark up language rules, what it should do with the text in between the tags. Or in the case of Twitter and a #hashtag, what the computer should do with the single word following the #. In this case it means to make your important snippet of thought searchable under the specific #WordsWithoutSpaces that you broadcast to the entire world. 5

6 What is XML? X means “eXtensible”  X because the World Wide Web Consortium doesn’t realize that the word “extensible” starts with an “e.”  Extensible means you can “extend it” to define your own unique data, as long as you follow some basic rules. 6

7 What is XML? XML is a mark-up language approach to defining any given hierarchical dataset. Heirarchical data: A building has one or more rooms. Each room has one or more closets. Each closet has one or more articles of clothing. Each room has one or more articles of furniture. But an article of clothing never “has a building,” nor does a closet have a room. 7

8 XML Format XML can define anything, as long as it is consistent and follows standard rules: Favorite Shoes Old Shoes New Shoes Cute Shoes Ugly Shoes 8

9 XML Format 1.The data must be hierarchical and ordered. 2.All elements must have a closing tag, or a single combined open and close tag. 3.You must close nested elements before you close their parent. 4.Attributes are assigned to elements on a one to one basis. 5.Sorry DBA’s and SQL developers, but XML is case sensitive. 9

10 XML Namespaces and XSLT 1.Namespaces: Landon Donovan Michael Bradley 2.XSLT 10

11 SQL Server and XML 1.Store XML data in SQL with the XML data type. 2.Query XML data types directly with XQuery using XPath references. 3.Shred XML data and import it into relational tables, rows and columns using OPENXML. 4.Output relational data to XML format using FOR XML.

12 XML Data Type 1.Stores valid XML 2.2GB Maximum Size 3.You can query it to extract a derived XML value using XQuery 4.Great for storing data in transit 12

13 XQuery and XPath 1.XQuery 1.Available using Query method of an XML Data Type 2.Language with it’s own syntax and rules 2.XPath 1.Standard expressions to navigate XML 2.XQuery uses XPath references

14 XPath /root /playerany element (by name) /player/node test /player/@numberattribute /player[@number=1]predicate /player/..axis (parent) /player/name/text()method

15 XQuery: FLWOR F: Forfor $p in players/player L: Letlet $g := $p/careergoals W: Wherewhere $g > 0 O: Order Byorder by ($g)[1] R: Return {$g/text()} 15

16 Relational Data to XML Output FOR XML Clause FOR XML Raw“row” elements w/ attributes FOR XML Autospecify columns as attributes or elements More Useful: FOR XML Pathspecify exact XML to return 16

17 OPENXML 1.Use OPENXML to import XML data into SELECT * FROM OPENXML (@idoc, 'players/player', 2) WITH (name varchar(10), careergoals int); 1.Foreign Keys can be difficult if not defined in XML 17

18 Real World Example 1.Import Dona Ana County property records (1.5 GB file) into SQL Server 2.Generate database schema based on XSLT of the properties XML 3.Few primary key candidates defined in the XML data 4.No foreign key relationships defined 18

19 Additional Import Approaches 1..NET Code http://blogs.msdn.com/b/dilkushp/archive/2013/09/15/case-study- import-data-from-xml-to-sql-part-1.aspx 1.Exult XML Conversion Wizard http://www.novixys.com/ExultSQLServer/19

20 Thank you to our Sponsors

21 Thank you! Register at http://www.sqlpass.org and join LCEP (southwest)http://www.sqlpass.org Free stuff and Q&A We send updates, so check your email Come back next month! Next month’s meeting at City Hall in Las Cruces. Details forthcoming…


Download ppt "XML and SQL Server Better friends than you thought Matt Hartman."

Similar presentations


Ads by Google