Hello World


Wednesday, December 19, 2007

XSLT - Simple HTML Table

In this example we will convert a simple XML file to HTML























The HTML output
The goal is to generate the HTML output shown below using the XML document as the source of the information.



















XSLT will help us to do this.
Add this line to your XML file.

Here's the people.xsl (XSL) file that produces the HTML.
Every XSL file needs to specify the XSL namespace so that the parser knows which version of XSLT to use.























Here's a break down of the XSL file.
The namespace prefix xsl: is used in the rest of the XSL file to identify XSL processing statements. If a statement isn't prefixed with xsl:, then it's simply copied to the output without being processed. This is the way to add HTML statements to the output. When we are working with XSLT, the context for a query is the node in the source XML document currently being processed. So in the template xsl:template match="/", we are in the context of the root of the XML document. This isn't the PEOPLE element, it's the whole document, above the PEOPLE element.
When you're debugging XSL, the first question you should ask is, "What context is being processed?".

The backslashes are not part of the syntax.
<\xsl:template match="/"\>

Before processing can begin, the part of the XML document with the information to be copied to the output must be selected with a XPath expression. The selected section of the document is called a node and is normally selected with the match operator. If the entire document is to be selected, match the root node using match="/". Another approach is to match the document element (the element that includes the entire document). In our example, the document element can be selected using match="PEOPLE". (If you use this alternative approach, don't include PEOPLE in the 'for-each' selection below.)

The backslashes are not part of the syntax.
<\xsl:for-each select="PEOPLE/PERSON"\>


The expression xsl:for-each finds all 'PERSON' elements in the 'PEOPLE' element context using the XPath expression 'PEOPLE/PERSON'. If the selected node contains all elements in the root, all of the 'PEOPLE' elements will be selected. Since we want to include all 'PERSON' elements in our output document, we have used this expression.
The 'for-each' expression is a loop that processes the same instructions for these elements.


The backslashes are not part of the syntax.
<\xsl:value-of select="NAME"/\>

<\xsl:value-of select="ADDRESS" /\>
<\xsl:value-of select="TEL" /\> etc...

When the xsl:for-each expression has selected a 'PERSON' element, the xsl:value-of expression extracts and copies to the output file the value stored in the selected element. In this case, the value stored in the 'NAME' element is copied to the output. This call is made for each value you are extracting.


This will produce the html required that can then be displayed in the clients browser.

No comments:

4GuysFromRolla.com Headlines