Hướng dẫn dùng xsd validator trong PHP

It’s 2017 and you still prefer using XML[Extensible Markup Language] to JSON[JavaScript Object Notation] for data-interchange? Well, that is not the point about this article. Everything you want to say about the trade-offs of either of them has been dealt with here. But there are still a lot of systems using XML today and I can assure you that it would still be the case X years from now.
Validating XML against XSD might be the first step to take especially when building a feed Reader/Ingester. For Starters, any file like the sample below is a well-formed XML file.



  2002-05-30T09:30:10.5
   
      Gambardella, Matthew
      XML Developer's Guide
      Computer
      44.95
      2000-10-01
      An in-depth look at creating applications 
      with XML.
   
   
      Ralls, Kim
      Midnight Rain
      Fantasy
      5.95
      2000-12-16
      A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.
   

and below is a sample XSD [XML Schema Definition] file:


  
    
      
        
        
          
            
              
              
              
              
              
              
            
            
          
        
      
    
  

Writing a XSD for your XML is actually easy to do, you make use of tools like freeformatter or do a crash course on w3Schools.
Now, we are ready to validate our XML file against the XSD using either DOMDocument or XMLReader. First, make sure that these extensions are enabled on PHP installation.

Validating With DOMDocument

Chủ Đề