using sql queries to generate xml- formatted data joline morrison mike morrison department of...
TRANSCRIPT
Using SQL Queries to Using SQL Queries to Generate XML-Generate XML-
Formatted Data Formatted Data Joline MorrisonJoline Morrison
Mike MorrisonMike Morrison
Department of Computer ScienceDepartment of Computer Science
University of Wisconsin-Eau ClaireUniversity of Wisconsin-Eau Claire
OutlineOutline
Study motivationsStudy motivations Overview of XMLOverview of XML XML SQL query syntaxXML SQL query syntax
OracleOracle SQL ServerSQL Server
ConclusionsConclusions Platform strengths/weaknessesPlatform strengths/weaknesses Suggestions for student activitiesSuggestions for student activities
Study MotivationsStudy Motivations
XML has become the de facto standard XML has become the de facto standard for sharing data acfor sharing data across diverse ross diverse applications and hardware/software applications and hardware/software platformsplatforms
SQL-2003 ISO specifies standards for SQL-2003 ISO specifies standards for forming queries that retrieve XML-forming queries that retrieve XML-formatted data formatted data
But most organizational data is stored in But most organizational data is stored in relational databases...relational databases...
XML OverviewXML Overview
Defines Defines structured datastructured data using using markup markup languagelanguage notation notation
Structured data: Data defined in a specific Structured data: Data defined in a specific and unambiguous formatand unambiguous format
Markup language:Markup language: Uses tags or other symbols to specify document Uses tags or other symbols to specify document
formattingformatting Tags are defined in a document type definition Tags are defined in a document type definition
(DTD)(DTD)
XML FeaturesXML Features
XML DTD allows developers to define XML DTD allows developers to define custom tags to define the syntax, custom tags to define the syntax, semantics, and structure of datasemantics, and structure of data
XML documents are XML documents are text filestext files Can be shared across different applications Can be shared across different applications
and hardware/software platformsand hardware/software platforms
Example XML DocumentExample XML Document<?xml version="1.0" encoding="UTF-8" ?><books> <book isbn="99999-99999"> <title>CS 365: A Visual History</title> <authors> <author id="100">
<firstname>Tom</firstname><lastname>Moore</lastname>
</author> <author id="101">
<firstname>Leonard</firstname><lastname>Larsen</lastname>
</author> </authors> <publisher>UWEC-CS Press</publisher> <publishyear>2000</publishyear> <price type="USD">10.00</price> </book>
Attribute value
Element value
Aggregated dataelement
Prolog
Representing Data Relationships Representing Data Relationships in XML Documentsin XML Documents
Relationships limited to 1:MRelationships limited to 1:M Relationships must be hierarchicalRelationships must be hierarchical
<book isbn="99999-99999"> <title>CS 365: A Visual History</title> <authors> <author id="100">
<firstname>Tom</firstname><lastname>Moore</lastname>
</author> <author id="101">
<firstname>Leonard</firstname><lastname>Larsen</lastname>
</author> </authors>
OutlineOutline
Research motivationsResearch motivations Overview of XMLOverview of XML XML SQL query syntaxXML SQL query syntax
OracleOracle SQL ServerSQL Server
ConclusionsConclusions Platform strengths/weaknessesPlatform strengths/weaknesses Suggestions for student activitiesSuggestions for student activities
XML SQL QueriesXML SQL Queries
Approach: create SQL queries that Approach: create SQL queries that retrieve relational database data and retrieve relational database data and "wrap" it in predefined XML tags "wrap" it in predefined XML tags
ISO-2003 SQL standards specify required ISO-2003 SQL standards specify required functionality but don't prescribe syntaxfunctionality but don't prescribe syntax Different vendors implement the same Different vendors implement the same
functionality quite differently!functionality quite differently!
Example XML SQL Queries Example XML SQL Queries
Operations: Operations: Format values as elements & attributesFormat values as elements & attributes Create aggregate elements to represent Create aggregate elements to represent
relationshipsrelationships
Platforms: Oracle & SQL ServerPlatforms: Oracle & SQL Server
Oracle:Oracle:Formatting Data as ElementsFormatting Data as Elements
SELECT XMLElement("department", department_name)FROM university_department ORDER BY department_name;
<department>Accounting</department><department>Chemistry</department><department>Computer Science</department>…
XMLElementXMLElement function creates a new XML function creates a new XML elementelement
Parameters specify element names and Parameters specify element names and associated data valuesassociated data values
Oracle:Oracle:Formatting Data as AttributesFormatting Data as Attributes
SELECT XMLElement("department", XMLAttributes(department_id AS "id", department_name AS "name"))FROM university_department ORDER BY department_name;
<department id="2" name="Accounting"></department><department id="5" name="Chemistry"></department><department id="4" name="Computer Science"></department>
You first use You first use XMLElementXMLElement to create the to create the elementelement XMLAttributesXMLAttributes function retrieves and formats function retrieves and formats
one or more data values as attributesone or more data values as attributes
Oracle:Oracle:Creating Aggregate DataCreating Aggregate Data
SELECT XMLElement("department", XMLAgg(XMLElement("course", course_name)))FROM university_department a INNER JOIN university_course bON a.department_id = b.department_idGROUP BY department_name;
<department> <course>ACCT 201</course> <course>ACCT 312</course></department><department><course>CHEM 205</course></department><department><course>CS 245</course></department>
Create the parent element using Create the parent element using XMLElementXMLElement Retrieve the child values as elements using the Retrieve the child values as elements using the
XMLAggXMLAgg functionfunction
Oracle:Oracle:Nesting XML FunctionsNesting XML Functions
SELECT XMLElement("department", XMLAttributes(department_name AS "name"), XMLElement("courses", (XMLAgg(XMLElement("course", course_name)))))FROM university_department a INNER JOIN university_course bON a.department_id = b.department_idWHERE a.department_id = 1GROUP BY department_name;<department name="Management Information Systems"> <courses> <course>MIS 240</course> <course>MIS 310</course> <course>MIS 344</course></courses></department>
Oracle allows you to nest XML functions to Oracle allows you to nest XML functions to retrieve data in a variety of formats...retrieve data in a variety of formats...
SQL Server:SQL Server:General ApproachGeneral Approach
SELECT ...FOR XML Mode[, ELEMENTS]
ModeMode values: values: RAWRAW: returns each record as an XML element enclosed in a : returns each record as an XML element enclosed in a
<row> element<row> element AUTOAUTO: returns each record as a named XML element and : returns each record as a named XML element and
hierarchically nests child nodes from JOIN querieshierarchically nests child nodes from JOIN queries EXPLICITEXPLICIT: provides precise control on how data values are : provides precise control on how data values are
formattedformatted ELEMENTS option: formats each field as a separate ELEMENTS option: formats each field as a separate
elementelement
SQL Server:SQL Server:Formatting Data as ElementsFormatting Data as Elements
SELECT DepartmentName, CourseNameFROM UniversityDepartment a INNER JOIN UniversityCourse bON a.DepartmentID = b.DepartmentIDORDER BY DepartmentName, CourseNameFOR XML RAW
<row DepartmentName="Accounting" CourseName="ACCT 201" /><row DepartmentName="Accounting" CourseName="ACCT 312" /><row DepartmentName="Chemistry" CourseName="CHEM 205" />
RAWRAW modemode::
SQL Server:SQL Server:Formatting Data as ElementsFormatting Data as Elements
SELECT DepartmentName, CourseNameFROM UniversityDepartment a INNER JOIN UniversityCourse bON a.DepartmentID = b.DepartmentIDORDER BY DepartmentName, CourseNameFOR XML RAW, ELEMENTS
<row> <DepartmentName>Accounting</DepartmentName> <CourseName>ACCT 201</CourseName></row><row> <DepartmentName>Accounting</DepartmentName> <CourseName>ACCT 312</CourseName></row>
RAW, ELEMENTSRAW, ELEMENTS option: option:
SQL Server:SQL Server:Formatting Data as ElementsFormatting Data as Elements
SELECT DepartmentName, CourseNameFROM UniversityDepartment dept INNER JOIN UniversityCourse courseON dept.DepartmentID = course.DepartmentIDORDER BY DepartmentName, CourseNameFOR XML AUTO, ELEMENTS
<dept> <DepartmentName>Accounting</DepartmentName> <course> <CourseName>ACCT 201</CourseName> </course> <course> <CourseName>ACCT 312</CourseName> </course></dept><dept>
AUTO, ELEMENTSAUTO, ELEMENTS option: option:
SQL Server:SQL Server:Formatting Data as AttributesFormatting Data as Attributes
SELECT DepartmentName, CourseNameFROM UniversityDepartment dept INNER JOIN UniversityCourse courseON dept.DepartmentID = course.DepartmentIDORDER BY DepartmentName, CourseNameFOR XML AUTO
<dept DepartmentName="Accounting"> <course CourseName="ACCT 201" /> <course CourseName="ACCT 312" /></dept><dept DepartmentName="Chemistry"> <course CourseName="CHEM 205" /></dept><dept DepartmentName="Computer Science"> <course CourseName="CS 245" /></dept>
AUTO mode (remove ELEMENTS option)AUTO mode (remove ELEMENTS option)::
SQL Server:SQL Server:XML EXPLICIT modeXML EXPLICIT mode
Allows you to specify parent and child Allows you to specify parent and child elements precisely elements precisely
Each level is defined within a separate queryEach level is defined within a separate query Queries are joined using the UNION operator Queries are joined using the UNION operator
Level 1 query:Level 1 query: SELECT 1 as tag, NULL as parent, DepartmentName AS [dept!1!name!element]FROM UniversityDepartmentORDER BY DepartmentNameFOR XML EXPLICIT<dept> <name>Accounting</name></dept><dept> <name>Chemistry</name></dept>
XML EXPLICIT query with 2 levelsXML EXPLICIT query with 2 levelsSELECT 1 As tag, NULL As parent, DepartmentName As [dept!1!dname], NULL As [course!2!cname!element], NULL As [course!2!title!element]FROM UniversityDepartmentUNIONSELECT 2 As tag, 1 As parent, DepartmentName,CourseName, CourseTitleFROM UniversityDepartment a INNER JOIN UniversityCourse bON a.DepartmentID = b.DepartmentIDORDER BY [dept!1!dname], [course!2!cname!element]FOR XML EXPLICIT<dept dname="Accounting"> <course> <cname>ACCT 201</cname><title>Accounting I</title> </course> <course> <cname>ACCT 312</cname> <title>Managerial Accounting</title> </course></dept>
OutlineOutline
Study motivationsStudy motivations Overview of XMLOverview of XML XML SQL query syntaxXML SQL query syntax
OracleOracle SQL ServerSQL Server
ConclusionsConclusions Platform strengths/weaknessesPlatform strengths/weaknesses Suggestions for student activitiesSuggestions for student activities
ConclusionsConclusions
Platform strengths/weaknessesPlatform strengths/weaknesses Oracle syntax seems a little shorter and Oracle syntax seems a little shorter and
cleaner overallcleaner overall SQL Server automatically creates SQL Server automatically creates
aggregate data from JOIN queriesaggregate data from JOIN queries SQL Server EXPLICIT mode provides SQL Server EXPLICIT mode provides
precise formatting precise formatting At a high cost!At a high cost!
ConclusionsConclusions
Platform strengths/weaknesses Platform strengths/weaknesses (continued)(continued) Oracle allows you to create aggregated Oracle allows you to create aggregated
data in a way that SQL Server does not:data in a way that SQL Server does not:
<department name="Management Information Systems"> <courses> <course>MIS 240</course> <course>MIS 310</course> <course>MIS 344</course></courses></department>
ConclusionsConclusions
Suggestions for student activitiesSuggestions for student activities Manually create XML-formatted data for a series Manually create XML-formatted data for a series
of related database tablesof related database tables Create queries in both Oracle and SQL Server to Create queries in both Oracle and SQL Server to
retrieve XML-formatted data and analyze syntax retrieve XML-formatted data and analyze syntax differencesdifferences
Generate XML-formatted data and display it in a Generate XML-formatted data and display it in a browserbrowser
Generate XML-formatted data and transform it Generate XML-formatted data and transform it into and HTML document using XSLTsinto and HTML document using XSLTs
Additional ResourcesAdditional Resources
Scripts to create databases in Oracle & Scripts to create databases in Oracle & SQL ServerSQL Server
Electronic copy of the paperElectronic copy of the paper Electronic copy of the slideshowElectronic copy of the slideshow
http://www.cs.uwec.edu/~morrisjp/Public/Conferences/MICS