SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – What is XML? – Day 29 of 35

Answer simple quiz at the end of the blog post and –

Every day one winner from India will get Joes 2 Pros Volume 5.

Every day one winner from United States will get Joes 2 Pros Volume 5.

What is XML?

A common observation by people seeing an XML file for the first time is that it looks like just a bunch of data inside a text file. XML files are text-based documents, which makes them easy to read.  All of the data is literally spelled out in the document and relies on a just a few characters (<, >, =) to convey relationships and structure of the data.  XML files can be used by any commonly available text editor, like Notepad.

Much like a book’s Table of Contents, your first glance at well-formed XML will tell you the subject matter of the data and its general structure. Hints appearing within the data help you to quickly identify the main theme (similar to book’s subject), its headers (similar to chapter titles or sections of a book), data elements (similar to a book’s characters or chief topics), and so forth. We’ll learn to recognize and use the structural “hints,” which are XML’s markup components (e.g., XML tags, root elements).

Applications and websites scan XML and read these components to understand the organization and structure of the data. Additionally, XML can include instructions for how a program should consume its data.  To understand how this works, we first must discuss the concept of data and metadata.

Metadata vs. Metadata

In short, metadata is data about data. A timestamp, a row number, a column name, the name of table, a data type – these are examples of metadata, because they help characterize and describe what kind of data you have but aren’t actually part of the raw data itself.  Metadata describes your data, including relationships within your data.

When we were in grade school we learned how indentation is a simple formatting cue used to show structure, such as subtopics belonging to a topic. In the right hand side of the figure below the Data List B indentation serves as metadata because it helps us understand the relationships between the items in the list.  Later we will see the layout of an XML document includes fairly simple cues, like indentation and tags to help distinguish one hierarchical level from another.

Since the heading “Veggies” appears below the title “Food” and is indented once, we know Veggies is a category belonging to Food.  “Meat” and “Fruit” appear at the same level as Veggies, so they are a peer of Veggies and also a type of Food.  Below each type sits a further indented level, which indicates that these items (Carrots, Chicken, Banana, etc.) are types of Veggies, Meat, and Fruit and are sub-types of Food.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - What is XML? - Day 29 of 35 J2P_29_1

Much like the examples above, XML is comprised of two components:

1)     Data – The information you have.

2)     Metadata – the description of the data, including relationships and properties of the data.

Streams

In the IT world, the term stream signifies the output of information by a program or process. Don’t feel badly if that definition doesn’t immediately register with you. It took me some time to understand the “streams” concept. A colleague finally sat me down and explained it. Let me save you years of bewilderment on this funny geek-speak term.

Instead of water, these days I think “result set” when I hear the term stream. It’s even easier to think of a stream as a table or a view. Just a big grid of data. 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. SQL Server even produces spatial data first as a grid, although we humans prefer to view the result shaped as a sphere or map. However, XML does not set things up in rows and columns.

Tabular Stream

Let’s look at a simple SELECT statement from JProCo’s Location table and notice we get a table-like result set, also known as a tabular result set (see figure below). As noted above, the tabular stream is SQL Server’s preferred mode of streaming output from its data engine to your display.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - What is XML? - Day 29 of 35 J2P_29_2

XML Streams

Unlike SQL Server, XML actually has many modes of streaming output. You can instruct SQL Server to stream your XML result using the mode you prefer.  Let’s look again at the data we queried from the Location table. This time we will have the output appear as an XML stream, rather than a tabular result.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - What is XML? - Day 29 of 35 J2P_29_3

XML Raw Mode

To our base query, we need to add the keywords “FOR XML” in order for our results to appear as XML. We also need to specify the mode, which in the figure below the raw mode.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - What is XML? - Day 29 of 35 J2P_29_4

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - What is XML? - Day 29 of 35 J2P_29_5

We would say this result set now appears in an XML raw data format. Notice the result is a hyperlink, which you click to view your results. Also notice that the default tag label is “row” for each row of our result set and the data appear as attributes.

Note: If you want to setup the sample JProCo database on your system you can watch this video. For this post you will want to run the SQLInteropChapter1.0Setup.sql script from Volume 5.

Question 29

If you don’t specify any option, then XML RAW will have your data streamed in…

  1. Element text.
  2. Attributes.

Rules:

Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Every day one winner from India will get Joes 2 Pros Volume 5.
Every day one winner from United States will get Joes 2 Pros Volume 5.
The contest is open till next blog post shows up at which is next day GTM+2.5.

Reference:  Pinal Dave (https://blog.sqlauthority.com)

Joes 2 Pros, SQL Scripts
Previous Post
SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Structured Error Handling – Day 28 of 35
Next Post
SQL SERVER – SSQL Architecture Basics – Core Architecture Concepts – Book Available for SQL Server Certification

Related Posts

78 Comments. Leave new

  • dilipkumarjena
    August 30, 2011 12:10 am

    the correct answer is 2) Attributes.

    As perfectly explained by you.

    Dilip Kumar Jena
    country: India

    Reply
  • Answer is :

    2) Attributes.

    If we do not specify any option, then XML RAW will have your data streamed in “Attributes”

    Kedar
    India.

    Reply
  • Damodaran Venkatesan
    August 30, 2011 2:14 am

    The answer is 2 Attributes.

    Raw translates to Attributes of data.

    Country: USA.

    Reply
  • Uday Bhoopalam
    August 30, 2011 8:54 am

    corre`ct answer is option 2 Attributes.

    Uday
    USA

    Reply
  • Raushan Kumar Jha
    August 30, 2011 10:42 am

    Correct Ans : 2

    2. Attributes

    Some more points to keep in mind:

    a. If we’re using “FOR XML RAW”
    e.g.
    Query : select top(1) CustomerID, CustomerName from TestCustomers
    FOR XML RAW
    Output :

    b. If we want come customized name instead of “row” in the result set specify the name.
    e.g.
    Query : select top(1) CustomerID, CustomerName from TestCustomers
    FOR XML RAW (‘CustomerInfo’)
    Output :

    c. If we want result in element text instead of attribute try this (use PATH instead of RAW)
    e.g.
    Query : select top(1) CustomerID, CustomerName from TestCustomers
    FOR XML Path (‘CustomerInfo’)

    Output :
    0000001
    Shri Ram

    Reply
  • Option 2 (Attributes) is correct as XML RAW display data in Attributes format.

    Somnath Desai

    India

    Reply
  • correct answer is option 2 which is “Attributes”

    vaishali
    Hyderabad, Andhra Pradesh, India

    Reply
  • Kevin Scharnhorst
    August 30, 2011 9:08 pm

    2.Attributes.

    USA

    Kevin Scharnhorst

    Reply
  • Option 2 is correct.

    2. Attributes.

    -JD Varu
    Ahmedabad-INDIA

    Reply
  • Answer 2

    2.Attributes.

    USA

    Reply
  • what is the difference between xml raw and xml path?

    Reply
  • Answer: Attribute

    The default tag label is “row” for each row of our result set and the data appear as attributes.

    Aditi
    USA

    Reply
  • The correct answer is #2 (attributes)

    Eric
    (USA)

    Reply
  • Sreejith Vijayan
    September 2, 2011 12:50 pm

    If you don’t specify any option, then XML RAW will have your data streamed in… Attributes.

    Sreejith Vijayan
    Kerala
    India

    Reply
  • Correct Answer is #2. Attributes

    Thanks
    Raghavendra
    India

    Reply
  • Thanks for Sharing , it will really helpful for me to understand meta data clearly

    Reply

Leave a Reply