WEB ENGINEERING

Archive for the ‘XML’ Category

XSL

XSL stands for EXtensible Stylesheet Language.

XSL = Style Sheets for XML

XSL consists of:

  • XSLT – a language for transforming XML documents
  • XPath – a language for navigating in XML documents
  • XSL-FO – a language for formatting XML documents

XSLT

XSLT stands for XSL Transformations

XSLT transforms an XML document into another XML document

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.

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 to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.

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.

Example

catalog.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>

<cd>
<title>Rising Force</title>
<artist>Yngwie Malmsteen</artist>
<label>Polydor</label>
<price>10.90</price>
<year>1984</year>
</cd>

<cd>
<title>Fire and Ice</title>
<artist>Yngwie Malmsteen</artist>
<label>Elektra</label>
<price>9.90</price>
<year>1992</year>
</cd>

<cd>
<title>The Seventh Sign</title>
<artist>Yngwie Malmsteen</artist>
<label>CMC International - Spitfire</label>
<price>9.90</price>
<year>1994</year>
</cd>

</catalog>

cdcatalog.xsl


<?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>
<th>Label</th>
<th>Price</th>
<th>Year</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="label"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="year"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

OUTPUT


Note

The <xsl:template> element is used to build templates.

The match attribute is used to associate a template with an XML element. The value of the match attribute is an XPath expression (i.e. match=”/” defines the whole document).

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

The select attribute contains an XPath expression. An XPath expression works like navigating a file system; a forward slash (/) selects subdirectories.

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

FILTERING


<xsl:for-each select="catalog/cd[year='1992']">

 

OUTPUT


Advertisements

XML stands for eXtensible Markup Language.

XML is a markup language much like HTML

XML was designed to carry data, not to display data

XML tags are not predefined.

XML is designed to be self-descriptive, and has user defined tags.

XML is a W3C Recommendation

XML was designed to transport and store data.

XML is a software- and hardware-independent tool for carrying information.

XML Separates Data from HTML

XML is Used to Create New Internet Languages

•    XHTML the latest version of HTML
•    WSDL for describing available web services
•    WAP and WML as markup languages for handheld devices
•    RSS languages for news feeds
•    SMIL for describing multimedia for the web

XML documents are characterized by two distinct properties: well-formedness and validity.

XML with correct syntax is “Well Formed” XML.

XML validated against a DTD or XML Schema is “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 be quoted

A “Valid” XML document is a “Well Formed” XML document, which also conforms to the rules of a Document Type Definition (DTD)

XML DTD

It defines the document structure with a list of legal elements and attributes.

A DTD can be declared inline inside an XML document, or as an external reference.

Defining XML DTD

<?xml version="1.0" ?>
<!DOCTYPE bookstore [
<!ELEMENT bookstore (book+)  >
<!ELEMENT book (title,author,year,price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST  book category CDATA #REQUIRED>
<!ATTLIST  title lang CDATA #REQUIRED>
]>

<bookstore>
<bookLLLLLLLLLLLLLL category="PHP">
<title langLLLLLL="en">PHP Made Easy</title>
<author>XYZ</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

</bookstore>

Description

  • !DOCTYPE bookstore defines that the root element of this document is bookstore
  • !ELEMENT bookstore (book+) defines that the there should be atleast one element of type book.
  • !ELEMENT book defines that the note element contains four elements: “tile, author, year, price”
  • !ELEMENT title defines the to element  to be of type “#PCDATA”
  • !ELEMENT author defines the from element to be of type “#PCDATA”
  • !ELEMENT year defines the heading element to be of type “#PCDATA”
  • !ELEMENT price defines the body element to be of type “#PCDATA”

The Building Blocks of XML Documents

Seen from a DTD point of view, all XML documents (and HTML documents) are made up by the following building blocks:

  • Elements
  • Attributes
  • Entities
  • PCDATA
  • CDATA

PCDATA means parsed character data.

PCDATA is text that WILL be parsed by a parser. The text will be examined by the parser for entities and markup.

CDATA means character data.

CDATA is text that will NOT be parsed by a parser.

Tags inside the text will NOT be treated as markup and entities will not be expanded.

Validating With the XML Parser

If XML file has error  and we try to open the XML document, the XML Parser might generate an error. By accessing the parseError object, you can retrieve the error code, the error text, or even the line that caused the error.

Note: The load( ) method is used for files, while the loadXML( ) method is used for strings.

Javascript code to catch error.


<html>
<body>
<h3>This demonstrates a parser error:</h3>

<script type="text/javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.validateOnParse="true";
xmlDoc.load("bookstore.xml");

document.write("<br />Error Code: ");
document.write(xmlDoc.parseError.errorCode);
document.write("<br />Error Reason: ");
document.write(xmlDoc.parseError.reason);
document.write("<br />Error Line: ");
document.write(xmlDoc.parseError.line);
</script>

</body>
</html>

OUTPUT


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 50 other followers

What I am Upto Now

Follow Me On Twitter

Blog Stats

  • 75,314 hits

Live Traffic

My Paintings

%d bloggers like this: