【建立xml】
$dom=new DOMDocument("1.0");
$book=$dom->appendChild($dom->createElement('book'));//建立根項目
$title=$book->appendChild($dom->createElement('title'));//在根項目下建立節點
$title->appendChild($dom->createTextNode('php cookbook'));//在節點下建立子節點
$title->setAttribute("cover","soft");//title節點下建立屬性
$sklar=$book->appendChild($dom->createElement('author'));
//$sklar->appendChild($dom->createTextNode('sklar'));
$name=$sklar->appendChild($dom->createElement('name'));
$name->appendChild($dom->createTextNode('fsl'));
$trac=$book->appendChild($dom->createElement('author'));
$trac->setAttribute("cover","feng");
$trac->setAttribute("id","123");
$trac->appendChild($dom->createTextNode('trac'));
$dom->formatOutput=true;
echo $dom->saveXML();
$dom->save('test.xml');
$dom->saveHTMLFile('1.html');
1.htm
<?xml version="1.0"?>
<book>
<title cover="soft">php cookbook</title>
<author>
<name>fsl</name>
</author>
<author cover="feng" id="123">trac</author>
</book>
【xml檔案處理方法-xpath】
$s=simplexml_load_file("12-1.xml");
$ids=$s->xpath("/people/person/@id");//處理屬性
foreach ($ids as $id)
{
echo $id."<br>";
}
$pname=$s->xpath("/people/person/name");//處理節點
foreach ($pname as $name)
{
echo $name."<br>";
}
$per=$s->xpath("/people/person");
foreach ($per as $person){
list($age)=$person->xpath("age");
list($city)=$person->xpath("address/city");
list($pro)=$person->xpath("address/province");//處理含有子節點的節點
echo $city."_".$pro.":".$age."<br>";
}
$dom=new DOMDocument;
$dom->load("12-1.xml");
$xpath=new DOMXPath($dom);
$did=$xpath->query("/people/person/@id");
foreach ($did as $id)
{
echo $id->nodeValue."<br>";
}
$dname=$xpath->query("/people/person/age");
foreach ($dname as $name)
{
echo $name->nodeValue."<br>";
}
$dper=$xpath->query("/people/person");
foreach ($dper as $persion)
{
$fn=$xpath->query('name',$persion);
echo $fn->item(0)->firstChild->nodeValue." : ";
$fn2=$xpath->query('age',$persion);
echo $fn2->item(0)->firstChild->nodeValue."<br>";
echo $persion->nodeValue."<br>";
}
12-1.xml
<?xml version="1.0"?>
<?xml-stylesheet href="7-2.xsl" type="text/xsl"?>
<people>
<person id="001">
<name>張俊</name>
<age>20</age>
<address>
<province>河北</province>
<city>石家莊</city>
<street>人民東路</street>
<house_number>13#</house_number>
</address>
</person>
<person id="002">
<name>劉軍</name>
<age>21</age>
<address>
<province>吉林</province>
<city>長春</city>
<street>解放南路</street>
<house_number>25#</house_number>
</address>
</person>
<person id="003">
<name>周泰</name>
<age>26</age>
<address>
<province>廣東</province>
<city>深圳</city>
<street>深南路</street>
<house_number>37#</house_number>
</address>
</person>
<person id="004">
<name>張群</name>
<age>20</age>
<address>
<province>廣西</province>
<city>南寧</city>
<street>江南大道</street>
<house_number>67#</house_number>
</address>
</person>
</people>
7-2.xsl
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="people/person/name"/>
</xsl:template>
</xsl:stylesheet>
【應用xslt】
必須開啟php擴充php_xsl
$xsl=new DOMDocument;
$xsl->load('7-2.xsl');
$xslt=new XSLTProcessor();
$xslt->importStylesheet($xsl);
$xml=new DOMDocument;
$xml->load('12-1.xml');//雙參數問題
$results=$xslt->transformToXml($xml);
$results2=$xslt->transformToURI($xml,'results.html');
$results3=$xslt->transformToDoc($xml);