Using XSL with XMLReader

Một phần của tài liệu Pro PHP XML and Web Services phần 5 pptx (Trang 79 - 82)

In the previous chapter, you read about XSL; it requires the use of the DOM extension. In the current chapter, you have seen how XMLReader can interface with the DOM extension. If you haven’t gotten the idea where I am going with this yet, let me make it a bit clearer. Using a combination of XMLReader, DOM, and XSL, it is possible to transform very large documents, which ordinarily wouldn’t be possible without running some real heavyweight hardware. For example:

<?php

/* Set up the XSLT processor */

$xslDoc = new DOMDocument();

$xslDoc->load("bigxml2.xsl");

$xsltProc = new XsltProcessor();

$xsltProc->importStylesheet($xslDoc);

$reader = new XMLReader();

$reader->open('bigxml2.xml');

/* Following two lines, position cursor on the document element node */

$reader->read();

$reader->read();

/* Move cursor to first child node of document element */

if ($reader->read()) {

/* Perform tests, and use next() method to traverse sibling nodes */

do {

if ($reader->nodeType == XMLREADER::ELEMENT) {

/* XSL output filenames will be based on element names */

$filename = $reader->localName.".xml";

$node = $reader->expand();

/* Add expanded node to a DOMDocument, and transform it */

$dom = new DOMDocument();

$dom->appendChild($node);

$xsltProc->transformToUri($dom, $filename);

unset($dom);

}

} while($reader->next());

}?>

This example is similar to the previous XMLReader and DOM example except that rather than creating an XInclude document, subtrees are being transformed using XSL. The file bigxml2.xslcould consist of any XSL templates you like. That is not provided in this example and is left up to you to implement if you choose to do so.

Conclusion

No single parser will solve every problem. Each has its own strengths and weaknesses, and it’s not always easy to determine which one to choose. This chapter covered the XML extensions available in PHP 5 and provided some insight into how to go about determining which one may be best suited for a particular problem at hand. It is not always easy to determine ahead of time the right balance between memory usage and execution speed. Many aspects affect these issues so the best you can do is plan as far enough ahead as possible, perhaps even mak- ing some assumptions and writing code based on those assumptions. You can try to write as efficient code as possible without being detrimental to the effectiveness of the application.

After reading this chapter and examining the results of the different tests, you should realize that, when possible, you should use XMLReader. It not only uses minimal resources but also provides excellent execution speed. Alternatively, it may be beneficial to see whether using a combination of the technologies would help you.

This chapter concludes the coverage of specific XML-based extensions included with PHP 5. This does not mean this is all that you can do with XML in PHP. The remaining chapters in this book will cover some of the other XML technologies and how you can use the extensions to leverage them. The next chapter will introduce you to XML security, including XML signa- tures and XML encryption. Currently, no extension provides this support directly, but you can use the extensions to write some supporting functionality for XML security.

XML Security

PHP 5 provides many mechanisms to deal with XML documents. Applications can take advantage of the tree-based parsers (DOM and SimpleXML) and the stream-based parsers (xml and XMLReader), as well as document transformations using XSL. Processing, although one of the primary tasks for an application, is just one aspect that you need to consider when writing XML-based applications. Security and authentication of the data are often important as well. This chapter will cover some standards and methods you can use to provide support for XML encryption and digital signatures in documents.

Note No built-in extension or package in PHP natively provides encryption and digital signatures.

Currently, it is possible to write code to perform limited XML security functions for simple documents using an encryption extension, such as OpenSSL, mcrypt, or the sha1()function, and using a tree-based parser, such as DOM. The examples within this chapter require that the DOM extension is available on the develop- ment machine. This chapter will focus more on how you can implement digital signatures and encryption using PHP rather than cover the complete specification. For this reason, I will use SHA1 and HMAC-SHA1 within the examples for digital signatures, because it is possible to use these algorithms without requiring additional extensions, and I will use mcrypt for the examples of XML encryption because of its flexibility.

Một phần của tài liệu Pro PHP XML and Web Services phần 5 pptx (Trang 79 - 82)

Tải bản đầy đủ (PDF)

(94 trang)