Why use an empty element in XML?

前端 未结 3 2271
[愿得一人]
[愿得一人] 2021-02-20 15:36

Rank amateur here. I get the instructions on HOW to use an empty element in XML. But why would you? What would it be used for, other than (I guess) to add a blank record when th

3条回答
  •  孤街浪徒
    2021-02-20 15:47

    I think you are using "empty" elements to mean "elements with no children of any kind." Self-closing elements -- ones which don't have a separate closing tag -- may still have attributes and aren't empty in that sense.

    (X)HTML itself defines "empty" elements, such as
    . Again, even
    may have class and other attributes.

    To the question of how they might be used in your own schemas, you can use empty XML elements as "markers" (borrowing from the way that .NET uses the term "marker interface" for an interface with no members). This is useful when you want to add a "boolean" attribute to an element (like HTML allows valueless attributes such as checked). In such cases, using an attribute would force you to add noise of some kind (in the form of a meaningless or empty value).

    I use it this way, for example, to mark speeches as prose in Shakespeare plays:

    
        Archidamus
        
        If you shall chance, Camillo, to visit Bohemia, on
        the like occasion whereon my services are now on
        foot, you shall see, as I have said, great
        difference betwixt our Bohemia and your Sicilia.
    
    

    That said, it is kind of a kludge to work around what can be seen as a defect of XML, viz, the inability to represent non-text primitives.

提交回复
热议问题