选择显示字体大小

使用php和xsl stylesheets转换xml文档

     php是不少在web开发领域奋战的勇士们所选用的武器,因为它是一种很直观的编程语言,有强大的函数,良好的跨平台兼容性,还有它是免费的。从网上的小商店到大型企业的网站都能看到php的影子。
  
  php有一点特性经常被人们忽视,那就是和xsl stylesheets合作对xml进行解析的能力。下面就让我们来看看怎样在php中设置一个xsl解析器以及你该如何使用这一功能。
  
  例子
  列表a是一个简单的订单文档,我们会将这个文档输入xsl解析器。同时,列表b中的xsl stylesheet也会被输入xsl解析器。
  listing a: order.xml
  <?xml version="1.0" ?>
  <order>
   <account>9900234</account>
   <item id="1">
   <sku>1234</sku>
   <priceper>5.95</priceper>
   <quantity>100</quantity>
   <subtotal>595.00</subtotal>
   <description>super widget clamp</description>
   </item>
   <item id="2">
   <sku>6234</sku>
   <priceper>22.00</priceper>
   <quantity>10</quantity>
   <subtotal>220.00</subtotal>
   <description>mighty foobar flange</description>
   </item>
   <item id="3">
   <sku>9982</sku>
   <priceper>2.50</priceper>
   <quantity>1000</quantity>
   <subtotal>2500.00</subtotal>
   <description>deluxe doohickie</description>
   </item>
   <item id="4">
   <sku>3256</sku>
   <priceper>389.00</priceper>
   <quantity>1</quantity>
   <subtotal>389.00</subtotal>
   <description>muckalucket bucket</description>
   </item>
   <numberitems>1111</numberitems>
   <total>3704.00</total>
   <orderdate>07/07/2002</orderdate>
   <ordernumber>8876</ordernumber>
  </order>
  listing b: order.xsl
  <?xml version="1.0" ?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">
   <xsl:param name="column" select="’sku’"/>
   <xsl:param name="order" select="’ascending’"/>
   <xsl:template match="/">
   <html>
   <body>
   <xsl:apply-templates select="order">
   <xsl:with-param name="sortcolumn" select="$column" />
   <xsl:with-param name="sortorder" select="$order" />
   </xsl:apply-templates>
   </body>
   </html>
   </xsl:template>
   <xsl:template match="order">
   <xsl:param name="sortcolumn" />
   <xsl:param name="sortorder" />
   <table border="1">
   <tr>
   <th>account</th>
   <th>sku</th>
   <th>description</th>
   <th>price</th>
   <th>quantity</th>
   <th>subtotal</th>
   </tr>
   <xsl:apply-templates select="item">
   <xsl:sort select="*[name()=$sortcolumn]" order="{$sortorder}" />
   </xsl:apply-templates>
   </table>
   </xsl:template>
   <xsl:template match="item">
   <tr>
   <td><xsl:value-of select="../account" /></td>
   <td><xsl:value-of select="sku" /></td>
   <td><xsl:value-of select="description" /></td>
   <td><xsl:value-of select="priceper" /></td>
   <td><xsl:value-of select="quantity" /></td>
   <td><xsl:value-of select="subtotal" /></td>
   </tr>
   </xsl:template>
  </xsl:stylesheet>
  
  概述
  在这个例子中我们主要用到php中的三个xsl函数。首先我们要创建一个xsl引擎的实例,然后把所有要输入的文档输入这个xsl引擎进行处理,并得到返回结果,最后,当我们再也不需要这个xsl引擎时就关闭它。
  
  创建、处理、关闭
  我们将要在内存中新建一个xsl进程。为了方便在其他xsl函数中使用这个xsl进程,php会给我们提供这个xsl进程的句柄,而不是一个对象。建立这个xsl引擎的命令是xslt_create。函数返回一个句柄,如下所示:
  $handle = xslt_create();
  为了真正的解析xml文档并使xslt能够进行处理,你必须使用php中的xslt_process函数。这个函数需要获取几个不同的参数。
  在这里我们使用一个很基本的方法,为xslt_process提供三个参数。第一个参数是我们较早前创建的那个xsl引擎的句柄。第二个参数是输入的xml文档的文件名。第三个参数是输入的xsl文件的文件名。这个函数会返回处理结果。下面是例子:
  $return = xslt_process($handle, $xmlfile, $xslfile);
  最后我们要用到的函数是xslt_free。这个函数用来杀掉内存中的xsl引擎实例并释放出内存空间。它只需要一个参数,就是内存中这个xsl实例的句柄。下面是个例子:
  xslt_free($handle);
  综合实现
  下面让我们结合上面的各个代码片断实现php通过xsl stylesheets来处理xml文档的方法。我们使用列表a作为我们的输入xml文档,列表b作为我们xsl输入。列表c是这个例子的完整php代码:
  listing c: order.php
  <?php
  $xmlfile = "order.xml";
  $xslfile = "order.xsl";
  $args = array("column"=>"quantity", "order"=>"descending");
  $engine = xslt_create();
  $output = xslt_process($engine, $xmlfile, $xslfile, null, null, $args);
  print $output;
  xslt_free($engine);
  ?>
  这里需要注意一点,我们在代码中做了一点变动。在xsl stylesheet中,通过指定一些参数,我们可以改变一些区域,比如地址。这时我们要指定订单上的项目应该按数量递减方式排列。我们使用php的数组来存储名字对应我们的参数,然后通过xslt_process函数将名字传递给xsl引擎。
  
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons