Home > Article > Backend Development > Use xmllint command to process xml
Example
curl http://www.php.cn /ip/?q=8.8.8.8 2>/dev/null | xmllint --html --xpath " //ul[@id='csstb']" - 2>/dev/null | sed -e 's/549a3fd9a3c62568d8b32cd8627105c3]*>//g'
In the above example, it is mainly through After querying the ownership of the IP address on 123cha, only the text part is obtained by extracting the result (ul#csstb). The results after executing the above script statement are as follows:
[Your query]:8.8.8.8
Primary data of this site:
United States
Secondary data of this site: Google Public DNS provider: hypo
United States Google Free Google Public DNS provider: zwstar Reference data one: United States
Reference data two: United States
Let’s take a look at the usage of other main parameters with examples.
1. --format
This parameter is used to format xml to make it readable.
Suppose there is xml (person.xml) with the following content:
0833d296851a1e081f2175a4cbfa71358a11bc632ea32a57b3e3693c7987c420balldf406f776eecbaf16b62325323196f14ff4fd28be6111b38109cb452b13c2daa30bcfbf0f15553394c7c6f337b58908853maled575e2c07f4556bb151a80c4adfa44ffcb884f44f2f1937276917c04c3676183
After performing the following operations, the output will be a more readable xml format:
#xmllint --format person.xml <?xml version="1.0"?> <person> <name>ball</name> <age>30</age> <sex>male</sex> </person>
2, --noblanks
is the opposite of --format, sometimes in order to save Transmission volume, we want to remove the blanks in xml, then we can use the --noblanks command.
Assume that the content of xml (person.xml) is as follows
<?xml version="1.0"?> <person> <name>ball</name> <age>30</age> <sex>male</sex> </person>
After executing this parameter operation, the output result is:
#xmllint --noblanks person.xml <?xml version="1.0"?> <person><name>ball</name><age>30</age><sex>male</sex></person>
3, --schema
Use scheam to verify the correctness of the xml file (XML Schema is an XML-based DTD replacement)
Assume that there are xml files (person.xml) and scheam files (person.xsd) files , the contents are as follows
person.xml
<?xml version="1.0"?> <person> <name>ball</name> <age>30</age> <sex>male</sex> </person>
person.xsd
<?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 result after executing the following command is:
#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 verified file content will be output after verification. You can use the --noout option to remove this output, so that we can only get the final verification result.
#xmllint --noout --schema person.xsd person.xml
person.xml validates
Now we change person.xml so that the age field of this file and Sex does not meet the definition of xsd.
#xmllint --noout --schema person.xsd person.xml person.xml:4: element age: Schemas validity error : Element 'age': 'not age' is not a valid value of the atomic type 'xs:integer'. person.xml:5: element sex: Schemas validity error : Element 'sex': [facet 'enumeration'] The value 'test' is not an element of the set {'male', 'female'}. person.xml:5: element sex: Schemas validity error : Element 'sex': 'test' is not a valid value of the local atomic type. person.xml fails to validate
You can see that xmllint successfully reported an error!
4. About the output of --schema
Before talking about the output, let’s look at the following scenario. If you want to execute xmllint through php and get the return result, your code is usually It should look like this valid.php
<?php $command = "xmllint --noout --schema person.xsd person.xml"; exec($command, $output, $retval); //出错时返回值不为0 if ($retval != 0){ var_dump($output); } else{ echo "yeah!"; }
We keep the error in person.xml above.
Execute this code, you will find that the output you get is not an error, but array(0) {}, amazing!
Why is this so?
Because of xmllint --schema, if an error occurs during verification, the error message is not displayed through the standard output (stdout), but through the standard error (stderr).
The output parameter of exec can only get the content displayed by the standard output (stdout).
So, in order to get the error message, we need to redirect the standard error to the standard output and modify the code accordingly:
$command = "xmllint --noout --schema person.xsd person .xml 2>$1";
Execute valid.php again and get the error message successfully!
Example
First create an xml document, named po.xml, with the following content:
<?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>
ee8c11313070fdbf42a8eb4b3e605a5aThen write the schema for po.xml The file is named po.xsd and the content is 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 2000 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.111cn.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="100"/> </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> <!-- Stock 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>
207a5cd0530fd3904973dfab79171a7f Use xmllint to verify the po.xml file:
$ xmllint -schema po.xsd po.xml If there is no error message, it means the verification has passed.
The above is the detailed content of Use xmllint command to process xml. For more information, please follow other related articles on the PHP Chinese website!