Use xmllint command to process xml

Source: Internet
Author: User
In the preceding example, the attribution of the IP address queried on 123cha is obtained by extracting the result (ul # csstb) and obtaining only the content of the text part. The execution result of the preceding script statement is as follows:

Curl https://www.php1.cn/ip /? Q = 8.8.8.8 2>/dev/null | xmllint -- html -- xpath "// ul [@ id = 'csb']"-2>/dev/null | sed-e' s/<[^>] *> // g'
In the preceding example, the attribution of the IP address queried on 123cha is obtained by extracting the result (ul # csstb) and obtaining only the content of the text part. The execution result of the preceding script statement is as follows:


[Your query]: 8.8.8.8
Main site data:
USA
Auxiliary data on this site: Google Public DNS provides: hypo
Google Public DNS is provided free of charge by Google USA: zwstar reference data 1: USA
Reference Data 2: United States
Next, 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.
Assume that the content of xml (person. xml) is as follows:


Ball 30Male
After the following operations are performed, the output is in a more readable xml format:

#xmllint --format person.xml    
     
       
  
   ball
        30      
  
   male
      
 

2. -- noblanks

In contrast to -- format, sometimes we want to remove the blank content in xml to save the transmission volume. in this case, we can use the -- noblanks command.
Assume that the content of xml (person. xml) is as follows:

     
       
  
   ball
        30      
  
   male
      
 

After the parameter operation is executed, the output result is:

#xmllint --noblanks person.xml    
     
 
  
   ball
  30
  
   male
  
 

3. -- schema

Use scheam to verify the correctness of the xml file (XML Schema is an XML-based DTD substitution)
Assume that there are xml files (person. xml) and scheam files (person. xsd). The content is as follows:

Person. xml

     
       
  
   ball
        30      
  
   male
      
 

Person. xsd

     
       
        
        
          
             
                
                 
               
            
         
        
          
             
                
                 
                 
               
            
         
      
 

The result is as follows:

#xmllint --schema person.xsd person.xml    
     
       
  
   ball
        30      
  
   male
      
 

Person. xml validates
Note: By default, the content of the file to be verified 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
Next, we modify the person. xml file so that the age field and sex of this file do not comply with the xsd definition.

#xmllint --noout --schema person.xsd person.xmlperson.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

An error is reported when xmllint is successful!

4. about -- schema output

Let's take a look at the following scenario before talking about the output. if you want to execute xmllint in php and get the returned result, your code is usually like valid. php

 

We keep the above person. xml errors.
Run this code and you will find that the output you get is not an error, but array (0) {}, amazing!
Why?

Because xmllint -- schema, if an error is verified, the error message is not displayed by stdout, but by stderr.
The output parameter of exec can only be displayed in standard output (stdout.
Therefore, in order to get the error information, 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 ";
Run valid. php again to get the error message!

Example

First, create an xml document named po. xml with the following content:

  
      
           
    
     Alice Smith
            
    
     123 Maple Street
            
    
     Mill Valley
            
    
     CA
            
    
     90952
        
       
           
    
     Robert Smith
            
    
     8 Oak Avenue
            
    
     Old Town
            
    
     PA
            
    
     95819
        
       
   
    Hurry, my lawn is going wild!
       
           
                
     
      Lawnmower
                 
     
      1
                 
     
      148.95
                 
     
      Confirm this is electric
             
            
                
     
      Baby Monitor
                 
     
      1
                 
     
      39.98
                 
     
      1999-05-21
             
        
   
  

The schema file written for po. xml is named po. xsd. the content is as follows:

   
     
    
        Purchase order schema for Example.com.   Copyright 2000 Example.com. All rights reserved.  
     
    
    
    
     
       
        
        
        
       
      
     
    
     
       
        
        
        
        
       
      
    www.111cn.net 
    
     
       
         
           
        
         
         
          
           
            
           
          
         
         
         
         
            
           
         
       
     
    
    
     
       
       
     
   
  

Use xmllint to verify the po. xml file:

$ Xmllint-schema po. xsd po. xml if no error message is displayed, the verification is successful.

The above code uses the xmllint command to process detailed xml content. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.