HandsOn Examining the Contents of an XSL File

1. Use Internet Explorer to open the Shippers.xsl file located in the

Learn_XML folder. Access displays the contents of the Shippers.xsl file as shown in Figure 29-6.

Note: To find out more about XML schemas, check out the following links: http://www.w3.org/TR/2004/REC-xmlschema-0-20041028/ http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/ http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/

Part V

C:\Learn_XMLVShippers.xsl Microsoft Internet Explorer

File Edit View Favorites Tools Help

Addiess C;\Leam_XML\5hippers.xsl

<7:-:mt version™" 1.0" '■> - < n 51; styl e 5 heet vera o n 1,0" xm Ins: xsl- '"http: / / mvw. vv3. orq/19 9 9/X S L/Trarisfo rm'' xrn Ins: msxsl=" urn :sche mas-micros oft-com:xslt" ">:' -fx I jncticII' exclude-result:-preflxes-'msKSI fx

<xsl; output method="htmr verslon="4.0" indent="yes" xmln 5 :*sl="http://www.yi3.org/1999/XSL/Transform?! /> - <xsl:ternplate rnatch="//(iatsroor"

Nmlns'.wsM'http://www.w3.org/19Q9/XiiL/Transtorm">

imetahttp-EQUIV-"ContentTypi?"content-"text/litinl;char5et=UTr8"/> ■ -I' -, xhi[>:>>-[; - r ■ >

<body I ink-"#000 Off" vlink-M#B000a0">

- 'table border"' 1" bgcolor—"#ffffff cellspadng-"0" cellp5dding-"0" ¡d="CTRLl">

<co style='Texr-nUGM: right; width: 0.B02ln'> !> ccol styla="WIDTH: 1.1562ln"/i ■ccal style-"WIDTH: lin" /> </talgroup>

- cdiv align-"center">

<strono>compariY Name</stronQ> </dly> «Ad> - -rtd?

Mill

¿J Dona j My Computer

Figure 29-6: The XSL stylesheet document is just another XML document that contains HTML formatting instructions and XSLT formatting elements for transforming raw XML data into HTML.

When you take a look at the contents of the Shippers.xsl file you will notice a number of XSLT formatting elements such as <xsl:template>, <xsl:for-each>, and <xsl:value of>. You will also find in it many HTML formatting instructions such as <head>, <title>, <body>, <table>, <colgroup>, <col>, <tbody>, <tr>, <td>, <div>, and <strong>.

The first line of the stylesheet code declares that this is an XML document that follows the XML 1.0 standard (version). An XSL document is a type of XML document. While XML documents store data, XSL documents specify how the data should be displayed.

The second line declares the namespace to be used to identify the tags in the XSL document. (See the "Understanding the XML Data File" section earlier in this chapter for more information about namespaces). The third line specifies that HTML should be used to display the data.

The next line is the beginning of the formatting section. Before we look at the XSLT tags, you need to know that XSL documents use templates to perform transformations of XML documents. The XSL stylesheet can contain one or more XSLT templates. You can think of templates as special blocks of code

Taking Your VBA Programming Skills to the Web that apply to one or more XML tags. Templates contain rules for displaying a particular branch of elements in the XML document. The use of templates is made possible via special formatting tags.

Notice that the Shippers.xsl file contains the <xsl:template> tag to define a template for the entire document. The <xsl:template> element has a match attribute. The value of the match attribute indicates the nodes (elements) for which this template is appropriate.

For example, the special pattern "//" in the match attribute tells the XSL processor that this is the template for the document root:

<xsl:template match="//dataroot" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

The template ends with the </template> closing tag.

Following the definition of the template, standard HTML tags are used to format the document. Next, the XSLT formatting instruction <xsl:for-each> tells the XSL processor to do something every time it finds a pattern. The pattern follows the select attribute. For example,

<xsl:for-each select="Shippers">

tells the XML processor to loop through the <Shippers> elements. The loop is closed with a closing loop tag:

The XSLT formatting instruction <xsl:value-of> tells the XSL processor to retrieve the value of the tag specified in the select attribute. For example,

<xsl:value-of select="ShipperID">

tells the XML processor to select the ShipperlD column. Because this formatting instruction is located below the <xsl:for-each> tag, the XSL processor will retrieve the value of the ShipperlD column for each Shippers element.

The select attribute uses the XML Path language (XPath) expression to locate the child elements to be processed.

0 0

Post a comment