0

There are 3 variants for an XML file. What's the difference between them and which should I use?

  1. <!ELEMENT Name (#PCDATA)>

  2. <!ELEMENT Name (First, Last)> <!ELEMENT First (#PCDATA)> <!ELEMENT Last (#PCDATA)>

  3. <!ELEMENT Name EMPTY> <!ATTLIST Name First CDATA #REQUIRED Last CDATA #REQUIRED >

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Vlad Vokkz
  • 13
  • 3

2 Answers2

1

The second and third cases amount to the distinction between XML attributes vs XML elements, which has already been answered extensively in the linked question.

Your first case introduces the possibility of not marking up a name into separate first and last names. You ask when you might want to do this. Generally, name data originates in a form where you have first and last name already determined separately. Preserving this distinction makes sense. However, there are some reasons you might want to use a single name field rather than separate first and last names:

  • Some people don't have separate first and last names.
  • Your data source might not provide separate first and last names, and parsing a single name into first and last parts can be very difficult.

These reasons generalize to non-name data. You will facilitate downstream processing if your markup is at a finer level of granularity, but it's not always easy to achieve that granularity. There's also a judgement call to be made regarding the likelihood of the need for the greater degree of markup by current and future consuming applications. More markup that's never needed just increases overhead to no purpose.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
0
  • #PCDATA represents a node's or an attribute's simple string content.
  • !ELEMENT is mapped to an XML Node:

    <!ELEMENT Name (#PCDATA)>:

    <Name>Tony Stark</Name>

  • !ATTLIST is mapped to an XML Node's attributes:

    <!ELEMENT Name EMPTY> <!ATTLIST Name First CDATA #REQUIRED Last CDATA #REQUIRED >:

    <Name First="Tony" Last="Stark"/>

The usage of these is entirely up to you and your use case.

Ron.B.I
  • 2,726
  • 1
  • 20
  • 27