Cg0119 web database systems parsing xml using simplexml xslt
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

CG0119 Web Database Systems Parsing XML: using SimpleXML & XSLT PowerPoint PPT Presentation


  • 50 Views
  • Uploaded on
  • Presentation posted in: General

CG0119 Web Database Systems Parsing XML: using SimpleXML & XSLT. XSLT. E X tensible S tylesheet L anguage T ransformations What is it? w3c recommendation that applies style sheets to XML Allows styles to be applied for outputting XML, to a web browser for instance.

Download Presentation

CG0119 Web Database Systems Parsing XML: using SimpleXML & XSLT

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cg0119 web database systems parsing xml using simplexml xslt

CG0119 Web Database SystemsParsing XML: using SimpleXML & XSLT


Cg0119 web database systems parsing xml using simplexml xslt

XSLT

  • EXtensible Stylesheet Language Transformations

  • What is it?

    • w3c recommendation that applies style sheets to XML

    • Allows styles to be applied for outputting XML, to a web browser for instance.

    • Can also sort and filter data for output

    • Like CSS, separates formatting from content


Creating the xslt 1 6

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="heading"

select="'XML Grade Listing (using XSLT)'"/>

<xsl:output method="html" encoding="iso-8859-1" indent="no"/>

<xsl:template match="/">

<h1><xsl:value-of select="$heading"/></h1>

<table border="1">

...

<xsl:for-each select="grades/grade">

<tr>

<td><xsl:value-of select="gradeID"/></td>

<td><xsl:value-of select="result"/></td>

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>

Style sheet declaration (must be at the beginning of the file)‏

Creating the XSLT (1/6)‏


Creating the xslt 2 6

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="heading"

select="'XML Grade Listing (using XSLT)'"/>

<xsl:output method="html" encoding="iso-8859-1" indent="no"/>

<xsl:template match="/">

<h1><xsl:value-of select="$heading"/></h1>

<table border="1">

...

<xsl:for-each select="grades/grade">

<tr>

<td><xsl:value-of select="gradeID"/></td>

<td><xsl:value-of select="result"/></td>

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>

This is optional – purely demonstrates that parameters can be used in a similar way to variables.

Creating the XSLT (2/6)‏


Creating the xslt 3 6

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="heading"

select="'XML Grade Listing (using XSLT)'"/>

<xsl:output method="html" encoding="iso-8859-1" indent="no"/>

<xsl:template match="/">

<h1><xsl:value-of select="$heading"/></h1>

<table border="1">

...

<xsl:for-each select="grades/grade">

<tr>

<td><xsl:value-of select="gradeID"/></td>

<td><xsl:value-of select="result"/></td>

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>

Declares the output method. Can also output as “xml” and “text”.

Creating the XSLT (3/6)‏


Creating the xslt 4 6

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="heading"

select="'XML Grade Listing (using XSLT)'"/>

<xsl:output method="html" encoding="iso-8859-1" indent="no"/>

<xsl:template match="/">

<h1><xsl:value-of select="$heading"/></h1>

<table border="1">

...

<xsl:for-each select="grades/grade">

<tr>

<td><xsl:value-of select="gradeID"/></td>

<td><xsl:value-of select="result"/></td>

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>

Create a template that will be applied to any part of the XML that matches the expression. “/” is the expression for the root element.

Creating the XSLT (4/6)‏


Creating the xslt 5 6

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="heading"

select="'XML Grade Listing (using XSLT)'"/>

<xsl:output method="html" encoding="iso-8859-1" indent="no"/>

<xsl:template match="/">

<h1><xsl:value-of select="$heading"/></h1>

<table border="1">

...

<xsl:for-each select="grades/grade">

<tr>

<td><xsl:value-of select="gradeID"/></td>

<td><xsl:value-of select="result"/></td>

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>

value-of is used to extract the data from a XML element or a param.

XHTML can be inserted where ever required…

Creating the XSLT (5/6)‏


Creating the xslt 6 6

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="heading"

select="'XML Grade Listing (using XSLT)'"/>

<xsl:output method="html" encoding="iso-8859-1" indent="no"/>

<xsl:template match="/">

<h1><xsl:value-of select="$heading"/></h1>

<table border="1">

...

<xsl:for-each select="grades/grade">

<tr>

<td><xsl:value-of select="gradeID"/></td>

<td><xsl:value-of select="result"/></td>

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>

Selects & applies a style for every element of a node-set as defined by the expression in the ‘select’ attribute.

Creating the XSLT (6/6)‏


Displaying xml using xslt in php

Displaying XML using XSLT in PHP

// Load the XML data source

$xml= simplexml_load_file('grade.xml'); // one of these will work

$xml= DOMDocument::loadXML(file_get_contents('grade.xml');

// Load the XML stylesheet

$xsl = simplexml_load_file('grade.xsl'); // one of these will work

$xsl = DOMDocument::loadXML(file_get_contents('grade.xsl');

// create an xslt processor instance

$proc = new XSLTProcessor;

// import the xsl stylesheet into the xslt processor

$proc->importStyleSheet($xsl);

// Transform and output the xml data source

echo $proc->transformToXML($xml);


The output unsorted

The output (unsorted)…


Sorting data using the xslt

Sorting Data using the XSLT

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

...

<xsl:template match="/">

<h1><xsl:value-of select="$heading"/></h1>

<table border="1">

...

<xsl:for-each select="grades/grade">

<xsl:sort select="result"/>

<tr>

<td><xsl:value-of select="gradeID"/></td>

<td><xsl:value-of select="result"/></td>

</tr>

</xsl:for-each>

</table>

</xsl:template>

</xsl:stylesheet>


The output sorted

The output (sorted)…


Summary

Summary

  • XSLT allows style sheets to be applied to XML documents

    • Separates style from content

    • Can output as xml, html and text

Use the reference on the next page. It is a good resource


Recommended reading

Recommended Reading

http://www.w3schools.com/xsl/default.asp

- w3schools XSLT tutorial & reference


  • Login