140 likes | 219 Views
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">
E N D
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>
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>
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 < 2500">price</xsl:if> </td> </tr> </xsl:for-each> </tbody> </table></body></html></xsl:template></xsl:transform>
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>
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>
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
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>
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
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
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
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; ?>