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?
An XML element is everything from (including) the element’s start tag to (including) the element’s end tag.
An element consists of an opening tag, its attributes, any content, and a closing tag.
An element can contain: 1. text 2. attributes 3. other elements 4. or a mix of 1 thru 3.
What are tags?
A tag – either opening or closing – is used to mark the start or end of an element.
What is a Node
A node is a part of the hierarchical structure that makes up an XML document. “Node” is a generic term that applies to any type of XML document object, including elements, attributes, comments, processing instructions, and plain text.
What is a Well Formed XML document?
- Contains a Single Root Element, that Root Element contains all other elements.
- All elements are properly nested.
- All elements are closed
- All Attributes are quoted.
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 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’)