1 / 14

Lecture 17

Lecture 17. Side remark: for-each equivalence again. <?xml version="1.0"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head><title>Second-hand cars</title></head> <body> <table rules="all">

Download Presentation

Lecture 17

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 17

  2. Side remark: for-each equivalence again <?xml version="1.0"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head><title>Second-hand cars</title></head> <body> <table rules="all"> <thead><tr><th>Item</th><th>Model</th><th>Engine Size</th><th>Price</th></tr></ thead> <tbody><xsl:apply-templates select="./stock/cars/item"> </tbody> </table></body></html></xsl:template> <xsl:template match="item"> <xsl:variable name="thisType" select="./type"/> <tr> <td> <xsl:value-of select="./@num"/> </td> <td> <xsl:value-of select="/stock/models/model[@id=$thisType]/name"/> </td> <td> <xsl:value-of select="/stock/models/model[@id=$thisType]/engineSize"/> </td> <td> <xsl:value-of select="./price"/> </td> </tr> </xsl:template> </xsl:transform>

  3. Conditional processing • In essence, conditional processing is already provided by the select and match attributes • However, XSLT also provides two construct for explicitly specifying conditional processing: the xsl:if element and xsl:choose element • The xsl:if element <xsl:iftest = boolean-expression> <!-- Content: template --> </xsl:if>

  4. Example usage of the xsl:if element <?xml version="1.0"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"><html> <head><title>Second-hand cars</title></head> <body> <table rules="all"> <thead><tr><th>Item</th><th>Model</th><th>Engine Size</th><th>Price</th></tr></thead> <tbody> <xsl:for-each select="./stock/cars/item"> <xsl:variable name="thisType" select="./type"/> <tr><td> <xsl:value-of select="./@num"/> </td> <td> <xsl:value-of select="/stock/models/model[@id=$thisType]/name"/> </td> <td> <xsl:value-of select="/stock/models/model[@id=$thisType]/engineSize"/> </td> <td> <xsl:if test="price>=2500">Too expensive</xsl:if> <xsl:if test="price &lt; 2500">price</xsl:if> </td> </tr> </xsl:for-each> </tbody> </table></body></html></xsl:template></xsl:transform>

  5. Conditional processing (contd.) • Format of the xsl:choose element <xsl:choose> <!-- Content: (xsl:when+, xsl:otherwise?) --> </xsl:choose > • Format of the xsl:when and xsl:otherwise elements <xsl:whentest = boolean-expression> <!-- Content: template --></xsl:when > <xsl:otherwise> <!-- Content: template --> </xsl:otherwise>

  6. Example usage of the xsl:choose element <?xml version="1.0"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"><html> <head><title>Second-hand cars</title></head> <body> <table rules="all"> <thead><tr><th>Item</th><th>Model</th><th>Engine Size</th><th>Price</th></tr></thead> <tbody> <xsl:for-each select="./stock/cars/item"> <xsl:variable name="thisType" select="./type"/> <tr><td> <xsl:value-of select="./@num"/> </td> <td> <xsl:value-of select="/stock/models/model[@id=$thisType]/name"/> </td> <td> <xsl:value-of select="/stock/models/model[@id=$thisType]/engineSize"/> </td> <td> <xsl:choose> <xsl:when test="price >= 2500">Too expensive</xsl:when> <xsl:otherwise><xsl:value-of select="price"/></xsl:otherwise> </xsl:choose> </td></tr> </xsl:for-each> </tbody> </table></body></html></xsl:template></xsl:transform>

  7. Calling templates by name <?xml version="1.0"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"><html> <head><title>Second-hand cars</title></head> <body> <table rules="all"> <thead><tr><th>Item</th><th>Model</th><th>Engine Size</th><th>Price</th></tr></thead> <tbody> <xsl:for-each select="./stock/cars/item"> <xsl:call-template name="show_item"/></xsl:foreach> </tbody> </table></body></html></xsl:template> <xsl:template name="show_item"> <xsl:variable name="thisType" select="./type"/> <tr><td> <xsl:value-of select="./@num"/> </td> <td> <xsl:value-of select="/stock/models/model[@id=$thisType]/name"/> </td> <td> <xsl:value-of select="/stock/models/model[@id=$thisType]/engineSize"/> </td> <td> <xsl:value-of select="./price"/> </td></tr> </xsl:template> </xsl:transform> • Unlike xsl:apply-templates, xsl:call-template does not change the current node or the current node list

  8. Passing parameter values to named templates <?xml version="1.0"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"><html> <head><title>Second-hand cars</title></head> <body> <table rules="all"> <thead><tr><th>Item</th><th>Model</th><th>Engine Size</th><th>Price</th></tr></thead> <tbody> <xsl:for-each select="./stock/cars/item"> <xsl:variable name="thisVar" select="./type"/> <xsl:call-template name="show_item"> <xsl:with-param name="thisNum" select="./@num"/> <xsl:with-param name="thisName" select="/stock/models/model[@id=$thisVar]/name"/> <xsl:with-param name="thisEngineSize" select="/stock/models/model[@id=$thisVar]/engineSize"/> <xsl:with-param name="thisPrice" select="./price"/> </xsl:call-template> </xsl:for-each> </tbody> </table></body></html> </xsl:template> <xsl:template name="show_item"> <xsl:param name="thisNum"/> <xsl:param name="thisName"/> <xsl:param name="thisEngineSize"/> <xsl:param name="thisPrice"/> <tr> <td> <xsl:value-of select="$thisNum"/> </td> <td> <xsl:value-of select="$thisName"/> </td> <td> <xsl:value-of select="$thisEngineSize"/> </td> <td> <xsl:value-of select="$thisPrice"/> </td> </tr> </xsl:template> </xsl:transform>

  9. A note on generating XML from PHP

  10. The Usual situation in PHP • By default, a PHP programmer does not have to generate headers for the HTTP responses that his programs generate • The PHP run-time system does this for him • It assumes that he is generating HTML • Thus, the headers it generates include the following Content-Type: text/html

  11. Example PHP program • The text that is generated look like XML • But the header that will precede this text in the response message will be Content-Type: text/html • Thus, the browser will treat the response body as if it were a HTML file and will simply ignore the unrecognizable tags

  12. Corrected PHP program • The header that will precede this text in the response message will be Content-Type: application/xml • The browser will treat the response body as if it were an XML file

  13. Server-side use of XSL stylesheets

  14. Example PHP program • The program below applies the stylesheet in carStyleSheet.xsl to the XML file in cars.xml and sends the resultant output to the browser which sent a HTTP request to the PHP program <?php $parser = xslt_create(); $html = xslt_process($parser, 'cars.xml','carStyleSheet.xsl'); xslt_free($parser); echo $html; ?>

More Related