XML - Managing Data Exchange/C.3

=More Features of an XML Stylesheet=

In the last chapter, a stylesheet was created to simply output text from the XML document to a HTML page. The presentation of a document can be formatted using table to layout the content for display. In addition, a different background color, font size, font weight, and alignment can be defined using the style tag in HTML to design the output page. Table 3-3 shows the features mentioned. Note that HTML commands are uppercase.

 

< xsl : stylesheet version ="1.0" xmlns : xsl ="http : //www.w3.org/1999/XSL/Transform" >

< xsl : output method ="html"/ > < xsl : template match ="/" > < HTML > < HEAD > < TITLE >Tour Guide  <--The style tag can be used to define different colors, font size, font family, text alignment, margins, and many other style-formatting elements just like a HTML page.--> < STYLE TYPE ="text/css"> H2 {TEXT-ALIGN : CENTER;} .greenBackground {BGCOLOR=“LIGHTGREEN; TEXT-ALIGN : CENTER;} .yellowBackground {BGCOLOR=“YELLOW; TEXT-ALIGN : CENTER; FONT-WEIGHT : BOLD ; FONT-SIZE : 14pt;} .salmonBackground {BGCOLOR=“LIGHTSALMON; TEXT-ALIGN : CENTER; FONT-SIZE : 12pt;}   < BODY > < H2 > Cities and Hotels  <--The template defined is called to display in a HTML page by the xsl:apply-templates element--> < xsl : apply-templates select ="tourGuide"/ >    < xsl : template match ="tourGuide" > < TABLE BORDER ="1" WIDTH ="100%" > <--There is a xsl:for-each element, hotel, nested inside the xsl:for-each city element. (Display the appropriate contents for the       		hotel elements for every hotel inside every city).--> < xsl : for-each select ="city" > < TR > < TD COLSPAN ="6" CLASS ="greenBackground" > < BR / > < xsl : text > Continent :  < xsl : value-of select ="continent"/ > < BR / > < xsl : text > Country :  < xsl : value-of select ="country"/ > < BR / > < xsl : text > Administration Unit :  < xsl : value-of select ="adminUnit"/ > < BR / > < xsl : text > City :  < xsl : value-of select ="cityName"/ > < BR / > < BR / >   < TR CLASS ="yellowBackground" > < TD / > < TD > < xsl : text > Hotel Name  </ TD > < TD > < xsl : text > Street Address </ xsl : text > </ TD > < TD > < xsl : text > Telephone Number </ xsl : text > </ TD > < TD > < xsl : text > Email Address </ xsl : text > </ TD > < TD > < xsl : text > Hotel Rating </ xsl : text > </ TD > </ TR > < xsl : for-each select ="hotel" > < TR CLASS ="salmonBackground" > < TD STYLE FONT-SIZE:8pt" >                            <--An image tag is used to display an image-->                             <--Just like the src, the attributes width, height, and alt                        		can be called to format the image.-->                             < IMG >                                 < xsl : attribute name ="SRC" >                                     <--The file name and location is defined as an attribute (filename) of the empty tag, hotelPicture, in the XML schema.                                		To indicate that filename is the data source, the src attribute of the image tag must be called-->                                     < xsl : value-of select ="hotelPicture/@filename"/ >                                 </ xsl : attribute >                                 < xsl : attribute name ="WIDTH" >                                     < xsl : value-of select ="hotelPicture/@size"/ > </ xsl : attribute > < xsl : attribute name ="HEIGHT" > < xsl : value-of select ="hotelPicture/@size"/ > </ xsl : attribute > < xsl : attribute name ="ALT" > < xsl : value-of select ="hotelPicture/@value"/ > </ xsl : attribute > </ IMG > < BR / > < xsl : value-of select ="hotelPicture/@imageURL"/ > </ TD > < TD > < xsl : value-of select ="hotelName"/ > </ TD > < TD > < xsl : value-of select ="streetAddress"/ > </ TD > < TD > < xsl : value-of select ="telephoneNumber"/ > </ TD > < TD > < xsl : value-of select ="emailAddress"/ > </ TD > < TD > < xsl : value-of select ="hotelRating"/ > </ TD > </ TR > </ xsl : for-each > < TR CLASS ="salmonBackground" > < TD COLSPAN ="6" TEXT-ALIGN : RIGHT" >                        < H3 >                             < xsl : text > Number of hotel(s) found : </ xsl : text >                             <--<xsl:value-of select="count(hotel)"/>, count is a                            built-in function that counts the number of node indicated by the parameter, in this case the number of hotels.                            This means that the total number of hotels for each city is reported.-->                             < xsl : value-of select ="count(hotel)"/ >                         </ H3 >                     </ TD >                 </ TR >             </ xsl : for-each >         </ TABLE >     </ xsl : template >  </ xsl : stylesheet >

Note: Comment Tags are not formatted properly. In order to get the to show up on the screen, slight alterations were needed. You will need to add the ! to make sure that you can validate your stylesheet. Example:

Table 3-3: XML Stylesheet for a one-to-many relationship

Refers to Chapter 2 - A single entity for steps in using NetBeans to create the above XML stylesheet.

Output result of the above stylesheet (city_hotel.xsl) using city_hotel.xml that validate with city_hotel.xsd: link to city_hotel.html

Regex
Special regular expression (regex) language can be used to construct a pattern. The regex language in XML Schema is based on Perl’s regular expression language. The following are some common notations:

Common Errors in Netbeans

 * There are errors listed in the last chapter that will be useful for errors in this chapter as well.