使用PHP和XSL stylesheets转换XML文档
PHP是不少在Web开发领域奋战的勇士们所选用的武器,因为它是一种很直观的编程语言,有强大的函数,良好的跨平台兼容性,还有它是免费的。从网上的小商店到大型企业的网站都能看到PHP的影子。PHP有一点特性经常被人们忽视,那就是和XSLstylesheets合作对XML进行解析的能力。下面就让我们来看看怎样在PHP中设置一个XSL解析器以及你该如何使用这一功能。
例子
列表A是一个简单的订单文档,我们会将这个文档输入XSL解析器。同时,列表B中的XSLstylesheet也会被输入XSL解析器。
ListingA:order.xml
<?xmlversion="1.0"?> <Order> <Account>9900234</Account> <Itemid="1"> <SKU>1234</SKU> <PricePer>5.95</PricePer> <Quantity>100</Quantity> <Subtotal>595.00</Subtotal> <Description>SuperWidgetClamp</Description> </Item> <Itemid="2"> <SKU>6234</SKU> <PricePer>22.00</PricePer> <Quantity>10</Quantity> <Subtotal>220.00</Subtotal> <Description>MightyFoobarFlange</Description> </Item> <Itemid="3"> <SKU>9982</SKU> <PricePer>2.50</PricePer> <Quantity>1000</Quantity> <Subtotal>2500.00</Subtotal> <Description>DeluxeDoohickie</Description> </Item> <Itemid="4"> <SKU>3256</SKU> <PricePer>389.00</PricePer> <Quantity>1</Quantity> <Subtotal>389.00</Subtotal> <Description>MuckalucketBucket</Description> </Item> <NumberItems>1111</NumberItems> <Total>3704.00</Total> <OrderDate>07/07/2002</OrderDate> <OrderNumber>8876</OrderNumber> </Order> ListingB:order.xsl <?xmlversion="1.0"?> <xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:paramname="column"select="’SKU’"/> <xsl:paramname="order"select="’ascending’"/> <xsl:templatematch="/"> <html> <body> <xsl:apply-templatesselect="Order"> <xsl:with-paramname="sortcolumn"select="$column"/> <xsl:with-paramname="sortorder"select="$order"/> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:templatematch="Order"> <xsl:paramname="sortcolumn"/> <xsl:paramname="sortorder"/> <tableborder="1"> <tr> <th>Account</th> <th>SKU</th> <th>Description</th> <th>Price</th> <th>Quantity</th> <th>Subtotal</th> </tr> <xsl:apply-templatesselect="Item"> <xsl:sortselect="*[name()=$sortcolumn]"order="{$sortorder}"/> </xsl:apply-templates> </table> </xsl:template> <xsl:templatematch="Item"> <tr> <td><xsl:value-ofselect="../Account"/></td> <td><xsl:value-ofselect="SKU"/></td> <td><xsl:value-ofselect="Description"/></td> <td><xsl:value-ofselect="PricePer"/></td> <td><xsl:value-ofselect="Quantity"/></td> <td><xsl:value-ofselect="Subtotal"/></td> </tr> </xsl:template> </xsl:stylesheet> |
- 共2页:
- 上一页
- 1
- 2
- 下一页
PHP之友评论