Use a parent XML element as a container for reoccurring child elements?

前端 未结 4 1064
天命终不由人
天命终不由人 2021-01-02 09:55

Let\'s say you want to allow some particular XML element to occur 0+ times. For example, the element can occur multiple times:

&         


        
4条回答
  •  忘掉有多难
    2021-01-02 10:13

    Having a parent element makes it simpler to identify sections of the XML and makes it simpler for handlers to map the child elements to a collection. If you have a system that forbids the use of attributes (some do, it's annoying), you also need to use wrapper elements to distinguish properties (such as ids) that belong to particular children.

    Other than that it is valid to omit the parent element and can make the file markedly less verbose.

    In your first example, a user element could be mingled in with the records, this is valid but it might be hard to spot:

    
      Record 1
      Record 2
      Bill
      Record 3
    
    

    Whereas with a surrounding element you can separate the collection, and have multiple instances of that collection:

    
      
        Bill
      
      
        id1
        
          recordSet1
          Record 1
          Record 2
          Record 3
        
        
          recordSet2
          Record 1
          Record 2
          Record 3
        
      
      
        id2
        
          Record 1
          Record 2
          Record 3
        
      
    
    

提交回复
热议问题