[Create XML]
$ Dom = new domdocument ("1.0 ");
$ Book = $ dom-> appendchild ($ dom-> createelement ('book'); // create a root element
$ Title = $ book-> appendchild ($ dom-> createelement ('title'); // create a node under the root element
$ Title-> appendchild ($ dom-> createtextnode ('php cookbook'); // create a subnode under the node
$ Title-> setattribute ("cover", "soft"); // create an attribute under the title Node
$ 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 file processing method-XPath]
$ S = simplexml_load_file ("12-1.xml ");
$ IDs = $ S-> XPath ("/people/person/@ ID"); // process attributes
Foreach ($ IDS as $ id)
{
Echo $ id. "<br> ";
}
$ Pname = $ S-> XPath ("/people/person/Name"); // process nodes
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"); // process nodes with subnodes
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> Zhang Jun </Name>
<Age> 20 </age>
<Address>
<Province> Hebei </province>
<City> Shijiazhuang </city>
<Street> Renmin East Road </street>
<House_number> 13 # </Address>
</Person>
<Person id = "002">
<Name> LIU Jun </Name>
<Age> 21 </age>
<Address>
<Province> Jilin </province>
<City> Changchun </city>
<Street> Jiefang South Road </street>
<House_number> 25 # </Address>
</Person>
<Person id = "003">
<Name> Zhou Tai </Name>
<Age> 26 </age>
<Address>
<Province> Guangdong </province>
<City> Shenzhen </city>
<Street> ShenNan road </street>
<House_number> 37 # </Address>
</Person>
<Person id = "004">
<Name> Zhang Qun </Name>
<Age> 20 </age>
<Address>
<Province> Guangxi </province>
<City> Nanning </city>
<Street> Jiangnan Avenue </street>
<House_number> 67 # </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>
[Apply XSLT]
PHP extension php_xsl must be enabled
$ XSL = new domdocument;
$ XSL-> load ('7-2. XSL ');
$ XSLT = new xsltprocessor ();
$ XSLT-> importstylesheet ($ XSL );
$ Xml = new domdocument;
$ XML-> load ('12-1. xml'); // double Parameter Problem
$ Results = $ XSLT-> transformtoxml ($ XML );
$ Results2 = $ XSLT-> transformtouri('results.html ');
$ Results3 = $ XSLT-> transformtodoc ($ XML );