Skip to content Skip to navigation

OpenStax_CNX

You are here: Home » Content » XML Basics

Navigation

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

XML Basics

Module by: Sarah Coppin. E-mail the author

Summary: This module describes XML (eXtensible Markup Language) and the rules that govern its usage. It also explains what a well-formed and valid document is.

Note: You are viewing an old version of this document. The latest version is available here.

What is XML?

The eXtensible Markup Language (XML) is a meta-markup language defined by the World Wide Web Consortium (W3C). It is not strictly a markup language itself, but rather a set of rules for creating markup languages. For our purposes a markup language is any language (HTML, for example) that uses tags surrounding text to convey information such as content or format. CNXML, the markup language used by the Connexions Project is an example of a language written in XML. There are many other examples at the W3C site. Here is an example of some markup in CNXML.


      <para>
	This is a paragraph in <term>CNXML</term>.  Notice that the
	markup contains tags that express the meaning of the text.
      </para>
      

<para> and </para> are the tags that the enclose the text. In XML, tags are always marked by angle brackets (also known as < and >). Tags generally come in pairs. An opening tag will look like <tagname>. A closing tag will look like </tagname>, with a / preceding the tag name.

XML allows the separation of presentation from content. HTML has tags such as <u> and <i>, which underline and italicize text respectively. There is no content information expressed, only formatting. XML allows you to define your own language of tags to represent content. You could create a tag called <book> to represent book titles, and create a stylesheet (a separate formatting document), that says that every <book> tag should be italicized or underlined. Then when you want to change the presentation of that type of content, you just change one small part of the stylesheet. Also, if you make tags that convey the content of the document, you can enable better searching. For example, you might look for the author of a document by looking at the md:author tag.

Well-formed XML

XML has a few rules that apply to all of its languages, including CNXML. If a document satisfies these rules, then it is well-formed. XML documents are required to be well-formed.

  • Every tag that is opened must be closed. An opening tag looks like <module> and a closing tag looks like </module>. There is a shortcut. If your tag contains no other tags (referred to as an empty tag), then you can can type a / before the end of the opening tag and delete the closing tag. For example, <media> </media> can be abbreviated <media/>.
  • Tags must be nested within each other. So, <b>red <i>and</i> blue</b> is fine, but <b>red <i>and</b> blue</i>is incorrect because the <b> and <i> tags have overlapping content.
  • You must put either single or double quotes around an attribute value. An attribute is some sort of information that is associated with a tag and is listed inside of the tag itself. For example, <module id="m0001"> and <module id='m0001'> are fine, but <module id=m0001> is incorrect.
  • You can also choose to start every document with an XML declaration. If you do use the XML declaration, then it has to be the very first thing in the file. It cannot even be preceded by whitespace. It is not considered to be a tag. The XML declaration is as follows. <?xml version="1.0"?> You can also include other information such as the encoding of the document or whether the document depends on other files or not.
  • There must be one tag that contains all of the other tags. For example in xhtml <html> and </html> must surround all of the other tags. There are some things that are included at the top of the document that are not tags and that are not included with the tags. The XML declaration is an example of this.

Valid XML

It is possible to define a set of rules that apply to all of the tags in a particular XML language. These rules can be defined in a couple of different ways. The most common way is to use a DTD (Document Type Definition). Any document which follows all of the rules for that language is called valid. A document is not required to be valid in order to be XML. However, it is generally a good idea.

Entities

An entity is a way to escape characters. Entities always have the sa me basic form. An entity will start with an ampersand and end with a semicolon. For example, &amp; will escape the & character. So if you type &amp; in the source file, you will see & when you view it in a browser. There are several entities that are predefined and will work in any XML file. They are &amp (&), &lt (<), &gt (>), &quot ("), and &apos ('). These are the same entities that are defined for HTML. In addition to these basic symbols, you can define your own entities using a DTD. These self defined entities can point to other symbols, or they can be short cuts to a larger piece of text.

You can also include unicode characters in XML. Unicode is a project to define a unique code for every character in any human language. Unicode is very useful any time that you need to use a symbol that is not a part of ASCII.

Unicode entities have an extra # sign. So they look like: &#134;. Or if the code is in hexadecimal, there is also an extra x causing them to look like: &#x03C0;.

Resources

The World Wide Web Consortium(W3C) is responsible for XML.

See www.unicode.org for more information on Unicode.

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks