This example describes the way PHP uses the xmllint command to process XML and HTML. Share to everyone for your reference. The specific analysis is as follows:
Xmllint is a very convenient tool for processing and validating XML and processing HTML, so you can use this command as long as you install LIBXML2 under Linux. First look at an example of how HTML is processed with--html 、--XPath parameters:
Examples are as follows:
Copy Code code as follows:
Curl http://www.jb51.net/ip/?q=8.8.8.8 2>/dev/null | Xmllint--html--xpath "//ul[@id = ' csstb ']"-2>/dev/null | Sed-e ' s/<[^>]*>//g '
In the example above, the content of the text part is only obtained by extracting the result (UL#CSSTB) after the attribution of the IP address queried on 123cha. The results of the above script statement are as follows:
[Your query]:8.8.8.8
Main data of this site:
United States
This station auxiliary data: Google Public DNS provides: Hypo
Google free Google public DNS provides: Zwstar reference data One: the United States
Reference Data two: the United States
Let's take a look at the use of the other main parameters with the example below.
1,--format
This parameter is used to format the XML so that it is well readable.
Suppose the XML (person.xml) content is as follows:
Copy Code code as follows:
<person><name>ball</name><age>30</age<sex>male</sex></person>
This is done as an XML format that is easier to read after doing the following:
Copy Code code as follows:
#xmllint--format Person.xml
<?xml version= "1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
2,--noblanks
In contrast to--format, sometimes we want to get rid of whitespace in XML in order to save the amount of traffic, when we can use the--noblanks command.
Suppose the XML (person.xml) content is as follows
Copy Code code as follows:
<?xml version= "1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
When the parameter operation is performed, its output is:
Copy Code code as follows:
#xmllint--noblanks Person.xml
<?xml version= "1.0"?>
<person><name>ball</name><age>30</age><sex>male</sex></person>
3 、--Schema
Verifying the correctness of an XML file using Scheam (XML Schema is an xml-based DTD substitute)
Suppose you have an XML file (person.xml) and a Scheam file (person.xsd) file with the following contents
Person.xml
Copy Code code as follows:
<?xml version= "1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
Person.xsd
Copy Code code as follows:
<?xml version= "1.0"?>
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema" >
<xs:element name= "name" type= "xs:string"/>
<xs:element name= "Age" type= "Xs:integer"/>
<xs:element name= "Sex" >
<xs:simpleType>
<xs:restriction base= "Xs:string" >
<xs:enumeration value= "Male"/>
<xs:enumeration value= "female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name= "Person" >
<xs:complexType>
<xs:all>
<xs:element ref= "Name"/>
<xs:element ref= "Age"/>
<xs:element ref= "Sex"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
The results of the following command are:
Copy Code code as follows:
#xmllint--schema person.xsd Person.xml
<?xml version= "1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
Person.xml validates
Note: By default, the validated file content is output after validation, and you can use the--noout option to remove this output so that we can only get the final validation results.
Copy Code code as follows:
#xmllint--noout--schema person.xsd person.xml
Person.xml validates
Let's change the person.xml so that both the age field and the sex are incompatible with the XSD definition.
Copy Code code as follows:
#xmllint--noout--schema person.xsd person.xml
Person.xml:4: element age:schemas validity error:element ' age ': ' Isn't age ' isn't a valid value of the atomic type ' xs:i Nteger '.
Person.xml:5: element sex:schemas validity error:element ' sex ': [facet ' enumeration '] The value ' test ' is not a elemen T of the set {' Male ', ' female '}.
Person.xml:5: element sex:schemas validity error:element ' sex ': ' Test ' isn't ' a valid value of the local atomic type.
Person.xml fails to validate
Can see Xmllint success of the newspaper made a mistake!
4, about the output of--schema
Look at the following scenario before you say the output, and if you want to execute xmllint through PHP and get the return results, your code should usually look like this valid.php
Copy Code code as follows:
<?php
$command = "Xmllint--noout--schema person.xsd person.xml";
EXEC ($command, $output, $retval);
The return value is not 0 when an error occurs
if ($retval!= 0) {
Var_dump ($output);
}
else{
echo "yeah!";
}
We keep the mistakes person.xml above.
Executing this code, you will find that the output you get is not an error, but array (0) {}, amazing!
Why is that?
Because of the Xmllint--schema, if an error is validated, the error message is not displayed through standard output (stdout), but by a standard error (STDERR).
Exec's output parameters can only be displayed in standard output (stdout).
So, in order to get the error message, we need to redirect the standard error to the standard output, corresponding to the modified code:
Copy Code code as follows:
$command = "Xmllint--noout--schema person.xsd person.xml 2>$1";
Execute valid.php again, the error message smoothly get!
Examples are as follows:
First, create an XML document named Po.xml, which reads as follows:
Copy Code code as follows:
<?xml version= "1.0"?>
<purchaseorder orderdate= "1999-10-20" >
<shipto country= "US" >
<name>alice smith</name>
<street>123 Maple street</street>
<city>mill valley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
<billto country= "US" >
<name>robert smith</name>
<street>8 Oak avenue</street>
<city>old town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<comment>hurry, my lawn is going wild!</comment>
<items>
<item partnum= "872-aa" >
<productName>Lawnmower</productName>
<quantity>1</quantity>
<USPrice>148.95</USPrice>
<comment>confirm this is electric</comment>
</item>
<item partnum= "926-aa" >
<productname>baby monitor</productname>
<quantity>1</quantity>
<USPrice>39.98</USPrice>
<shipDate>1999-05-21</shipDate>
</item>
</items>
</purchaseOrder>
Then the schema file written for Po.xml, named PO.xsd, reads as follows:
Copy Code code as follows:
<xsd:schema xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" >
<xsd:annotation>
<xsd:documentation xml:lang= "en" >
Purchase Order schema for example.com.
Copyright example.com. All rights reserved.
</xsd:documentation>
</xsd:annotation>
<xsd:element name= "PurchaseOrder" type= "Purchaseordertype"/>
<xsd:element name= "comment" type= "xsd:string"/>
<xsd:complextype name= "Purchaseordertype" >
<xsd:sequence>
<xsd:element name= "ShipTo" type= "USAddress"/>
<xsd:element name= "BillTo" type= "USAddress"/>
<xsd:element ref= "comment" minoccurs= "0"/>
<xsd:element name= "Items" type= "items"/>
</xsd:sequence>
<xsd:attribute name= "OrderDate" type= "Xsd:date"/>
</xsd:complexType>
<xsd:complextype name= "USAddress" >
<xsd:sequence>
<xsd:element name= "name" type= "xsd:string"/>
<xsd:element name= "Street" type= "xsd:string"/>
<xsd:element name= "City" type= "xsd:string"/>
<xsd:element name= "state" type= "xsd:string"/>
<xsd:element name= "Zip" type= "Xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name= "Country" type= "Xsd:nmtoken" fixed= "US"/>www.jb51.net
</xsd:complexType>
<xsd:complextype name= "Items" >
<xsd:sequence>
<xsd:element name= "item" minoccurs= "0" maxoccurs= "unbounded" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name= "ProductName" type= "xsd:string"/>
<xsd:element name= "Quantity" >
<xsd:simpleType>
<xsd:restriction base= "Xsd:positiveinteger" >
<xsd:maxexclusive value= "/>"
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name= "Usprice" type= "Xsd:decimal"/>
<xsd:element ref= "comment" minoccurs= "0"/>
<xsd:element name= "ShipDate" type= "xsd:date" minoccurs= "0"/>
</xsd:sequence>
<xsd:attribute name= "Partnum" type= "SKU" use= "required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<!--keeping unit, a code for identifying products-->
<xsd:simpletype name= "SKU" >
<xsd:restriction base= "Xsd:string" >
<xsd:pattern value= "d{3}-[a-z]{2}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
To validate a po.xml file using Xmllint:
Copy Code code as follows:
$ Xmllint-schema po.xsd Po.xml
If there is no error message, it means that the checksum passed.
I hope this article will help you with your PHP program design.