PHP - XML - Transforming XML

Use PHP's DOM and XSLT extensions together:

  1. <?php
  2. // read XML data
  3. $xml = new DOMDocument;
  4. $xml->load('review.xml');
  5. // read XSL stylesheet data
  6. $xsl = new DOMDocument;
  7. $xsl->load('review.xsl');
  8. // initialize XSLT engine
  9. $xslp = new XSLTProcessor;
  10. // attach XSL stylesheet object
  11. $xslp->importStyleSheet($xsl);
  12. // perform transformation
  13. echo $xslp->transformToXML($xml);
  14. ?>

Comments

XSL, the Extensible Style Language, is a powerful language that makes it possible to apply presentation rules to XML documents, and convert—or transform—them from one format to another. For example, you could use different XSL transformations to create an HTML Web page, a WML deck, and an ASCII text file all from the same source XML.

An XSL Transformation essentially consists of converting an XML source tree into a new—and usually completely different—result tree. This is accomplished by means of an XSLT stylesheet, which contains one or more template rules. A template rule performs two functions: It first identifies a pattern to match in the source tree, and then describes the structure of the desired result tree. It is this process of transforming the source tree into the result tree that gives XSLT its name.

The previous listing demonstrates how this works in PHP. Here, two instances of the DOMDocument class are created, one for the XML data and the other for the XSLT style sheet. Next, the XSLT engine is initialized by creating an object of the XSLTProcessor class, and the object's importStyleSheet() method is used to import the XSLT style sheet. Once the style sheet has been successfully parsed, the transformToXML() method is used to apply the style rules to the XML document, "transform" it, and return the result.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值