How to specify an ordered list in XSD

前端 未结 1 1742
感情败类
感情败类 2021-01-24 20:36

I have a list of numbers that need to be ordered. Currently they are in the XML as such:

0.2
0.4
&l         


        
1条回答
  •  温柔的废话
    2021-01-24 21:05

    Elements in XML are inherently ordered per their order in the document. This order is significant (unlike that of attributes) and will be preserved by parsers.

    XML documents must have a single root element, so let's wrap your example elements in a single containing element:

    
      0.2
      0.4
      0.6
      
      1.8
    
    

    An XSD can be written for this XML:

    
    
      
        
          
            
            
            
            
            
          
        
      
    
    

    But an improvement is immediately obvious: Eliminate the indexing number from the component name:

    
      0.2
      0.4
      0.6
      
      1.8
    
    

    The XSD for this improved XML is similarly streamlined (and also can actually handle an indefinite number of value elements as well):

    
    
      
        
          
            
          
        
      
    
    

    Now one might ask whether the XSD can enforce a sorted order on the elements such that

    
      0.2
      0.4
      0.6
    
    

    would be valid, but

    
      0.6
      0.2
      0.4
    
    

    would be invalid.

    This is not possible to do in XSD 1.0, however XSD 1.1 can express such a constraint via assertions.

    Asserting sorted ordering in XSD 1.1

    
    
      
        
          
            
          
          
        
      
    
    

    Credit: The idea for the assertion test is based on one from Michael Kay.

    0 讨论(0)
提交回复
热议问题