1.27k likes | 1.37k Views
SAS912: XML Support in ASA. Matthew Young-Lai ASA Query Processing iAnywhere Solutions, Inc. August 2003. Introduction to XML. What is XML?. Extensible Markup Language A text markup language like HTML More flexible because the tags are not pre-defined. Elements.
E N D
SAS912: XML Support in ASA Matthew Young-LaiASA Query ProcessingiAnywhere Solutions, Inc.August 2003
What is XML? • Extensible Markup Language • A text markup language like HTML • More flexible because the tags are not pre-defined
Elements • An XML document contains elements: <name> content </name> • Elements can be nested: <name> <name1> content </name1> </name>
Attributes • Elements can contain attributes: <name att1=‘content’ att2=“content” > </name> • Can abbreviate the end tag if an element contains nothing but attributes: <name att1=‘content’ />
Other Structures • CDATA sections • Processing instructions • Namespace declarations and prefixes • Comments • Entity references • Character references
well-formed valid Legal XML • Two levels of legality:
Well-formed XML • Single root element • All opened tags must be closed • Wrong: <li> … <li> … • No overlapping elements • Wrong: <b>…<i> … </b> … </i> • etc.
Valid XML • Elements and attributes are used “correctly” according to a model specified with either • A DTD • context free grammar • An XML Schema • DTD plus data types • XML format
XML Support in ASA • Intended for import and export • Not intended to turn ASA into a hierarchical database • Storage of XML as strings is reasonable in some cases
2. Exporting XML Tagging Result Sets
Other Points of Comparison • Efficiency • EXPLICIT can be better than SQLX (unions vs subqueries) • Compatibility • SQLX is a draft standard that other vendors are committed to supporting
2.1 FOR XML RAW • Append to a query: select emp_id as id, emp_fname as name from employee FOR XML RAW
FOR XML RAW (continued) • Result is a 1-row, 1-column result set • The column is a string in the database encoding <row id="102" name="Fran"/> <row id="105"name="Matthew"/> <row id="129" name="Philip"/> <row id="148" name="Julie"/> …
FOR XML RAW (continued) • Result is not well-formed unless you insert a root element: • Example select XMLELEMENT( name root, (select emp_id as id, emp_fname as name from employee for xml raw))
FOR XML RAW (continued) • Result <root> <row id="102" name="Fran"/> <row id="105"name="Matthew"/> <row id="129" name="Philip"/> <row id="148" name="Julie"/> … </root>
FOR XML RAW (continued) • Use ELEMENTS to generate elements rather than attributes • Example select emp_id as id, emp_fname as name from employee for xml raw, ELEMENTS
FOR XML RAW (continued) • Result <row> <id>102</id> <name>Fran</name> </row> <row> <id>105></id> <name>Matthew</name> </row> …
Name Quoting • Illegal names are quoted • Example select emp_id as “emp id” from employee for xml raw
Name Quoting (Continued) • Result <row emp_x0020_id="102"/> <row emp_x0020_id="105"/> <row emp_x0020_id="129"/> <row emp_x0020_id="148"/>
Content Quoting • All markup characters are quoted in content • Example • select ‘<y/>’ x • for xml raw, elements • Result • <row><x><y/></x></row>
Content Quoting (continued) • Quoting is not done for content of type xml • Example • select cast( ‘<y/>’ as xml ) x • for xml raw, elements • Result • <row><x><y/></x></row>
Base64 Encoding • Binary columns are Base64 encoded create variable x long binary; set x = xp_read_file( ‘c:\\windows\\explorer.exe’ ); select x for xml raw
Base64 Encoding (continued) • Result <row x="TVqQAAMAAAAEAAAA//8 AALgAAAAAAAAAQAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAA6AAAAA4fug 4AtAnNIbgBTM0hVGhpcyBwcm9ncmF TIGNhbm5vdCBiZSBydW4gaW4gRE9T …"/>
Treatment of NULLs • By default, elements or attributes generated by NULL values are omitted • Example select 1 x, NULL y for xml raw • Result <row x=“1”/>
Treatment of NULLs (continued) • Example Select 1 x, NULL y for xml raw, elements • Result <row> <x>1</x> </row>
Treatment of NULLs (continued) • Use the FOR_XML_NULL_TREATMENT option to change this: set temporary option for_xml_null_treatment=empty • Default is “omit”
Treatment of NULLs (continued) • Example: select 1 x, NULL y for xml raw • Result: <row x=“1” y=“”/>
Treatment of NULLs (continued) • Example: select 1 x, NULL y for xml raw, elements • Result: <row> <x>1</x> <y/> </row>
Schema of the Result • If you need a DTD or XML Schema, you must write it yourself • This applies to all export techniques
2.2 FOR XML AUTO • Append to a query: select e.emp_id, s.cust_id, s.region from employee e key join sales_order s order by e.emp_id FOR XML AUTO • Splits up the result set • vertically by which tables are referenced • horizontally by values
Vertical Splitting e.emp_id s.cust_id s.region 129 119 Eastern 129 119 Western 129 131 Eastern 195 109 Eastern 195 121 Central
Vertical Splitting e.emp_id s.cust_id s.region 129 119 Eastern 129 119 Western 129 131 Eastern 195 109 Eastern 195 121 Central
Horizontal Splitting e.emp_id s.cust_id s.region 129 119 Eastern 129 119 Western 129 131 Eastern 195 109 Eastern 195 121 Central
Horizontal Splitting e.emp_id s.cust_id s.region 129 119 Eastern 129 119 Western 129 131 Eastern 195 109 Eastern 195 121 Central
e.emp_id s.cust_id s.region 129 119 Eastern 129 119 Western 129 131 Eastern 195 109 Eastern 195 121 Central Horizontal Splitting
FOR XML AUTO (continued) • Every segmented region generates an element • The element is named after the table that the columns came from
e.emp_id s.cust_id s.region 129 119 Eastern 129 119 Western 129 131 Eastern 195 109 Eastern 195 121 Central FOR XML AUTO (continued) 2 <e> elements 5 <s> elements
FOR XML AUTO (continued) • A region is a child of the region to its left
e.emp_id s.cust_id s.region 129 119 Eastern 129 119 Western 129 131 Eastern 195 109 Eastern 195 121 Central FOR XML AUTO (continued) <e> <s/> <s/> <s/> </e> <e> <s/> <s/> </e>
FOR XML AUTO (continued) • Every column is an attribute: <e emp_id=‘129’> <s cust_id=‘119’ region=‘Eastern’/> <s cust_id=‘119’ region=‘Western’/> <s cust_id=‘121’ region=‘Eastern’/> </e> <e emp_id=195> <s cust_id=‘109’ region=‘Eastern’/> <s cust_id=‘121’ region=‘Central’/> </e>
FOR XML AUTO (continued) • These work the same as RAW mode: • ELEMENTS mode • Name quoting • Content Quoting • Base64 encoding • Treatment of NULL values
2.3 SQLX • Five functions • xmlelement, xmlconcat, xmlforest, xmlgen, xmlagg • An XML type • create domain xml long varchar
XMLELEMENT() • Example select xmlelement( name x, ‘abc’ ) • Result <x>abc</x>
XMLELEMENT (continued) • Can insert attributes select xmlelement( name x, xmlattributes( ‘abc’ as y, ‘def’ as z ) ) • Result <x y=‘abc’ z=‘def’/>
XMLELEMENT (continued) • Can have any number of arguments: select xmlelement( name x, ‘ab’, ‘de’, ‘f’ ) • Result <x>abcdef</x>
XMLFOREST() • Example select xmlforest( ‘abc’ as x, ‘def’ as y ) • Result <x>abc</x> <y>def<y>
XMLFOREST (continued) • Example select xmlforest( emp_id as id, emp_lname as name, state ) from employee • Result <id>102</id> <name>Whitney</name> <state>MA</state> <id>105</id> <name>Cobb</name> <state>MA</state> …
XMLCONCAT() • Example select xmlconcat( xmlelement( name x, ‘abc’ ), xmlelement( name y, ‘def’ ) ) • Result <x>abc</x> <y>def</y>