Chapter 7 Querying and Managing XML Data Flashcards
What is XQuery?
It’s short for XML Query Language. It’s the standard language used to query and manipulate XML data.
What are elements?
Elements are part of an XML document. They are made up of a begin tag and an end tag. Elements can be nested.
What are tags?
Tags are used to name parts of an XML document, e.g. . Tags cannot be interleaved.
What is an XML document?
TODO
What is a well-formed XML document?
An XML document is said to be well formed if every begin tag has a corresponding end tag and if tags are nested properly.
Are XML documents “ordered”?
Yes, but not by any specific element value; it means the position of elements matters.
Is XML case-sensitive?
Yes. XML is case-sensitive unicode text.
What is markup?
Characters that have special meaning such as ‘ which introduces a tag.
What do you do if you want to include markup in the values of your XML?
They must be escaped by using an ampersand (&), a special code, followed by a semicolon (;). For example: & => & " => " < => < > => > ' => '
What is XML CDATA?
CDATA is short for character data. This will prevent special characters in the string from being parsed as XML makup. It’s written as <![CDATA[…]]> The … can be replaced with any character string.
What is the prolog of an XML document?
The prolog occurs at the beginning of the document and denotes the XML version and encoding of the document, such as
What is an XML fragment and how is it different from an XML document?
An XML document has a single root node. A fragment does not have a root node.
What are attributes?
Attributes are name value pairs that are listed on elements. They have their own names and their values are enclosed in quotation marks.
What is an attribute centric presentation?
Where each “property” is written as an attribute on the elements.
What is an element centric presentation?
Where each “property” is written as a nested element of the original element.
Do element names need to be unique?
No, element names do not have to be unique because they can be referred to by their position.
What are namespaces?
Namespaces can be used to distinguish elements that share the same name. You declare namespaces that are used in the root element of an XML document. You can also add an alias for each namespace. The namespace alias is used to prefix element names, e.g.
What is a schema?
Describes the metadata of XML documents. Currently, the most widely used metadata description is with XML Schema Description (XSD) documents. XSD documents are XML documents that describe the metadata of other XML documents. It includes element names, data types, number of occurrences of an element, constraints, and more.
What is a typed XML document?
A document with a predefined schema. A document that complies with or has been validated against a schema.
How do you produce XML from relational data?
Use SELECT … FROM … FOR XML construct.
What does the FOR XML RAW option do?
In RAW mode, every row from returned rowsets converts to a single element named row and columns translate to the attributes of this element, e.g. This behavior can be enhanced to include a root element, namespaces, and making the XML element-centric, e.g. FOR XML RAW, ROOT(‘ProductModel’), ELEMENTS.
What does the FOR XML AUTO option do?
AUTO mode generates nested elements in the resulting XML using heuristics based on the way the SELECT statement is specified, e.g. JOINS, etc. You have minimal control over the shape of the XML generated.
In AUTO and RAW modes, what does the ELEMENTS KEYWORD do for you?
Produces element-centric XML, e.g. FOR XML AUTO, ELEMENTS
What does the WITH NAMESPACES clause do?
The WITH NAMESPACES clause precedes the SELECT part of the query and defines the namespaces and aliases in the returned XML, e.g. WITH XMLNAMESPACES(‘TK461-CustomersOrders’ AS co)… SELECT
How are the table and column aliases in the query used to produce element names?
You can alias column and table names such as SELECT [co:Customer].custid as [co:custid]
FROM Sales.Customers AS [co:Customer]
using a colon to separate the namespace from the element and this will in turn produce the element names.
Why is ORDER BY important when using T-SQL SELECT to format and return XML?
Without the ORDER BY clause, the order of rows returned is unpredictable and you can get a weird XML document with an element repeated multiple times with just part of the nested elements every time.
Where does the FOR XML clause go in relation to the ORDER BY clause?
The FOR XML clause comes after the ORDER BY clause in a query.
Why is the order of columns important when using T-SQL SELECT to format and return XML?
SQL Server uses column order to determine the nesting of elements. The order of the columns should follow one-to-many relationships. A customer can have many orders; therefore, you should have the customer columns before the order columns in your query.
What does the XMLSCHEMA option do?
FOR XML in both RAW and AUTO modes can also return the XSD schema of the document you are creating. This schema is included inside the XML that is returned before the actual data - i.e. it is called inline schema. The XMLSCHEMA directive accepts a parameter where you define a target namespace, e.g. FOR XML AUTO, ELEMENTS, XMLSCHEMA(‘TK461-CustomersOrders’);
How can you use the XMLSCHEMA option to only export the schema and not include any data?
Simply include a WHERE condition in your query with a predicate that no row can satisfy, e.g. WHERE 1 = 2 FOR XML AUTO, ELEMENTS, XMLSCHEMA(‘TK461-CustomersOrders’);
What does the FOR XML EXPLICIT option do?
The EXPLICIT mode allows you to manually define the XML returned - you have total control. It is included for backwards compatibility only; it uses proprietary T-SQL syntax for formatting XML.
What does the FOR XML PATH option do?
The PATH mode allows you to manually define the XML returned - you have total control. It uses standard XML XPath expression to define the elements and attributes of the XML you are creating. By default, every column becomes an element; if you want to generate attribute-centric XML, prefix the alias name with the at (@) character.
SELECT Customer.custid AS [@custid], Customer.companyname as [companyname] FROM Sales.Customers AS Customer ORDER BY Customer.custid FOR XML PATH ('Customer'), ROOT('Customers')