Follow the rest of this series at the XQuery for the Non-Expert – X-Query Resources introduction post.
Myself, I am not an XQuery expert. These posts are summarizing what I know and understand. Some of this knowledge and understanding is changing on a regular basis. Please correct me if something incorrect is posted here.
To prepare for these new posts, we need a starting point. The starting point is going to be a vocabulary lesson. This way as I discuss different functions and aspects of XQuery we know what things mean.
Element tags are used to contain a domain of information within an XML document. In many cases the domains of information that are represented in elements and element tags can be thought of as tables or nouns.
Element tags have opening and closing structures that define the boundary for the element. These are identified with greater than (>) and less than (<) characters.
Let’s build out an example with element tags that defines a building. To define the building, let’s see this represented in XML.
This is the most common method for defining an element. There are separate opening and closing element tags; which allows information to be added to an element. For instance if the XML document from above was suppose to represent the Sears Tower we could put this information within the element tags.
Often times there will be elements in XML document that relate to other elements. To express these relationships, elements can be nested within other elements. This allows the XML document to build a hierarchy of information that is related to elements with the document.
To demonstrate this, let’s use the above XML example with an alternative method for defining element tag. If there will not be any information included in an element, the opening and closing tags can be combined into a single element tag. This alternative method is shown below with the addition of the floor element.
More data than can be expressed through elements will nearly always be needed. To accommodate this need, elements can have XML Attributes. Continuing on the table and noun analogy above, attributes would be columns or adjectives.
To continue the example above, suppose the building has multiple floors, each of these floors would likely have a level designation. Also, the Sears Tower is a skyscraper, this information would be valuable to represent in the XML document.
The following document adds elements for level and type to the element tags:
One important thing about elements is that when attributes are defined the values must be enclosed within double quotation marks.
I’ve talked about XML documents without a definition a few times now. So let’s discuss XML documents and give them a whole paragraph or three.
An XML document is a collection of element tags that provide information on a domain of information. When the XML document is complete with proper closing and ending tags and is nested properly it is considered well-formed XML document.
While an XML document might be well-formed, this does not guarantee the validity of the elements in the XML document. The XML document may have an XML schema that it is supposed to adhere to. But the XML document may contain or lack elements that would make it valid against the schema.
All of the building XML information above would be considered an XML document.
An XML schema is the collection of elements and attributes that are expected and allowed within an XML document. Beyond the elements and attributes, the schema can also define the data types and values that are allowed for each of the elements and attributes.
This is the query language designed to be used to query XML data. It is able to read through XML documents and transform it from it’s native XML format into smaller pieces or to return the XML data as rows inside a result set.
When an XML document is broken into smaller pieces, this is often called “shredding XML”. The smaller pieces from the XML documents are called XML fragments. An example of an XML fragment would be taking the XML document below:
And removing one of the Floor elements from it, as such:
This is both an XML fragment from the XML document it came from and it’s own XML document.
Ok, there is nothing in XML that is a wrap-up. This concludes my, hopefully, simple explanation of some of the concepts and terms within XML. If anything needs further clarity, please comment below. I expect that as time goes on I will update and expand this post.