Archive for the '未分类' Category

12.12阅读RSS和Atom Feeds

12.12.1问题
You want to retrieve RSS and Atom feeds and look at the items. This allows you to incorporate newsfeeds from
multiple web sites into your application.
你想要找回RSS和Atom feeds和考虑在这个items. 它允许你合并newsfeeds从多种web sites到你的应用程序
12.12.2解答
Use the MagpieRSS parser. Here’s an example that reads the RSS feed for the php.announce mailing list:
使用这个MagpieRss 剖析器。这里是一个例子阅读Rss feed到这个php.annouce邮件发送清单

?php
require ‘rss_fetch.inc’;
$feed = ‘http://news.php.net/group.php?group=php.announce&format=rss’;
$rss = fetch_rss( $feed );
print “<ul>\n”;
foreach ($rss->items […]

12.11处理编码内容

12.11.1问题
PHP XML extensions use UTF-8, but your data is in a different content encoding.
PHP的XML延展名使用UTF-8。但是你的数据在一个不同的内容编码
12.11.2解答
Use the iconv library to convert it before passing it into an XML extension:
使用这个iconv library去转换它,到一个XML延展名

$utf_8 = iconv(’ISO-8859-1′, ‘UTF-8′, $iso_8859_1);

Then convert it back when you are finished:
然后转换它当你完成
12.11.3讨论
Character encoding is a major PHP 5 weakness. Fortunately, Unicode support is the major driver behind PHP […]

12.10确认XML文件

12.10.1问题
You want to make sure your XML document abides by a schema, such as XML Schema, RelaxNG, and DTDs.
你想要确认你的XML文件坚持一个计划。例如XML计划。RelaxNG.和DTDs
12.10.2解答
Use the DOM extension
使用这个DOM延展名
With existing DOM objects, call DOMDocument::schemaValidate( ) or DOMDocument::relaxNGValidate( ):
使用现有的DOM对象。调用DOMDocument::schemaVlidate()或者DOMDOcument::relaxNGValidate()

$file = ‘address-book.xml’;
$schema = ‘address-book.xsd’;
$ab = new DOMDocument
$ab->load($file);
if ($ab->schemaValidate($schema)) {
    print “$file is valid.\n”;
} else {
    print “$file is invalid.\n”;
}

If your XML document specifies a […]

12.9从XSLT Stylesheets 调用PHP函数

12.9.1问题
You want to call PHP functions from within an XSLT stylesheet.
你想要调用PHP函数从一个XSLT stylesheet
12.9.2解答
Invoke the XSLTProcessor::registerPHPFunctions( ) method to enable this functionality
调用XSLTProcessor::registerPHPFunctions()方法到functionlity

$xslt = new XSLTProcessor();
$xslt->registerPHPFunctions();

And use the function( ) or functionString( ) function within your stylesheet:
然后使用这个function()或者functionString()函数和你的风格

<xsl:stylesheet version=”1.0″
    xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
    xmlns:php=”http://php.net/xsl”
    xsl:extension-element-prefixes=”php”>
<xsl:template match=”/”>
    <xsl:value-of select=”php:function(’strftime’, ‘%c’)” />
</xsl:template>
</xsl:stylesheet>

12.9.3讨论
XSLT parameters are great when you need to communicate from PHP to XSLT. […]

12.8设置XSLT参数从PHP中

12.8.1问题
You want to set parameters in your XSLT stylesheet from PHP.
你想要设置你的XSLT参数从PHP中
12.8.2解答
Use the XSLTProcessor::setParameter( ) method:
使用这个XSLTProcessor::setParameter()方法

// This could also come from $_GET[’city’];
$city = ‘San Francisco’;
$dom  = new DOMDocument
$dom->load(’address-book.xml’);
$xsl  = new DOMDocument
$xsl->load(’stylesheet.xsl’);
$xslt = new XSLTProcessor();
$xslt->importStylesheet($xsl);
$xslt->setParameter(NULL, ‘city’, $city);
print $xslt->transformToXML($dom);

This code sets the XSLT city parameter to the value stored in the PHP variable $city.
这个代码设置XSLT参数到这个存储数值在PHP的变量$city
12.8.3讨论
You can pass data from […]

12.7转换XML为XSLT

12.7.1问题
You have an XML document and an XSL stylesheet. You want to transform the document using XSLT and capture the
results. This lets you apply stylesheets to your data and create different versions of your content for
different media.
你有一个XML文件和一个XSL stylesheet, 你想要转换文件使用XSLT和capure这个结果。它将你应用你的数据和创造不同的
版本在不同的媒体
12.7.2解答
Use PHP’s XSLT extension
使用PHP的XSLT延展名

// Load XSL template
$xsl = newDOMDocument;
$xsl->load(’stylesheet.xsl’);
// Create new XSLTProcessor
$xslt = new XSLTProcessor();
// Load stylesheet
$xslt->importStylesheet($xsl);
// […]

12.6提取信息使用XPath

12.6.1问题
You want to make sophisticated queries of your XML data without parsing the document node by node.
你想要制造一个查询在你的XML数据和分解这个文件网点
12.6.2解答
Use XPath.
使用XPath
XPath is available in SimpleXML:
XPath是简单的可利用的

?php
$s = simplexml_load_file(’address-book.xml’);
$emails = $s->xpath(’/address-book/person/email’);
foreach ($emails as $email) {
    // do something with $email
}

And in DOM:
然后在DOM

?php
$dom = new DOMDocument;
$dom->load(’address-book.xml’);
$xpath = new DOMXPath($dom);
$email = $xpath->query(’/address-book/person/email’);
foreach ($emails as $email) {
    // do something with $email
}

12.6.3讨论
Except for […]

12.5分解巨大的XML文件

12.5.1问题
You want to parse a large XML document. This document is so large that it’s impractical to use SimpleXML or DOM because you cannot hold the entire document in memory. Instead, you must load the document in one section at a time.
你想要分解一个巨大的XML文件。这个文件是非常大、不切实际的使用SimpleXML或者DOM.因为你不希望全部的文件在memory。代替。你必须装在文件在一个部分
12.5.2解答
Use the XMLReader extension:
使用这个XMLReader延长名

?php
$reader = new XMLReader();
$reader->open(’card-catalog.xml’);
/* Loop through document */
while ($reader->read()) {
    /* […]

12.4分析复杂的XML文件

12.4.1问题
You have a complex XML document, such as one where you need to introspect the document to determine its
schema, or you need to use more esoteric XML features, such as processing instructions or comments.
你有一个复杂的XML文件。例如在你需要的内部文件去测定它的计划。或者你需要使用很多深奥的XML性质。例如处理指示或者
注释
12.4.2解答
Use the DOM extension. It provides a complete interface to all aspects of the XML specification
使用这个DOM延展名。它规定一个完整的界面到所有XML规范

?php
$dom = new DOMDocument;
$dom->load(’address-book.xml’);
foreach ($dom->getElementsByTagname(’person’) as $person) […]

12.2在DOM产生XML

12.2.1问题
You want to generate XML but want to do it in an organized way instead of using print and loops.
你想要产生XML。但是应该在一个organized路线代替使用print 和loops
12.2.2解答
Use the DOM extension to create a DOMDocument object. After building up the document, call DOMDocument::save(
) or DOMDocument::saveXML( ) to generate a well-formed XML document
使用这个DOM的延长区创造一个DOMDocument object、然后树立这个文件、调用DOMDocument::save()或者
DOMDocument::saveXML()去产生一个健康成型的文件

?php
// create a new document
$dom = new DOMDocument(’1.0′);
// create the […]