XML - Managing Data Exchange/C.3

More Features of an XML Stylesheet

edit

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.

<?xml version="1.0" encoding="UTF-8"?>
<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</TITLE> <--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;} </STYLE> </HEAD> <BODY> <H2>Cities and Hotels</H2> <--The template defined is called to display in a HTML page by the xsl:apply-templates element--> <xsl:apply-templates select="tourGuide"/> </BODY> </HTML> </xsl:template> <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:text> <xsl:value-of select="continent"/> <BR/> <xsl:text>Country: </xsl:text> <xsl:value-of select="country"/> <BR/> <xsl:text>Administration Unit: </xsl:text> <xsl:value-of select="adminUnit"/> <BR/> <xsl:text>City: </xsl:text> <xsl:value-of select="cityName"/> <BR/> <BR/> </TD> </TR> <TR CLASS="yellowBackground"> <TD/> <TD> <xsl:text>Hotel Name</xsl:text> </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 STYLEFONT-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: <!--This is a Named Custom SimpleType that is called from Hotel whenever someone types in an email address-->


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


The output can be formatted using HTML tables for layout with an XML stylesheet.

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

edit

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:

. (the period) for any character at all
\d for any digit
\D for any non-digit
\w for any word (alphanumeric) character
\W for any non-word character (i.e. -, +, =)
\s for any white space (including space, tab, newline, and return)
\S for any character that is not white space
x* to have zero or more x's
(xy)* to have zero or more xy's
x+ repetition of the x, at least once
x? to have one or zero x's
(xy)? To have one or no xy's
[abc] to include one of a group of values
[0-9] to include the range of values from 0 to 9
x{5} to have exactly 5 x's (in a row)
x{5,} to have at least 5 x's (in a row)
x{5,8} at least 5 but at most 8 x's (in a row)
(xyz){2} to have exactly 2 xyz's (in a row)

For example, the pattern for validating a Social Security Number is \d{3}-\d{2}-\d{4}

The prior schema code has a check for emailAddressType \w+\W*\w*@{1}\w+\W*\w+.\w+.*\w*
[w+]at least one word (alphanumeric) character,e. g. answer
[W*]followed by none, one or many non-word character(s),e. g. -
[w*@{1}]followed by any (or none) word character and one at-sign,e. g. my@
[w+]followed by at least one word character, e. g. mail
[W*]followed by none, one or many non-word character(s),e. g. _
[w+.]followed by at least one word character and period,e. g. please.
[w+.*]zero to infinite times followed by the previous string,e. g. opentourism.
[w*]finally followed by none, one or many word character(s)e. g. org
email-address: answer-my@mail_please.opentourism.org


Common Errors in Netbeans

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