Download presentation
Presentation is loading. Please wait.
Published byJuliana Shields Modified over 9 years ago
1
Using SQL Queries to Generate XML- Formatted Data Joline Morrison Mike Morrison Department of Computer Science University of Wisconsin-Eau Claire
2
Outline Study motivations Study motivations Overview of XML Overview of XML XML SQL query syntax XML SQL query syntax Oracle SQL Server Conclusions Conclusions Platform strengths/weaknesses Suggestions for student activities
3
Study Motivations XML has become the de facto standard for sharing data across diverse applications and hardware/software platforms XML has become the de facto standard for sharing data across diverse applications and hardware/software platforms SQL-2003 ISO specifies standards for forming queries that retrieve XML- formatted data SQL-2003 ISO specifies standards for forming queries that retrieve XML- formatted data But most organizational data is stored in relational databases... But most organizational data is stored in relational databases...
4
XML Overview Defines structured data using markup language notation Defines structured data using markup language notation Structured data: Data defined in a specific and unambiguous format Markup language: F Uses tags or other symbols to specify document formatting F Tags are defined in a document type definition (DTD)
5
XML Features XML DTD allows developers to define custom tags to define the syntax, semantics, and structure of data XML DTD allows developers to define custom tags to define the syntax, semantics, and structure of data XML documents are text files XML documents are text files Can be shared across different applications and hardware/software platforms
6
Example XML Document CS 365: A Visual History Tom Moore Leonard Larsen UWEC-CS Press 2000 10.00 Attribute value Element value Aggregated data element Prolog
7
Representing Data Relationships in XML Documents Relationships limited to 1:M Relationships limited to 1:M Relationships must be hierarchical Relationships must be hierarchical CS 365: A Visual History Tom Moore Leonard Larsen
8
Outline Research motivations Research motivations Overview of XML Overview of XML XML SQL query syntax XML SQL query syntax Oracle SQL Server Conclusions Conclusions Platform strengths/weaknesses Suggestions for student activities
9
XML SQL Queries Approach: create SQL queries that retrieve relational database data and "wrap" it in predefined XML tags Approach: create SQL queries that retrieve relational database data and "wrap" it in predefined XML tags ISO-2003 SQL standards specify required functionality but don't prescribe syntax ISO-2003 SQL standards specify required functionality but don't prescribe syntax Different vendors implement the same functionality quite differently!
10
Example XML SQL Queries Operations: Operations: Format values as elements & attributes Create aggregate elements to represent relationships Platforms: Oracle & SQL Server Platforms: Oracle & SQL Server
11
Oracle: Formatting Data as Elements SELECT XMLElement("department", department_name) FROM university_department ORDER BY department_name; Accounting Chemistry Computer Science … XMLElement function creates a new XML element XMLElement function creates a new XML element Parameters specify element names and associated data values Parameters specify element names and associated data values
12
Oracle: Formatting Data as Attributes SELECT XMLElement("department", XMLAttributes(department_id AS "id", department_name AS "name")) FROM university_department ORDER BY department_name; You first use XMLElement to create the element You first use XMLElement to create the element XMLAttributes function retrieves and formats one or more data values as attributes
13
Oracle: Creating Aggregate Data SELECT XMLElement("department", XMLAgg(XMLElement("course", course_name))) FROM university_department a INNER JOIN university_course b ON a.department_id = b.department_id GROUP BY department_name; ACCT 201 ACCT 312 CHEM 205 CS 245 Create the parent element using XMLElement Create the parent element using XMLElement Retrieve the child values as elements using the XMLAgg function Retrieve the child values as elements using the XMLAgg function
14
Oracle: Nesting 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 b ON a.department_id = b.department_id WHERE a.department_id = 1 GROUP BY department_name; MIS 240 MIS 310 MIS 344 Oracle allows you to nest XML functions to retrieve data in a variety of formats... Oracle allows you to nest XML functions to retrieve data in a variety of formats...
15
SQL Server: General Approach SELECT... FOR XML Mode[, ELEMENTS] Mode values: Mode values: RAW: returns each record as an XML element enclosed in a element AUTO: returns each record as a named XML element and hierarchically nests child nodes from JOIN queries EXPLICIT: provides precise control on how data values are formatted ELEMENTS option: formats each field as a separate element ELEMENTS option: formats each field as a separate element
16
SQL Server: Formatting Data as Elements SELECT DepartmentName, CourseName FROM UniversityDepartment a INNER JOIN UniversityCourse b ON a.DepartmentID = b.DepartmentID ORDER BY DepartmentName, CourseName FOR XML RAW RAW mode: RAW mode:
17
SQL Server: Formatting Data as Elements SELECT DepartmentName, CourseName FROM UniversityDepartment a INNER JOIN UniversityCourse b ON a.DepartmentID = b.DepartmentID ORDER BY DepartmentName, CourseName FOR XML RAW, ELEMENTS Accounting ACCT 201 Accounting ACCT 312 RAW, ELEMENTS option: RAW, ELEMENTS option:
18
SQL Server: Formatting Data as Elements SELECT DepartmentName, CourseName FROM UniversityDepartment dept INNER JOIN UniversityCourse course ON dept.DepartmentID = course.DepartmentID ORDER BY DepartmentName, CourseName FOR XML AUTO, ELEMENTS Accounting ACCT 201 ACCT 312 AUTO, ELEMENTS option: AUTO, ELEMENTS option:
19
SQL Server: Formatting Data as Attributes SELECT DepartmentName, CourseName FROM UniversityDepartment dept INNER JOIN UniversityCourse course ON dept.DepartmentID = course.DepartmentID ORDER BY DepartmentName, CourseName FOR XML AUTO AUTO mode (remove ELEMENTS option): AUTO mode (remove ELEMENTS option):
20
SQL Server: XML EXPLICIT mode Allows you to specify parent and child elements precisely Allows you to specify parent and child elements precisely Each level is defined within a separate query Each level is defined within a separate query Queries are joined using the UNION operator Queries are joined using the UNION operator Level 1 query: SELECT 1 as tag, NULL as parent, DepartmentName AS [dept!1!name!element] FROM UniversityDepartment ORDER BY DepartmentName FOR XML EXPLICIT Accounting Chemistry
21
XML EXPLICIT query with 2 levels SELECT 1 As tag, NULL As parent, DepartmentName As [dept!1!dname], NULL As [course!2!cname!element], NULL As [course!2!title!element] FROM UniversityDepartment UNION SELECT 2 As tag, 1 As parent, DepartmentName,CourseName, CourseTitle FROM UniversityDepartment a INNER JOIN UniversityCourse b ON a.DepartmentID = b.DepartmentID ORDER BY [dept!1!dname], [course!2!cname!element] FOR XML EXPLICIT ACCT 201 Accounting I ACCT 312 Managerial Accounting
22
Outline Study motivations Study motivations Overview of XML Overview of XML XML SQL query syntax XML SQL query syntax Oracle SQL Server Conclusions Conclusions Platform strengths/weaknesses Suggestions for student activities
23
Conclusions Platform strengths/weaknesses Platform strengths/weaknesses Oracle syntax seems a little shorter and cleaner overall SQL Server automatically creates aggregate data from JOIN queries SQL Server EXPLICIT mode provides precise formatting F At a high cost!
24
Conclusions Platform strengths/weaknesses (continued) Platform strengths/weaknesses (continued) Oracle allows you to create aggregated data in a way that SQL Server does not: MIS 240 MIS 310 MIS 344
25
Conclusions Suggestions for student activities Suggestions for student activities Manually create XML-formatted data for a series of related database tables Create queries in both Oracle and SQL Server to retrieve XML-formatted data and analyze syntax differences Generate XML-formatted data and display it in a browser Generate XML-formatted data and transform it into and HTML document using XSLTs
26
Additional Resources Scripts to create databases in Oracle & SQL Server Scripts to create databases in Oracle & SQL Server Electronic copy of the paper Electronic copy of the paper Electronic copy of the slideshow Electronic copy of the slideshow http://www.cs.uwec.edu/~morrisjp/Public/Conferences/MICS
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.