XML Concepts Flashcards
What does XML stand for?
XML stands for Extensible Markup Language.
XML is known as self-describing data because you get the data plus metadata.
XML capabilities allow it to serve much like a self-describing relational database stored as a file. All data-aware applications can interact with XML.
What are the two components of XML?
XML is comprised of two components.
- Data
- Metadata - The description of the data, including relationships and properties of the data.
What are XML tags?
XML utilizes tags < > to create the data ‘outline’. The tags can also be nested.
The tag has two sections.
- Beginning tags
The example of Beginning tag would be , It sits between brackets and contains no slash.
Beginning tag would be < Name >
- Ending tags
The example of ending tag would be . It appears identical to beginning tag, except it contains a forward slash. Ending tags must follow the same sequence as the beginning tags.
Ending tag would be < / Name >
Please note: If a tag contains no element data, or element children, then the beginning and ending tag may be combined into one tag. for example < Name / >
is all that is required if there is no element in the Name tag.
What are XML elements?
Elements are actually the tags themselves. Element data is the data which sits between tags. For example, Seahawks is element data. It is data belonging to the element , Team.
< Team > Seahawks < / Team >
What are Root Elements and Top Level Elements in XML?
The top Tag in the XML is the Root Element or the Root Node. All data in the XML appears beneath the Root Element.
The elements falling just under the Root Element are the Top Level Element.
What are XML Attibutes?
XML element tags may contain information within the tags providing descriptive information about the element or its properties. These are known as Attributes.
Attribute data appears with the opening tag name and is enclosed in quotation marks.
For example
< Team Name=”Seahawks” >< / Team >
What is XML?
- Metadata is data about your data. Metadata is information which helps to describe the properties and relationships of data.
- XML is self-describing data, as it contains both data and metadata.
- XML holds its data adn metadata in a hierarchical set of tags.
- A beginning and ending tag always have the same name. You can tell them apart because the ending tag starts with a forward slash / .
- The ending tag for < Jerrin > would be < / Jerrin >
- The data in XML can be in the form of attributes or elements.
- Element data is stored between the beginning and ending element tag.
- Attribute data is stored inside of the beginning tag.
- XML is case sensitive.
- XML tag names cannot have spaces between them.
- Each element or attribute has exactly one parent (Except for the root element).
- Any node which is two or more levels above is called as Ancestor.
- Any node that is two or more levels below is called Descendant.
- An attribute is data that is inside an element.
What are streams and what are the different ways you can stream XML data from SQL Server?
In SQL Server terms , a Stream resembles a table. The grid like presentation of rows and columns that you get when you run a query. This is because SQL Server prefers to present data in a grid shape. However, XML does not set things up in rows and columns.
XML Streams
XML actually has many modes of streaming output. you can instruct SQL Server to stream your XML result in the mode you prefer.
Following are the various modes of streaming output in XML
- XML Raw Mode
- Output with ROOT Element option
- output with Elements option.
- XSINIL
What is XML Raw Mode?
To our base query, we need to add the keywords “FOR XML” in order for our results to apprear as XML. We also need to specify the mode, which in this case is RAW
The example query would be
SELECT LocationID, City
FROM Location
FOR XML RAW
Raw is the easiest type of XML to run. The output in the screenshot attached is the result in XML raw dataformat. Notice the result is a hyperlink, which you click to view your results. The second screenshot shows the XML after clicking the hyperlink.
Notice that the default tag label is “row” for each row of our result set. The data appear as attributes.
XML will allow you to override the default name < row > and set it to something mode descriptive.
Raw is the simplest mode where all data is considered to be at the same level with no nesting. The XML RAW command by default does not create a root.
How do you give a descriptive custom tag for the output in XML RAW mode?
XML will allow you to override the default name < row > and set it to soemthing more descriptive. The syntax shown here will change the name for each attribute tag to “RowLocation”
SELECT LocationID, City
From Location
FOR XML RAW(“rowLocation”)
You can see the output in the screenshot attached. Observ the red Intellisense underline and mouseover tag appearing in the result. XML document cannot contain multiple root level elements. Also notice we have four top-level nodes and no root node. SQL Server would prefer that we have a root element appreaing in our data.
How do you generate XML output with the Root Element Option?
XML is considered complete(known as well-formed XML) only if it has a root tag which emcompasses all other tags. Adding the keywork ROOT to our code specifies the ROOT element option.
Adding the ROOT command to our code will add root tag in our XML data.
Select LocationID, City
From Location
FOR XML RAW(‘Location’), ROOT
in the screenshot you can see that instead of just XML fragment, our code produces an entire XML stream. This output is a well-formed XML document.
By default the ROOT command would add a root element with the tag “root”. If you want to add a custom name for the Root element then you can do the following.
Select LocationID, City
From Location
FOR XML RAW(‘Location’), ROOT(‘Jerrin’)
In the output you can see that the Root element is now named “Jerrin”. Notice that the data appears as attributes inside the tag.
How do you generate an XML output with a custom name for the Root element?
By default the ROOT command would add a root element with the tag “root”. If you want to add a custom name for the Root element then you can do the following.
Select LocationID, City
From Location
FOR XML RAW(‘Location’), ROOT(‘Jerrin’)
In the output you can see that the Root element is now named “Jerrin”. Notice that the data appears as attributes inside the tag.
How do you generate XML output using the ELEMENTS option?
The raw mode likes to store all data as attributes. You can choose whether data appears as attributes or as elements. If you prefer elements, then you can append the ELEMENTS option to the XML RAW mode.
Select LocationID, City
From Location
FOR XML RAW(‘Location’), ROOT(‘Jerrin’),ELEMENTS
Looking closely at the XML output in the screenshot you’ll recognize the values for records 1,2,3 and 4 from the location table.
PLEASE NOTE: With the ELEMENTS option if the row has any NULL value then it is not shown as an Element in the XML output. This is a default behaviour. To include the NULL values in the XML output for the elements we would have to use the XSINIL option.
What is the XSINIL option?
The XSINIL option allows you to force a tag(s) to be present, even if the underlying data is null.
For fields in SQL Server which include a null value for some records but are populated with regular values in the other records, you will seem to have missing tags for the NULL record. Often this is alright, as missing tags are presumed to be null. However, if you require all tags to be present(even if they have no data), then you can specify the XSINIL option for your XML stream.
The output of the following query is shown in the screenshot.
select FirstName,LastName,LocationID
FROM Employee
FOR XML RAW(‘Employee’),ROOT(‘Emp’),ELEMENTS XSINIL
In the output we see that the third sub-element for John Marshbank. The location tag is no longer missing. It is present and show the value of xsi:nil=”true” in place of a LocationID
Your Root tag is named < ThisIsATag > in your well formed XML. How should the ending root tag be named?
< / ThisIsATag >
What do you call data inside of a tag like the example seen below?
< Team Name=’Empyreans” >
Attribute
Raw is the only XML Mode?
True or False
False
Raw automatically adds the root element tag in SQL Server.
True or False
False