Use xmllint command to process xml

PHPz
Release: 2017-04-02 11:10:56
Original
4371 people have browsed it

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/<[^>]*>//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:


ball30male
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>
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

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 &#39;age&#39;: &#39;not age&#39; is not a valid value of the atomic type &#39;xs:integer&#39;.
person.xml:5: element sex: Schemas validity error : Element &#39;sex&#39;: [facet &#39;enumeration&#39;] The value &#39;test&#39; is not an element of the set {&#39;male&#39;, &#39;female&#39;}.
person.xml:5: element sex: Schemas validity error : Element &#39;sex&#39;: &#39;test&#39; is not a valid value of the local atomic type.
person.xml fails to validate
Copy after login

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!";
    }
Copy after login

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>
Copy after login

Then 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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!