Why use an empty element in XML?

前端 未结 3 2248
[愿得一人]
[愿得一人] 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 <br/>. Again, even <br/> 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:

    <speech>
        <speaker role="Archidamus">Archidamus</speaker>
        <prose/>
        <line a="If_you">If you shall chance, Camillo, to visit Bohemia, on</line>
        <line a="the_like">the like occasion whereon my services are now on</line>
        <line a="foot_you">foot, you shall see, as I have said, great</line>
        <line a="difference_betwixt">difference betwixt our Bohemia and your Sicilia.</line>
    </speech>
    

    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.

    0 讨论(0)
  • 2021-02-20 16:00

    If a schema requires that a tag be present in a certain order, you may need to include that tag even if it has an empty value. For example, the following fragment from a schema indicates you need tag foo before tag bar:

    <xs:element name="parent">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="foo" minOccurs="1" maxOccurs="1" />
          <xs:element name="bar" minOccurs="1" maxOccurs="1" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    

    so the following fragment would be valid:

    <parent>
      <foo />
      <bar>asdf!</bar>
    </parent>
    

    But the following would not:

    <parent>
      <bar>asdf!</bar>
    </parent>
    

    Additionally, you can use an empty tag to represent an empty value (as opposed to a null value). However, if you're trying to represent a null value in XML, you should really use an xsi:nil attribute

    0 讨论(0)
  • 2021-02-20 16:07

    From my experience, Ive come across many of these formats when using APIs. Sometimes, the API creator has variables that are empty, thus resulting in these empty XML tags. For example, let's say you are creating a website that gathers hotel availability for different hotels. Company A ( some hotel company ) provides you with access to their API. You send a request and they provide you with the following:

    <response>
      <date>2008-01-10</date>
      <available>TRUE</available>
    </response>
    

    But Sometimes you may get:

    <response>
      <date>2008-01-10</date>
      <available>FALSE</available>
    </response>
    

    Or sometimes you may get:

    <response>
      <date>2008-01-10</date>
      <available></available>
    </response>
    

    As a developer, you have to create code that is ready for any type of response since we dont know exactly how the data is sent back from the API creators, which is why this is mentioned in tutorials. Hope this helps.

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