You are on page 1of 37

UNIT 3 XML TECHNOLOGY FAMILY

XML: An Introduction

XML stands for Extensible Markup Language . "XML is a cross-platform, software and hardware independent tool for transmitting information". It is a markup language much like HTML used to describe data. In XML, tags are not predefined. A user defines his own tags and XML document structure like Document Type Definition (DTD) , XML Schema to describe the data. Hence it is self-descriptive too.It is just plain text with the addition of some XML tags enclosed in angle brackets. In a simple text editor, the XML document is easily visible . Importance of XML: There are number of reasons that contributes to the XML's increasing acceptance , few of them are: Plain Text In XML it is easy to create and edit files with anything from a standard text editor to a visual development environment. XML also provides scalability for anything from small configuration files to a company-wide data repository. Data Identification The markup tags in XML documents identifiy the information and break up the data into parts for example.. a search program can look for messages sent to particular people from the rest of the message. Different parts of the information are identified and further they can be used in different ways by different applications. Stylability When display matters, the stylesheet standard, XSL (an advance feature of XML), lets you dictate over the convectional designs ( like using HTML) to portray the data. XML being stylefree, uses different stylesheets to produce output in postscript, TEX, PDF, or some new format that hasn't even been invented yet. A user can use a simple XML document to display data in diverse formats like

a plain text file an XHTML file a WML (Wireless Markup Language) document suitable for display on a PDA an Adobe PDF document suitable for hard copy a VML (Voice Markup Language) dialog for a voicemail information system an SVG (Scalable Vector Graphic) document that draws pictures of thermometers and water containers

Universally Processed Apart from being valid , restrictions are imposed on a xml file to abide by a DTD or a Schema to make it well-formed .Otherwise, the XML parser won't be able to read the data. XML is a vendor-neutral standard, so a user can choose among several XML parsers to process XML data. Hierarchical Approach XML documents get benefitted from their hierarchical structure. Hierarchical document structures are, faster to access. They are also easier to rearrange, because each piece is delimited. This makes xml files easy to modify and maintain. Inline Reusabiliy XML documents can be composed of separate entities. XML entities can be included "in line" in a XML document. And this included sections look like a normal part of the document .A user can single-source a section so that an edit to it is reflected everywhere the section is used, and yet a document composed looks like a one-piece document. Applications of XML: Although there are countless applications that use XML, here are a few examples of the applications that are making use of this technology. XML is the most common tool for data manipulation and data transmission. Refined search results - With XML-specific tags, search engines can give users more refined search results. A search engine seeks the term in the tags, rather than the entire document, giving the user more precise results.

EDI Transactions - XML has made electronic data interchange (EDI) transactions accessible to a broader set of users. XML allows data to be exchanged, regardless of the computing systems or accounting applications being used. Cell Phones - XML data is sent to some cell phones, which is then formatted by the specification of the cell phone software designer to display text, images and even play sounds! File Converters - Many applications have been written to convert existing documents into the XML standard. An example is a PDF to XML converter. VoiceXML - Converts XML documents into an audio format so that a user can listen to an XML document. Comparing XML with HTML: XML describes and focuses on the data while HTML only displays and focuses on how data looks. HTML is all about displaying information but XML is all about describing information. XML is used to store data in files and for sharing data between diverse applications. XML hold only data. Different presentation logics could be applied to display the xml data in the required format. XML is the best way to exchange information. Unlike HTML document where data and display logic are available in the same file. XML is Free and Extensible.XML tags are not predefined. User must "invent" his tags. XML allows the user to define his own tags and document structure. The tags used to mark up HTML documents and the structure of HTML documents are predefined. The author of HTML documents can only use tags that are defined in the HTML standard (like <p>, <h1>, etc.). XML Tags are Case Sensitive In XML opening and closing tags must therefore be written with the same case: <message>This is correct</message> HTML tags are not case sensitive.

XML Elements Must be Properly Nested.Improper nesting of tags makes no sense to XML. <b><i>This text is bold and italic</i></b> In HTML some elements can be improperly nested within each other. <b><i>This text is bold and italic</b></i> In XML all elements must be properly nested within each other like this: XML is a Complement to HTML.XML is not a replacement for HTML. XML Syntax Rules The syntax rules for XML are very simple and strict. These are easy to learn and use. creating software that can read and manipulate XML is very easy. Xml enables an user to create his own tags. XML Elements XML Elements are extensible. They have relationships. They have simple naming rules.XML documents can be extended to carry more information. example: <?xml version="1.0" encoding="ISO-8859-1"?> <E-mail> <To>Rohan</To> <From>Amit</From> <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail> E-mail To: Rohan From: Amit Be ready for a cruise...i will catch u tonight We can easily add one more tag e.g.<Subject>in the xml document. So the new modified xml document will look like this : <?xml version="1.0" encoding="ISO-8859-1"?> <E-mail> <To>Rohan</To> <From>Amit</From>

<Subject>Surprise....</Subject> <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail> Now the new generated output will look like this E-mail To: Rohan From: Amit Subject : Surprise.... Be ready for a cruise...i will catch u tonight XML Elements have Relationships Elements in a xml document are related as parents and children. <?xml version="1.0" encoding="ISO-8859-1"?> <E-mail> <To>Rohan</To> <From>Amit</From> <Subject>Surprise....</Subject> <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail> Here, E-mail is the root element(parent element) while To, From, Subject and Body(siblings (or sister elements) are the child elements of the E-mail because they have the same parentage. Hence all the XML Elements have Relationships. XML Element Naming : Know-hows XML elements must follow these naming conventions: Names must not start with a number or punctuation character but it can contain letters, numbers, and other characters without spaces. Names must not start with the letters xml (or XML, or Xml, etc) XML Attributes XML elements can have attributes in the start tag, just like HTML. Attributes are used to provide additional information about elements. XML Attributes

XML elements can have attributes in the start tag, just like HTML. Attributes are used to provide additional information about elements. Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element: <file type="gif">roseindia.gif</file> Use the quote styles: "red" or 'red' Attribute values must always be enclosed in quotes. Use either single or double quotes eg.. <color="red"> or like this: <color='red'> Note: If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example: <name='Rose "India" Net'> Note: If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example: <name="Rose 'India' Net"> Use of Elements vs. Attributes If you start using attributes as containers for XML data, you might end up with the documents that are both difficult to maintain and manipulate. So the user should use elements to describe the data. Use attributes only to provide data that is not relevant to the reader. Only metadata (data about data) should be stored as attributes, and that data itself should be stored as elements. This is not the way to use attributes eg.. <?xml version="1.0" encoding="ISO-8859-1"?> <E-mail To="Rohan" From="Amit"

Subject="Surprise...." <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail> Try to avoid using attributes in few of the situations. Lot of problems occur with using attributes values. They are not easily expandable and cannot contain multiple values .They are not easy to test against a Document Type Definition and are also unable to describe their structure. Becomes more irritating ,because of its difficultly to get manipulated by program code. Here is an example, demonstrating how elements can be used instead of attributes. The following three XML documents contain exactly the same information. A date attribute is used in the first, a date element is used in the second, and an expanded date element is used in the third: <?xml version="1.0" encoding="ISO-8859-1"?> <E-mail date="15/05/07"> <To>Rohan</To> <From>Amit</From> <Subject>Surprise....</Subject> <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail> First xml document contains date as a attribute which can not be further extended. But date used a element in second document makes it more flexible. <?xml version="1.0" encoding="ISO-8859-1"?> <E-mail > <date="15/05/07"> <To>Rohan</To> <From>Amit</From> <Subject>Surprise....</Subject> <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail> Second xml document can be further extended as..

<?xml version="1.0" encoding="ISO-8859-1"?> <E-mail > <date> <day>12</day> <month>11</month> <year>99</year> </date> <To>Rohan</To> <From>Amit</From> <Subject>Surprise....</Subject> <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail> XML Validation XML validated against a DTD or a Schema is a Valid XML.XML with correct syntax is Well Formed XML.XML validated against a DTD or a Schema is a Valid XML. Well Formed XML Documents A "Well Formed" XML document has correct XML syntax.

XML documents must have a root element XML elements must have a closing tag XML tags are case sensitive XML elements must be properly nested XML attribute values must always be quoted

<?xml version="1.0" encoding="ISO-8859-1"?> <E-mail> <To>Rohan</To> <From>Amit</From> <Subject>Surprise....</Subject> <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail> Valid XML Documents: A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD) or a XML Schema . The following xml document is validated against a DTD , notice the highlighted text.

<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE e-mail SYSTEM "InternalE-mail.dtd"> <E-mail> <To>Rohan</To> <From>Amit</From> <Subject>Surprise....</Subject> <Body>Be ready for a cruise...i will catch u tonight</Body> </E-mail XML DTD : A DTD defines the legal elements of an XML document. The purpose of a

DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. XML Schema: XML Schema is an XML based alternative to DTD .W3C supports an

alternative to DTD called XML Schema.

Electronic Data Interchange


Electronic data interchange (EDI) is the structured transmission of data between organizations by electronic means. It is used to transfer electronic documents or business data from one computer system to another computer system, i.e. from one trading partner to another trading partner without human intervention.It is more than mere e-mail; for instance, organizations might replace bills of lading and even cheques with appropriate EDI messages. It also refers specifically to a family of standards. Definition: EDI as "the computer-to-computer interchange of strictly formatted messages that represent documents other than monetary instruments. EDI implies a sequence of messages between two parties, either of whom may serve as originator or recipient. The formatted data representing the documents may be transmitted from originator to recipient via telecommunications or physically transported on electronic storage media." It distinguishes mere electronic communication or data exchange, specifying that "in EDI, the usual processing of received messages is by computer only. Human intervention in the processing of a received message is typically intended only for error conditions, for quality review, and for special situations. For example, the transmission of binary or textual data is not EDI as defined here unless the data are treated as one or more data elements of an EDI message and are not normally intended for human interpretation as part of online data processing." EDI can be formally defined as the transfer of structured data, by agreed message standards, from one computer system to another without human intervention.

Standards

EDI is considered to describe the rigorously standardized format of electronic documents. EDI is very useful in supply chain. There are four major sets of EDI standards: The UN-recommended UN/EDIFACTis the only international standard and is predominant outside of North America. The US standard ANSI ASC X12 (X12) is predominant in North America. The TRADACOMS standard developed by the ANA (Article Numbering Association) is predominant in the UKretail industry. The ODETTE standard used within the European automotive industry

The EDI standard says which pieces of information are mandatory for a particular document, which pieces are optional and give the rules for the structure of the document. The standards are like building codes.

Specifications
Organizations that send or receive documents between each other are referred to as "trading partners" in EDI terminology. The trading partners agree on the specific information to be transmitted and how it should be used. This is done in human readable specifications (also called Message Implementation Guidelines). While the standards are analogous to building codes, the specifications are analogous to blue prints. (The specification may also be called a "mapping," but the term mapping is typically reserved for specific machine-readable instructions given to the translation software.)

Transmission
Trading partners are free to use any method for the transmission of documents. In the past one of the more popular methods was the usage of a bisync modem to communicate through a value added network (VAN). Some organizations have used direct modem to modem connections and bulletin board systems(BBS), and recently there has been a move towards using some of the many Internet protocols for transmission, but most EDI is still transmitted using a VAN. In the healthcare industry, a VAN is referred to as a "clearinghouse".

Value-added networks
VAN (value-added network) acts as a regional post office. They receive transactions, examine the 'from' and the 'to' information, and route the transaction to the final recipient. VANs provide a number of additional services, e.g. retransmitting documents, providing third party audit information, acting as a gateway for different transmission methods, and handling telecommunications support. Because of these and other services VANs provide, businesses frequently use a VAN even when both trading partners are using Internet-based protocols. Healthcare clearinghouses perform many of the same functions as a VAN, but have additional legal restrictions that govern VANs also provide an advantage with certificate replacement in AS2 transmissions. Because each node in a traditionally business-related AS2 transmission usually involves a security certificate,

routing a large number of partners through a VAN can make certificate replacement much easier. Value Added Networks are the go-between in EDI communications. The VAN is responsible for routing, storing and delivering EDI messages. They also provide delivery reports Depending on the VAN type, messages may need extra envelopes or may be routed using intelligent *VANs which are able to read the EDI message itself. VANs may be operated by various entities: o telecom companies; o industry group consortia; o a large company interacting with its suppliers/vendors.

Internet/AS2
The Internet transmission was handled by nonstandard methods between trading partners usually involving FTPor email attachments. There are also standards for embedding EDI documents into XML. Many organizations are migrating to this protocol to reduce costs. AS2 (Applicability Statement 2) is the draft specification standard by which vendor applications communicate EDI or other business-to-business data (such as XML) over the Internet using HTTP, a standard used by the World Wide Web. AS2 provides security for the transport payload through digital signatures and data encryption, and ensures reliable, non-repudiable delivery through the use of receipts.

Advantages of using EDI over paper systems


EDI and other similar technologies save a company money by providing an alternative to, or replacing information flows that require a great deal of human interaction and materials such as paper documents, meetings, faxes, etc. Even when paper documents are maintained in parallel with EDI exchange, e.g. printed shipping manifests, electronic exchange and the use of data from that exchange reduces the handling costs of sorting, distributing, organizing, and searching paper documents. EDI and similar technologies allow a company to take advantage of the benefits of storing and manipulating data electronically without the cost of manual entry. Another advantage of EDI is reduced errors, such as shipping and billing errors, because EDI eliminates the need to rekey documents on the destination side. One very important advantage of EDI over paper documents is the speed in which the trading partner receives and incorporates the information into their system thus greatly reducing cycle times. For this reason, EDI can be an important component of just-in-time production systems.

Barriers to implementation
There are a few barriers to adopting electronic data interchange..One of the most significant barriers is the accompanying business process change. Existing business processes built around slow paper handling may not be suited for EDI and would require changes to accommodate automated processing of business documents.

Another significant barrier is the cost in time and money in the initial set-up. The key hindrance to a successful implementation of EDI is the perception many businesses have of the nature of EDI..Increased efficiency and cost savings drive the adoption of EDI for most trading partners.

XSL Languages
It Started with XSL
XSL stands for EXtensible Stylesheet Language. The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XMLbased Stylesheet Language.

CSS = Style Sheets for HTML


HTML uses predefined tags, and the meaning of each tag is well understood. The <table> tag in HTML defines a table - and a browser knows how to display it. Adding styles to HTML elements are simple. Telling a browser to display an element in a special font or color, is easy with CSS.

XSL = Style Sheets for XML


XML does not use predefined tags (we can use any tag-names we like), and therefore the meaning of each tag is not well understood. A <table> tag could mean an HTML table, a piece of furniture, or something else - and a browser does not know how to display it.

XSL describes how the XML document should be displayed!

XSL - More Than a Style Sheet Language

XSL consists of three parts: XSLT - a language for transforming XML documents XPath - a language for navigating in XML documents XSL-FO - a language for formatting XML documents

Why Stylesheets?

XML is not a fixed tag set (like HTML) and has no (application) semantics XML markup does not (usually) include formatting information Reuse: the same content canlook different in different contexts Multiple output formats: different media (paper, online), different sizes (manuals, reports), different classes of output devices (workstations, hand-held devices) Styles tailored to the reader's preference (e.g., accessibility): print size, color, simplified layout for audio readers Standardized styles: corporate stylesheets can be applied to the content at any time Freedom from style issues for content authors: technical writers needn't be concerned with layout issues because the correct style can be applied later

Therefore there must be something in addition to the XML document that provides information on how to present or otherwise process the XML Options for displaying XML

What Does a Stylesheet Do? A stylesheet specifies the presentation of XML information using two basic categories of techniques:

An optional transformation of the input document into another structure o generation of constant text o suppression of content o moving text (e.g., exchanging the order of the first and last name) o duplicating text (e.g., copying titles to make a table of contents) o sorting o more complex transformations that "compute" new information in terms of the existing information A description of how to present the transformed information (i.e., a specification of what properties to associate to each of the various parts of the transformed information) Presentation Information

Description of how to present the (possibly transformed) data includes three levels of formatting information:

Specification of the general screen or page (or even audio) layout Assignment of the transformed content into basic "content container types" (e.g., lists, paragraphs, inline text) Specification of formatting properties (spacing, margins, alignment, fonts, etc.) for each resulting "container" Components of XSL

The full XSL language logically consists of three component languages which are described in three W3C (World Wide Web Consortium) Recommendations:

XPath: XML Path Language--a language for referencing specific parts of an XML document XSLT: XSL Transformations--a language for describing how to transform one XML document (represented as a tree) into another XSL: Extensible Stylesheet Language--XSLT plus a description of a set of Formatting Objects and Formatting Properties XML to Result Tree

An XSLT "stylesheet" transforms the input (source) document's tree into a structure called a result tree consisting of result objects

What is an XSL Stylesheet?


XSLT Stylesheets are XML documents; namespaces are used to identify semantically significant elements. Most stylesheets are stand-alone documents rooted at <xsl:stylesheet> (or <xsl:transform>). It is possible to have "single template" stylesheet/documents.

Note that it is the mapping from namespace abbreviation to URI that is important, not the literal namespace abbreviation "xsl:" that is used most commonly. Stylesheet Examples A small, complete stylesheet: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="doc"> <html> <head><title><xsl:value-of select="title"/></head> <body><xsl:apply-templates/></body> </html> </xsl:template> <xsl:template match="title"> <h1><xsl:apply-templates/></h1> </xsl:template>

<xsl:template match="para"> <p><xsl:apply-templates/></p> </xsl:template> </xsl:stylesheet> Understanding A Template Most templates have the following form: <xsl:template match="para"> <p><xsl:apply-templates/></p> </xsl:template>

The whole <xsl:template> element is a template The match pattern determines where this template applies Literal result elements come from non-XSL namespace(s) XSLT elements come from the XSL namespace Transformation is the Application of Templates

Templates transform portions of the source tree into portions of the result tree. The ordered accumulation of all the transformed portions forms the complete result tree. Individual templates are free to process elements from anywhere in the source tree.

Match Patterns (Locating Elements) One critical capability of a stylesheet language is to locate source elements to be styled. CSS, for example, does this with "selectors." FOSIs do it with "e-i-c's", elements in context. XSLT does it with "match patterns" defined by XPath.

XPath has an extensible string-based syntax inspired, in part, by the common "path/file" file system syntax:

para

Matches all <para> children in the current context para/emphasis Matches all <emphasis> elements that have a parent of <para> ancestor-or-self::*/@sepchar Matches the sepchar attribute on the current element or any ancestor of the current element numberedlist/listitem[position() mod 2 = 0] Matches odd listitems in a numbered list. Applying Style Recursively One model for applying style is to allow the process to run recursively, driven primarily by the document. A series of templates is created, such that there is a template to match each context, then these templates are recursively applied starting at the root of the document.

<xsl:template match="..."> <xsl:template match="section/title"> <h2><xsl:apply-templates/></h2> </xsl:template> <xsl:apply-templates> <xsl:apply-templates select="th|td"/>

There are two obstacles to overcome when using the recursive model, how to arbitrate between multiple patterns that match and how to process the same nodes in different contexts. These are solved by conflict resolution and modes, respectively. Applying Style Procedurally The other model for applying style is to select each action procedurally. A series of templates is created, such that each template explicitly selects and processes the necessary elements.

<xsl:for-each> <xsl:for-each select="row"> <tr> <xsl:for-each select="entry"> <td><xsl:value-of select="."/></td> </tr> </xsl:for-each> <xsl:template name="..."> <xsl:template name="admonition"> <xsl:param name="type">warning</xsl:param> ...

</xsl:template>

<xsl:call-template> <xsl:call-template name="admonition"> <xsl:with-param name="type">caution</xsl:with-param> </xsl:call-template> Conditional Processing

<xsl:if> Simple conditional (no "else") <xsl:if test="{$somecondition}"> <xsl:text>this text only gets used if $somecondition is true()</xsl:text> </xsl:if> <xsl:choose> Select among alternatives with <xsl:when> and <xsl:otherwise> <xsl:choose> <xsl:when test="$count > 2"><xsl:text>, and </xsl:text></xsl:when> <xsl:when test="$count > 1"><xsl:text> and </xsl:text></xsl:when> <xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise> </xsl:choose> Variables Variables can be used to save computed values.

Variables are created with <xsl:variable>. Variables are "single assignment" (no side effects) Variables are lexically scoped

Once created, variables can be used to generate content: <a href="{$file}">...</a> And control conditional processing: <xsl:if test="$count = 3">...</xsl:if> Creating the Result Tree Literal Result Elements Any element in a template rule that is not in the XSL (or other extension) namespace is copied literally to the result tree <p>...</p> XSL Elements Elements in the XSL namespace:

<xsl:text>

<xsl:value-of> <xsl:element> <xsl:attribute> ... Numbering and Sorting

You can:

Count source tree elements (chapters, list-items, stock quotes, etc.) Convert between number formats (1, B, iii, ...) Sort elements for presentation Overall XSL formatting capabilities

XSL FO formatting capabilities in XSL 1.0 are approximately the union of:

HTML + CSS capabilities most high quality print output capabilities including internationalization features

Not included are complex page layouts (e.g., magazine and newspaper layout), complex layout-driven formatting (e.g., copyfitting and complex floats), and looseleaf pagination (change page production) Formatting objects and properties XSL = XSLT + vocabulary of FOs and properties XSL defines a powerful set of formatting objects XSL uses (and extends) a set of Common Formatting Properties developed jointly with the CSS&FP (Cascading Style Sheet and Formatting Property) Working Group When a result tree uses this standardized set of formatting objects and properties, then an XSL-compliant formatter can process that result tree to produce the specified output Formatting Object Basics

Inline versus block objects Common formatting properties--harmonized with CSS Common Formatting Objects

page-sequence--a major part (such as front or body) in which the basic page layout may differ from other parts flow--a chapter- or section-like division within a page-sequence block--a paragraph (or title or block quote, etc.) inline--e.g., a font change within a paragraph wrapper--a "transparent" object usable as either a block or inline object that has no effect other than to provide a place to hang inheritable properties list FOs--list-block, list-item, list-item-label, list-item-body graphic--references an external graphic object

table FOs--mostly analogous to the standard (CALS, OASIS, HTML) table models

Basic properties

font properties margin and spacing properties border and padding properties keeps/breaks horizontal alignment/justification indentation more formatting object specific properties font properties margin and spacing properties border and padding properties keeps/breaks horizontal alignment/justification indentation more formatting object specific properties

XSLT Introduction
XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.XPath is a language for navigating in XML documents.
.What

is XSLT?
XSLT XSLT XSLT XSLT XSLT stands for XSL Transformations is the most important part of XSL transforms an XML document into another XML document uses XPath to navigate in XML documents is a W3C Recommendation

XSLT = XSL Transformations


XSLT is the most important part of XSL. XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element. With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree.

XSLT Uses XPath


XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.

How Does it Work?


In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document.

XSLT Browsers
All major browsers have support for XML and XSLT. Mozilla Firefox Internet Explorer Google Chrome Opera Apple Safari

XSLT - Transformation Correct Style Sheet Declaration


The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.
Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used! The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> or: <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document. The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0".

Start with a Raw XML Document


We want to transform the following XML document ("cdcatalog.xml") into XHTML: <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist>

. . </catalog>

<country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd>

Viewing XML Files in Firefox and Internet Explorer: Open the XML file (typically by clicking on a link) - The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu. Viewing XML Files in Netscape 6: Open the XML file, then right-click in XML file and select "View Page Source". The XML document will then be displayed with color-coded root and child elements. Viewing XML Files in Opera 7: Open the XML file, then right-click in XML file and select "Frame" / "View Source". The XML document will be displayed as plain text.

Create an XSL Style Sheet


Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

Link the XSL Style Sheet to the XML Document

Add the XSL style sheet reference to your XML document ("cdcatalog.xml"): <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> . . </catalog> If you have an XSLT compliant browser it will nicely transform your XML into XHTML.

XSLT <xsl:template> Element


An XSL style sheet consists of one or more set of rules that are called templates.A template contains rules to apply when a specified node is matched.

The <xsl:template> Element


The <xsl:template> element is used to build templates.The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body><h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td>.</td> <td>.</td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet>

XSLT <xsl:value-of> Element


The <xsl:value-of> element is used to extract the value of a selected node.

The <xsl:value-of> Element

The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation:

<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet>

XSLT <xsl:for-each> Element


The <xsl:for-each> element allows you to do looping in XSLT.

The <xsl:for-each> Element


The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd">

<tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr>


</xsl:for-each>

</table> </body> </html> </xsl:template> </xsl:stylesheet>

Filtering the Output


We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:foreach> element. <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> Legal filter operators are: = (equal) != (not equal) &lt; less than &gt; greater than

Take a look at the adjusted XSL style sheet: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

XSLT <xsl:sort> Element


The <xsl:sort> element is used to sort the output.

Where to put the Sort Information


To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

XSLT <xsl:if> Element


The <xsl:if> element is used to put a conditional test against the content of the XML file.

The <xsl:if> Element


To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.

Syntax
<xsl:if test="expression"> ...some output if the expression is true... </xsl:if>

Where to Put the <xsl:if> Element


To add a conditional test, add the <xsl:if> element inside the <xsl:for-each> element in the XSL file: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:if test="price &gt; 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

XSLT <xsl:choose> Element


he <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.

The <xsl:choose> Element


Syntax
<xsl:choose> <xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose>

Where to put the Choose Condition


To insert a multiple conditional test against the XML file, add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:

Example
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price &gt; 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

The code above will add a pink background-color to the "Artist" column WHEN the price of the CD is higher than 10.

Another Example
Here is another example that contains two <xsl:when> elements:

Example

<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price &gt; 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:when test="price &gt; 9"> <td bgcolor="#cccccc"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> The code above will add a pink background color to the "Artist" column WHEN the price of the CD is higher than 10, and a grey background-color WHEN the price of the CD is higher than 9 and lower or equal to 10.

XSLT <xsl:choose> Element


The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.

The <xsl:choose> Element


Syntax

<xsl:choose> <xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose>

Where to put the Choose Condition


To insert a multiple conditional test against the XML file, add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:

Example
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price &gt; 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

The code above will add a pink background-color to the "Artist" column WHEN the price of the CD is higher than 10.

Another Example
Here is another example that contains two <xsl:when> elements:

Example
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price &gt; 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:when test="price &gt; 9"> <td bgcolor="#cccccc"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> The code above will add a pink background color to the "Artist" column WHEN the price of the CD is higher than 10, and a grey background-color WHEN the price of the CD is higher than 9 and lower or equal to 10.

XSLT <xsl:apply-templates> Element


The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.

The <xsl:apply-templates> Element


The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.

If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed. Look at the following XSL style sheet:

Example
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template> </xsl:stylesheet>

XSLT Elements Reference


The XSLT elements from the W3C Recommendation (XSLT Version 1.0).

XSLT Elements

The links in the "Element" column point to attributes and more useful information about each specific element. FF: indicates the earliest version of Firefox that supports the tag IE: indicates the earliest version of Internet Explorer that supports the tag

Note: Elements supported in IE 5 may have NON-standard behavior, because IE 5 was released before XSLT became an official W3C Recommendation. Element apply-imports apply-templates attribute attribute-set call-template choose Description IE FF

Applies a template rule from an imported style 6.0 1.0 sheet Applies a template rule to the current element 5.0 1.0 or to the current element's child nodes Adds an attribute Defines a named set of attributes Calls a named template Used in conjunction with <when> and <otherwise> to express multiple conditional tests Creates a comment node in the result tree Creates a copy of the current node (without child nodes and attributes) 5.0 1.0 6.0 1.0 6.0 1.0 5.0 1.0

comment copy copy-of decimal-format

5.0 1.0 5.0 1.0

Creates a copy of the current node (with child 6.0 1.0 nodes and attributes) Defines the characters and symbols to be used 6.0 1.0 when converting numbers into strings, with the format-number() function Creates an element node in the output document Specifies an alternate code to run if the processor does not support an XSLT element Loops through each node in a specified node set 5.0 1.0 6.0 5.0 1.0

element fallback for-each if import

Contains a template that will be applied only if 5.0 1.0 a specified condition is true Imports the contents of one style sheet into another. Note: An imported style sheet has lower precedence than the importing style sheet 6.0 1.0

include

Includes the contents of one style sheet into 6.0 1.0 another. Note: An included style sheet has the same precedence as the including style sheet Declares a named key that can be used in the 6.0 1.0 style sheet with the key() function Writes a message to the output (used to report 6.0 1.0 errors)

key message

namespace-alias number otherwise output param preserve-space processing-instruction sort strip-space stylesheet template text transform value-of variable when with-param

Replaces a namespace in the style sheet to a different namespace in the output

6.0

Determines the integer position of the current 6.0 1.0 node and formats a number Specifies a default action for the <choose> element Defines the format of the output document Declares a local or global parameter Defines the elements for which white space should be preserved Writes a processing instruction to the output Sorts the output Defines the elements for which white space should be removed Defines the root element of a style sheet Rules to apply when a specified node is matched Writes literal text to the output Defines the root element of a style sheet Extracts the value of a selected node Declares a local or global variable Specifies an action for the <choose> element 5.0 1.0 6.0 1.0 6.0 1.0 6.0 1.0 5.0 1.0 6.0 1.0 6.0 1.0 5.0 1.0 5.0 1.0 5.0 1.0 6.0 1.0 5.0 1.0 6.0 1.0 5.0 1.0

Defines the value of a parameter to be passed 6.0 1.0 into a template

XSLT Functions
XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library.

XSLT Functions
XSLT includes over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more. The default prefix for the function namespace is fn: The URI of the function namespace is: http://www.w3.org/2005/xpath-functions Tip: Functions are often called with the fn: prefix, such as fn:string(). However, since fn: is the default prefix of the namespace, the function names do not need to be prefixed when called. The reference of all the built-in XSLT 2.0 functions is located in our XPath tutorial. In addition, there are the following built-in XSLT functions:

Name current() document() element-available() format-number() function-available() generate-id() key() system-property() unparsed-entity-uri()

Description Returns the current node Used to access the nodes in an external XML document Tests whether the element specified is supported by the XSLT processor Converts a number into a string Tests whether the function specified is supported by the XSLT processor Returns a string value that uniquely identifies a specified node Returns a node-set using the index specified by an <xsl:key> element Returns the value of the system properties Returns the URI of an unparsed entity

XForms: XForms is the next generation of HTML forms..XForms uses XML to create input forms on the Web. What Is XForms?

XForms is the next generation of HTML forms XForms is richer and more flexible than HTML forms XForms will be the forms standard in XHTML 2.0 XForms is platform and device independent XForms separates data and logic from presentation XForms uses XML to define form data XForms stores and transports data in XML documents XForms contains features like calculations and validations of forms XForms reduces or eliminates the need for scripting XForms is a W3C Recommendation

XForms Is The Successors Of HTML Forms Forms are an important part of many web applications today. An HTML form makes it possible for web applications to accept input from a user. Today, ten years after HTML forms became a part of the HTML standard, web users do complex transactions that are starting to exceed the limitations of standard HTML forms. XForms provides a richer, more secure, and device independent way of handling web input. We should expect future web solutions to demand the use of XForms-enabled browsers (All future browsers should support XForms). XForms Separate Data From Presentation XForms uses XML for data definition and HTML or XHTML for data display. XForms separates the data logic of a form from its presentation. This way the XForms data can be defined independent of how the end-user will interact with the application. XForms Uses XML To Define Form DataWith XForms, the rules for describing and validating data are expressed in XML. XForms Uses XML To Store And Transport Data With XForms, the data displayed in a form are stored in an XML document, and the data submitted from the form, are transported over the internet using XML. The data content is coded in, and transported as Unicode bytes.XForms Is Device Independent Separating data from presentation makes XForms device independent, because the data model can be used for all devices. The presentation can be customized for different user interfaces, like mobile phones, handheld devices, and Braille readers for the blind. Since XForms is device independent and based on XML, it is also possible to add XForms elements directly into other XML applications like VoiceXML (speaking web data), WML (Wireless Markup Language), and SVG (Scalable Vector Graphics).

You might also like