XML Flashcards
What are the four modes of XML
- Auto
- Raw - Attribute Centric
- Path
- Explicit - Deprecated
what is the difference between XML Fragments and an XML Document?
An XML Document has a single root node.
XML Fragments have no root node.
What is well formed XML
This means every begin tag has a corresponding end tag and they are nested properly.
<product></product>
<product></product>
<product></product>
What is XSD?
XML Schema Description.
One of the most widely used forms for storing meta data descriptiions in xml.
XSD Documents are XML documents that describe the meta data of other XML documents.
XSD predefines element names, data types, number of occurences of an element, the contraints, and more.
A predefined XML document is referred to as a ‘typed’ XML document.
Describe attribute centric xml
One row per row of data.
Attributes have their own name and their values are enclosed in quotes.
Describe Element centric xml
One tag per element
Is XML case sensitive?
Yes.
it is case sensitive unicode text
List the special value characters.
Character = Replacement Text
& (ampersand) = &
” (quotation mark) = "
< (less than) = <
> (greater than) = >
’ (apostrophe) = '
Recite the XML CDATA
…}}>
Replace the … with any text that is not a string literal.
Do elements need to be uniquely named?
No.
To seperate similarly named elements use namespaces.
Recite FOR XML RAW
SELECT
FROM dbo.Product
FOR XML RAW; –> Attribute Centric
SELECT
FROM dbo.Product
WHERE
ORDER BY
FOR XML RAW (‘ProductID’), ELEMENTS, ROOT(‘Product’);
Changes:
- The row element name to ‘ProductID’,
- The FOR XML RAW default from Attribute Centric into Element Centric
- Adds a root node and namespace (‘Product’)
Recite FOR XML AUTO
WITH NAMESPACES(‘ER70761-CustomersOrders’ AS co)
SELECT
[co:Customer].custid AS [co:custid],
[co:Customer].companyname AS [co:companyname],
[co:Order].orderid AS [co:orderid],
[co:Order].orderdate AS [co:orderdate]
FROM Sales.Customers AS [co:Customer]
JOIN Sales.Orders AS [co:Order]
ON [co:Customer].custid = [co:Order].custid
WHERE…
ORDER BY
[co:Customer].custid AS [co:custid],
[co:Order].orderid AS [co:orderid]
FOR XML AUTO, ELEMENTS, ROOT(‘CustomerOrders’);
The table and column aliases are used to create the element names. The element names are prefixed with the namespace name.
The results:
<customerorders></customerorders>
<customer></customer>
<custid>1</custid>
<companyname>Customer NRZBB</companyname>
<order></order>
<orderid>10692</orderid>
<orderdate>2018-10-23</orderdate>
<custid>2</custid>
<companyname>Customer NRZCC</companyname>
<order></order>
<orderid>10693</orderid>
<orderdate>2018-10-23</orderdate>
The Order By is important because it formats the returned XML; without it the order is not predictable.
Also the SELECT statement order is important as it is used to determine the order of nesting elements. The column order need to follow a one-to-many relationship. A customer can have many orders, therefore custid preceeds the orderid column.
What is converting XML into a table called?
What is used to convert XML into a table?
XML is converted into a relational table is called ‘shredding’ XML.
To shred XML use the OPENXML rowset function.
When shredding XML, what is the function used and what is process?
The function OpenXML is used to create a rowset over an in-memory XML document using the Document Object Model (DOM) presentation.
To process the DOM
- Declare an integer for the DOM handle
- Declare a NVarchar for the XML document and set it to the XML (copy…paste)
- Execute the sys.sp_xml_preparedocument @DocHandle OUTPUT, @XMLDocument;
- SELECT *
FROM OPENXML
(
@DocHandle,
‘/CustomerOrder/Customer’,
(1,2,8, or 11) )
- 1 = Attribute Centric
- 2 = Element Centric
- 8 is combined with either 1 or 2 to create
WITH
(
custid INT,
companyname NVarchar(40)
);
- Remove the DOM by…
EXECUTE sys.sp_xml_removedocument @DocHandle;
What is XQUERY?
XQUERY is standard language for browsing XML instances and returning XML output.
It is richer than XPATH (older).
XQUERY can loop over nodes, shape the returned XML instance, and more.