Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Last active February 28, 2019 18:05
Show Gist options
  • Save ORESoftware/c917af15b1e5bce9617e1ebcec780554 to your computer and use it in GitHub Desktop.
Save ORESoftware/c917af15b1e5bce9617e1ebcec780554 to your computer and use it in GitHub Desktop.
Standard way to tell XML parsers that child is a singleton as opposed to a list

Say we have this XML

<bookstore>
  <book>art of war</book>
</bookstore

Most bookstores have many books, so we'd probably be right to assume that books is a list. However, my question is, does XML have a standard way to declare that a child should be a singleton? Something like this:

<bookstore   single="book">  
  <book>art of war</book>
</bookstore

The "single" attribute would tell the parser, that there is only one instance of book. If there is more than one book, it would throw a parsing error.

On the flipside, perhaps there is an XML standard to tell parsers that the child is a list? Something like this:

<bookstore    list="book">
  <book>art of war</book>
  <book>moby dick</book>
</bookstore

Then parsers could safely assume that there will be many books in the bookstore.

@ALCarden
Copy link

There is a standard, it's just in the javascript world everyone seems to forget things called schemas. Proper XML tools in most languages when doing parsing, code gen etc will use and respect a schema.

The schema can tell you if a child is a list via the schema properties, minOccurs, maxOccurs, mustOccurs, e.g. if minOccurs and maxOccurs = 1 then its a single element not a list, if it had minOccurs=0 and no maxOccurs then it would be optional list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment