What is the difference between XML and XSD?

后端 未结 7 975
南笙
南笙 2020-11-27 11:31

What is the difference between Extensible Markup Language (XML) and XML Schema (XSD)?

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 11:59


    Take an example

    
      
         Y
         12
      
    
    

    and design an xsd for that:

    
      
        
          
            
              
                
                  
                  
                
              
            
          
        
      
    
    


    What isn't possible with XSD: would like to write it first as the list is very small
    1) You can't validate a node/attribute using the value of another node/attribute.
    2) This is a restriction : An element defined in XSD file must be defined with only one datatype. [in the above example, for appearing in another node, datatype cannot be defined other than int.
    3) You can't ignore the validation of elements and attributes, ie, if an element/attribute appears in XML, it must be well-defined in the corresponding XSD. Though usage of allows it, but it has got its own rules. Abiding which leads to the validation error. I had tried for a similar approach, and certainly wasn't successful, here is the Q&A


    what are possible with XSD:
    1) You can test the proper hierarchy of the XML nodes. [xsd defines which child should come under which parent, etc, abiding which will be counted as error, in above example, child_two cannot be the immediate child of root, but it is the child of "parent" tag which is in-turn a child of "root" node, there is a hierarchy..]
    2) You can define Data type of the values of the nodes. [in above example child_two cannot have any-other data than number]
    3) You can also define custom data_types, [example, for node , the possible data can be one of the 12 months.. so you need to define all the 12 months in a new data type writing all the 12 month names as enumeration values .. validation shows error if the input XML contains any-other value than these 12 values .. ]
    4) You can put the restriction on the occurrence of the elements, using minOccurs and maxOccurs, the default values are 1 and 1.

    .. and many more ...

提交回复
热议问题