> developer > dnu > courses > portal overview page 15
Novell Portal Services Overview and Gadget Development
April 2003
DeveloperNet University Course
Reader Rating    from ratings rate this article
View an eBook Version of this course - LARGE FILE! Send this page to a friend

XML/XSL Overview

The section of the course contains a short XML/XSL overview. If you are already familiar with XML and XSL, you may want to skip this section, otherwise, dive in!

Contents of the section include:

  • HTML

  • Cascading Stylesheets (CSS)

  • Extensible Markup Langusge (XML)

  • Document Type Definitions (DTD)

  • Extensible Stylesheet Language (XSL)

  • XSL Commands

HyperText Markup Language (HTML)

HTML is great for data presentation. It is simple to create, especially with editors. However, it lacks structure and is not suitable for data exchange. Probably one of the most negative things about HTML is that it is difficult to read HTML code.

Look at the following HTML example:


<html>
<head>
<title>Scott's Home Page</title>
</head>
<body>
<center><h1>Welcome to Scott's Website</h1></center>
<center><h2>This site <strong>will</strong>get better</h2></center>
<h2>I guarantee it!</h2>
<p>This is a sample Webpage</p>
</body>
</html>

When the above HTML code is executed, the following is displayed, Figure 22.

Sample HTML Page.

Figure 22: Sample HTML Page.

For more information on HTML, see: http://www.w3.org/MarkUp/

Cascading Stylesheets (CSS)

Cascading Stylesheets provide a common look and feel to web pages. They really only manage appearance of the page and not the data. However, they can be used by both HTML and XML documents. They are simple to use, but are not very powerful.

The following is a CSS example as applied to our sample Web page:


BODY{color: #000000;}
H1{ font-family: Arial;
color: #FF0000;
background-color: #C0C0C0;}
CENTER H2{font-family: Courier;}
H2{font-family: "Times New Roman";}
STRONG{color: #FF0000;
font-family: Arial;}
P{font-family: "Comic Sans MS";}

The result of adding CSS to our sample Web page is shown in Figure 23.

CSS encoded Web page.

Figure 23: CSS encoded Web page.

For everything you wanted to know about CSS, visit: http://www.w3.org/Style/CSS/

Extensible Markup Language (XML)

XML is all the rage today. Novell has capitalized on it in a big way with DirXML, and in several other products, including NPS. XML is a subset of Standard Generalized Markup Language, or SGML for short. XML defines data, but not appearance. It is easy to create documents in XML, since it provides data exchange capabilities. Also XML is widely supported.

The following is a short XML example:


<message>
<header>
<date>August 9, 2003</date>
<From>Me</From>
<To>You</To>
<CC>Them</CC>
<Sub>Test Message</Sub>
</header>
<Body>How is it going?</Body>
<Sig>There is no "I" in team</Sig>
</message>

For more information on XML, see: http://www.xml.com/ and http://www.w3.org/XML/

Document Type Definition (DTD)

The DTD defines XML document structure. It also defines the order of data in the document. Well formed XML documents will conform to the DTD. To make your life easier, there are tools available to help in verifying the conformation.

The following is a short DTD example:


<!ELEMENT message (Header, Body?, Sig?)>
<!ELEMENT Header (Date, From, To+, CC*, BC*, Sub?)>

Where:
"?" - 0 or 1
"*" - unlimited use
"+" - at least 1

For more information about DTDs, check out: http://www.w3schools.com/dtd/default.asp and http://www.xmlfiles.com/dtd/.

Extensible Stylesheet Language (XSL)

XSL is used to handle the appearance of XML documents. It also controls the data in the document by doing such things as filtering and sorting data. However, XSL can only be used with XML documents. XSL can also provide scripting capabilities.

Figure 24 shows some sample XSL code.

XSL Sample Code.

Figure 24: XSL Sample Code.

Figure 25 illustrates the XML flow through a typical use in NPS.

XML Flow Diagram.

Figure 25: XML Flow Diagram.

Note that the XML document should conform to the DTD. The XML data is sent to the XSL processor where it is massaged through the XSL StyleSheet. The resulting data (HTML, WAP, etc.) is sent to the application.

XSL Commands

Common XSL commands include the following:

<xsl:template>
<xsl:call-template>
<xsl:value-of>
<xsl:variable>
<xsl:for-each>
<xsl:sort>
<xsl:attribute>

Attribute Value Templates include:

<xsl:choose>
<xsl:when>
<xsl:otherwise>
<xsl:if>
<xsl:apply-templates>
<xsl:with-param>
<xsl:include>

For more information on these and other XSL command, the following book is recommended: Michael Kay's XSLT Programmer's Reference, chapters 1-3, 7, and 8.

Let's look at each of these commands in more detail.

<xsl:template> This is used to name a template, which is very similar to a procedure. It can also be used when there is a match to an XML element. This can be used to breakup your code into manageable reusable sections. For example:


<xsl:template name="Portal.BrandingArea">
XSL commands
</xsl:template>

You may also want to investigate: <xsl:call-template>

<xsl:call-template> This is used to call a named template. Note that there is no change to the current context. For example:


<xsl:call-template name="Portal.BrandingArea"/>

<xsl:value-of> This retrieves the value of an XML attribute/element or variable. For example:


<xsl:value-of select="$Count"/> (XSL variable)
<xsl:value-of select="@title"/> (XML attribute)
<xsl:value-of select="Email"/> (XML element)

<xsl:variable> This is used to define an XSL variable that can be used in the XSL stylesheet. You can define variables in an XSL file, and then include that file in other stylesheets to have global variables. Variables are static in XSL. An example would be:


<xsl:variable name="bgcolor">red</xsl:variable>

<xsl:for-each> This command is used to loop through XML elements that match the specified test. An example:


<xsl:for-each select="Gadget[@title!='MyGadget']">
XSL Commnds
</xsl:for-each>

<xsl:sort> This command is used to sort a list of XML nodes, typically with the <xsl:for-each> command. And example would look like this:


<xsl:for-each select="Gadget">
<xsl:sort select+@priority" data-type="number"
order="ascending"/>
<xsl:sort select="@title" order="ascending"/>
XSL commands
</xsl:for-each>

<xsl:choose> This command is used to choose an appropriate action based on a test (like a Case statement). This command can be used in conjunction with <xsl:when> and <xsl:otherwise>. For example:


<xsl:choose>
<xsl:when test="@title''Collaboration'">
XSL Comands
</xsl:when>
<xsl:otherwise>XSL Commands</xsl:otherwise>
</xsl:choose>

<xsl:when> This command is used in an <xsl:choose> statement. The first "when" test that is true is processed. For example:


<xsl:when test="@title='Collaboration'">
XSL Commands
<xsl:when>
<xsl:when test="@title='Preferences'">
XSL Commands
</xsl:when>

<xsl:otherwise> This command is used to catch all action in an <xsl:choose> statement. This is the equivalent to "if all else fails, do this." And example would be:


<xsl:otherwise>
XSL Commands
</xsl:otherwise>

<xsl:if> This command is used to do a conditional test - and if true, the XSL commands are processed. For example:


<xsl:if test="@title='Collaboration'">
XSL Commands
</xsl:if>

<xsl:attribute> This command is used to set attributes for XSL elements. For example:


<font>
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="$UseColor='yes'">
blue
</xsl:when>
<xsl:otherwise>
black
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</font>

Attribute Value Templates

Attribute Value Templates are used to set attribute values using XSL. For example:


<font color={$myColor}"Hello World</font>
<font color="{x/y/z/FontColor}"Hello World</font>

Note: multiple queries work just fine. For example:


<img src="{$myDir}/myIcon{$iconNumber}.gif" />

Let's look at several of the Attribute Value Template commands in detail.

<xsl:apply-templates> This command applies the templates of the selected XML elements. An example would be:


<xsl:apply-templates select="Gadget[@state='max']"/>

<xsl:with-param> This command is typically used with the <xsl:apply-templates> statement. It passes a parameter to a template. For example:


<xsl:apply-templates select="Page">
<xsl:sort data-type="number" order="ascending" select="@prioirty"/>
<xsl:sort order="ascending" select="@name"/>
<xsl:with-param name="level" select="number($level + 1)"/>
<xsl:apply-templates>

<xsl:include> This command includes an additional XSL file. It provides the ability to breakup XSL code into smaller files, as illustrated in the following example:


<xsl:include href="LayoutSettings.xsl"/>

Here is a listing of some of the available XSL Functions.

XSL Function What It Does
Concat()
Concatenate strings
Floor()
Returns the largest integer that is less than or equal to the numeric expression
Format-number()
Formats a string
Normalize-space()
Remove excess white space
Position()
Current position in the XML document
Starts-with()
Checks to see if a string starts with a specified string
String-length()
Finds the length of a string
Substring()
Returns a substring of a string
Substring-after()
Returns all of a string after a point
Substring-before()
Returns all of a string up to a point
Translate()
Translates one character to another

For more information about XSL programming, visit: http://www.w3.org/Style/XSL/

Now that we have a good theoretical understanding of NPS, its components and the technologies that make NPS a reality, let's move on to NPS Gadgets.

Previous Contents Next
download sample file