Recursion in an XML schema?

后端 未结 3 1871
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 02:28

I need to create an XML schema that validates a tree structure of an XML document. I don\'t know exactly the occurrences or depth level of the tree.

XML example:

3条回答
  •  执笔经年
    2020-11-29 03:08

    The other solutions work great for making root elements recursive. However, in order to make a non-root element recursive without turning it into a valid root element in the process, a slightly different approach is needed.

    Let's say you want to define an XML message format for exchanging structured data between nodes in a distributed application. It contains the following elements:

    • - the root element;
    • - the message's origin;
    • - the message's destination;
    • - the data structure type encoded in the message;
    • - the data contained in the message.

    In order to support complex data types, is a recursive element. This makes possible to write messages as below, for sending e.g. a geometry_msgs/TwistStamped message to a flying drone specifying its linear and angular (i.e. rotating) speeds:

    
    
    
      controller:8080
      drone:8080
      geometry_msgs/TwistStamped
      
        0
        
          1
          0
        
        base_link
      
      
        
          1.0
          0
          1.0
        
        
          0.3
          0
          0
        
      
    
    

    We can easily write an XML schema to validate this format:

    
    
    
      
        
          
            
          
          
        
      
    
      
        
          
            
            
            
            
          
        
      
    
    

    The problem with the schema above is that it makes a root element, which means it also validates the document below:

    
    
    
      
        0
        
          1
          0
        
        base_link
      
      
        
          1.0
          0
          1.0
        
        
          0.3
          0
          0
        
      
    
    

    In order to avoid this side-effect, instead of defining the element directly at the global level, we first define a data type, then define a data element of that type inside message:

    
    
    
      
        
          
        
        
      
    
      
        
          
            
            
            
            
          
        
      
    
    

    Notice that we end up having to define the element twice — once inside the data type, and again inside — but apart a little work duplication this is of no consequence.

提交回复
热议问题