XSD: difference between Element and Attribute

后端 未结 4 1160
旧巷少年郎
旧巷少年郎 2020-12-24 10:04

I\'m new to XSD, and I\'m quite confused as to when to use attribute, and when to use element?

Why cant we specify minOccurs and maxOccurs in attribute?

Also

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 10:19

    An element is an XML element - a opening tag, some content, a closing tag - they are the building blocks of your XML document:

    someValue
    

    Here, "test" would be an element.

    Attributes is an additional info on a tag - it's an "add-on" or an extra info on an element, but can never exist alone:

    somevalue
    

    "id" is an attribute.

    You cannot have multiple attributes of the same name on a single tag --> minOccurs/maxOccurs makes no sense. You can define required (or not) for an attribute - anything else doesn't make sense.

    The elements are defined by their occurrence inside complex types - e.g. if you have a complex type with a inside - you are defining that all elements must be present and must the in this particular order:

    
              
          
          
       
    
    

    Inside an element of that type, the sub-elements "Element1" and "Element2" are required and must appear in this order - there's no need for "required" or not (like with attributes). Whether or not an element is required is defined by the use of minOccurs and maxOccurs; both are =1 by default, e.g. the element must occur, and can only occur once. By tweaking those settings, you can define an element to be optional (minOccurs=0), or allow it to show up several times (maxOccurs > 1).

    I'd strongly recommend you check out the W3Schools Tutorial on XML Schema and learn some more about XML schema.

    Marc

提交回复
热议问题