What Is The DOM
What Is The DOM
What Is The DOM
The DOM defines a standard for accessing documents like XML and HTML:
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access
and update the content, structure, and style of a document."
The DOM defines the objects and properties of all document elements, and the methods (interface) to access them.
The HTML DOM defines the objects and properties of all HTML elements, and the methods (interface) to access them.
If you want to study the HTML DOM, find the HTML DOM tutorial on our Home page.
A W3C standard
The XML DOM defines the objects and properties of all XML elements, and the methods (interface) to access them.
In other words: The XML DOM is a standard for how to get, change, add, or delete XML elements.
DOM Nodes
DOM Example
The root node in the XML above is named <bookstore>. All other nodes in the document are contained within <bookstore>.
The first <book> node holds four nodes: <title>, <author>, <year>, and <price>, which contains one text node each, "Everyday Italian",
"Giada De Laurentiis", "2005", and "30.00".
In this example: <year>2005</year>, the element node <year>, holds a text node with the value "2005".
XSLT Example
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
HTML uses predefined tags, and the meaning of each tag is well understood.
The <table> tag in HTML defines a table - and a browser knows how to display it.
Adding styles to HTML elements are simple. Telling a browser to display an element in a special font or color, is easy with CSS.
XML does not use predefined tags (we can use any tag-names we like), and therefore the meaning of each tag is not well understood.
A <table> tag could mean an HTML table, a piece of furniture, or something else - and a browser does not know how to display it.
XSLT Introduction
« Previous Next Chapter »
XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.
Before you continue you should have a basic understanding of the following:
HTML / XHTML
XML / XML Namespaces
XPath
If you want to study these subjects first, find the tutorials on our Home page.
What is XSLT?
XSLT stands for XSL Transformations
XSLT is the most important part of XSL
XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like
HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.
With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests
and make decisions about which elements to hide and display, and a lot more.
A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree.
The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.
Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used!
The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
or:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.
The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must
also include the attribute version="1.0".
Start with a Raw XML Document
Viewing XML Files in Firefox and Internet Explorer: Open the XML file (typically by clicking on a link) - The XML document will be
displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or
collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the
browser menu.
Viewing XML Files in Netscape 6: Open the XML file, then right-click in XML file and select "View Page Source". The XML document will
then be displayed with color-coded root and child elements.
Viewing XML Files in Opera 7: Open the XML file, then right-click in XML file and select "Frame" / "View Source". The XML document will be
displayed as plain text.
View "cdcatalog.xml"
Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
View "cdcatalog.xsl"
cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
.
.
</catalog>
If you have an XSLT compliant browser it will nicely transform your XML into XHTML.
My CD Collection
Title Artist
XPointer allows the hyperlinks to point to more specific parts (fragments) in the XML document.
Before you continue you should have a basic understanding of the following:
HTML / XHTML
XML / XML Namespaces
XPath
If you want to study these subjects first, find the tutorials on our Home page.
What is XLink?
XLink is short for XML Linking Language
XLink supports simple links (like HTML) and extended links (for linking multiple resources
together)
With XLink, the links can be defined outside the linked files
What is XPointer?
The XML Linking Language (XLink) became a W3C Recommendation 27. June 2001.
The XML Pointer Language (XPointer) became a W3C Recommendation 25. March 2003.
You can read more about XML standards in our W3C tutorial.
There is some XLink support in Mozilla 0.98+ and Internet Explorer 6.0+. Earlier versions of these browsers have no XLinks support at all!
XLink Syntax
In HTML, we know (and all the browsers know!) that the <a> element defines a hyperlink. However, this is not how it works with XML. In
XML documents, you can use whatever element names you want - therefore it is impossible for browsers to predict what hyperlink elements
will be called in XML documents.
The solution for creating links in XML documents was to put a marker on elements that should act as hyperlinks.
Below is a simple example of how to use XLink to create links in an XML document:
<?xml version="1.0"?>
<homepages xmlns:xlink="http://www.w3.org/1999/xlink">
<homepage xlink:type="simple"
xlink:href="http://www.w3schools.com">Visit W3Schools</homepage>
<homepage xlink:type="simple"
xlink:href="http://www.w3.org">Visit W3C</homepage>
</homepages>
To get access to the XLink attributes and features we must declare the XLink namespace at the top of the document.
The xlink:type and the xlink:href attributes in the <homepage> elements define that the type and href attributes come from the xlink
namespace.
The xlink:type="simple" creates a simple, two-ended link (means "click from here to go there"). We will look at multi-ended (multidirectional)
links later.
XPointer Syntax
In HTML, we can create a hyperlink that either points to an HTML page or to a bookmark inside an HTML page (using #).
Sometimes it is more useful to point to more specific content. For example, let's say that we want to link to the third item in a particular list,
or to the second sentence of the fifth paragraph. This is easy with XPointer.
If the hyperlink points to an XML document, we can add an XPointer part after the URL in the xlink:href attribute, to navigate (with an XPath
expression) to a specific place in the document.
For example, in the example below we use XPointer to point to the fifth item in a list with a unique id of "rock":
href="http://www.example.com/cdlist.xml#id('rock').child(5,item)"
Look at the following XML document, "bookstore.xml", that represents a few books:
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
</bookstore>