Example of xmllint command processing xml and html

Source: Internet
Author: User
Xmllint is a convenient tool for processing and verifying xml and html. in linux, you only need to install libxml2, first, let's take a look at the example of processing html with -- html and -- xpath parameters. example :... xmllint is a convenient tool for processing and verifying xml and html. in linux, you only need to install libxml2, first, let's take a look at the example of processing html with -- html and -- xpath parameters.

Example:Curl http://www.phprm.com/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 data of this site: auxiliary data of this site in the United States: Google Public DNS: hypo US Google free Google Public DNS: zwstar reference data 1: US reference data 2: US

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 is an xml file (person. xml) and scheam files (person. xsd) file, 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 verified file 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.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  

An error is reported when xmllint is successful.

4. about -- schema output

Before giving output, let's look at the following scenario first. if you want to execute xmllint in php and get the returned result, your code will usually look like valid. php. the instance code is as follows:

$ Command = "xmllint -- noout -- schema person. xsd person. xml "; exec ($ command, $ output, $ retval); // when an error occurs, the returned value is not 0 if ($ retval! = 0) {var_dump ($ output);} // open source code phprm.com else {echo "yeah! ";}

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 the content displayed in the 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.   
     
    
    
    
     
       
        
        
        
       
      
     
    
     
       
        
        
        
        
       
      
     
    
     
       
         
           
       
        
        
         
          
           
          
         
        
        
        
        
            
           
         
       
     
    
    
     
       
       
     
   
  

Use xmllint to verify the po. xml file:

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

Tutorial URL:

You are welcome to add your _ favorites to the Favorites folder, but please keep the link for this article.

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.