0
2.9kviews
Explain XML, XSI. and XPATH with Example.

Mumbai University > information technology > sem 4> Web Programming

Marks: 10M

Year : May16

1 Answer
0
20views

Displaying XML with XSLT:

  • XSLT (eXtensible Stylesheet Language Transformations) is the recommended style sheet language for XML.

  • XSLT is far more sophisticated than CSS. 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.

  • XSLT uses XPath to find information in an XML document.

XSLT Example

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>

<food>
<name>Belgian Waffles</name>
<price>$5.95\lt/price\gt
    \ltdescription\gtTwo of our famous Belgian Waffles with plenty of real maple syrup\lt/description\gt
    \ltcalories\gt650\lt/calories\gt
    \lt/food\gt
    
    \ltfood\gt
    \ltname\gtStrawberry Belgian Waffles\lt/name\gt
    \ltprice\gt$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>

<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95\lt/price\gt
    \ltdescription\gtLight Belgian waffles covered with an assortment of fresh berries and whipped cream\lt/description\gt
    \ltcalories\gt900\lt/calories\gt
    \lt/food\gt
    
    \ltfood\gt
    \ltname\gtFrench Toast\lt/name\gt
    \ltprice\gt$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>

<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>

</breakfast_menu>
Use XSLT to transform XML into HTML, before it is displayed in a browser:

Example XSLT Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
    <xsl:value-of select="price"/>
    </div>
  <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
    <p>
    <xsl:value-of select="description"/>
    <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
    </p>
  </div>
</xsl:for-each>
</body>
</html>

Transform the XML Document with XSLT

XPath:

  • XPath is a major element in the XSLT standard.

  • XPath can be used to navigate through elements and attributes in an XML document.

  • XPath is a syntax for defining parts of an XML document

  • XPath uses path expressions to navigate in XML documents

  • XPath contains a library of standard functions

  • XPath is a major element in XSLT and in XQuery

  • XPath is a W3C recommendation

  • XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system.

  • XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.

  • XPath is a major element in the XSLT standard.

  • With XPath knowledge you will be able to take great advantage of XSL.

XPath Example

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book category="cooking">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</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">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.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>

In the table below we have listed some XPath expressions and the result of the expressions:

XPath Expression Result
/bookstore/book[1] Selects the first book element that is the child of the bookstore element
/bookstore/book[last()] Selects the last book element that is the child of the bookstore element
/bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element
/bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element
//title[@lang] Selects all the title elements that have an attribute named lang
//title[@lang='en'] Selects all the title elements that have a "lang" attribute with a value of "en"
/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00
/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00
Please log in to add an answer.