用户登录
用户注册

分享至

使用PHP和XSL stylesheets转换XML文档

  • 作者: 我的丁丁比我的名字还要长好多好多
  • 来源: 51数据库
  • 2021-11-22

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引擎。


本文作者brian schaffner是富士通咨询公司的副主任。他为富士通的技术咨询公司提供架构、设计和开发支持。

 

软件
前端设计
程序设计
Java相关