Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Hmm...somewhat disagree with the "correct" way to express a dictionary. I prefer:

   <root>
      <Name>John</Name>
      <City>London</City>
   </root>
Removes one level of indirection, XML already has keys.


This is not a dictionary, it’s a record. Unfortunately this fairly fundamental distinction has been thoroughly muddied by certain languages that want to use associative arrays for everything.


Hmm...you seem to be mapping programming language concepts 1:1 to XML. That is a mistake.


I'm not the one doing that. People using XML as something else than a document markup language are. Which is what the article author is complaining about. But if you really want to do that, then at least do it properly. Record fields have predefined names, just like XML elements have predefined names. Dictionary keys can be arbitrary, like XML text nodes or attribute values but emphatically not element or attribute names, unless your "XML" is actually just tag soup.


That's not a good way to express a dictionary because it does not allow arbitrary strings as key names.

It's also not a good example of XML, because XML schemas should have a fixed list of tag names.


But the starting point in the example was

    <item name="name" value="John" />  
    <item name="city" value="London" />  
where the key names are used as attributes, so it wouldn't work with arbitrary key names either, right?


It would work (kind of); most XML parsers/generators would take care of escaping and unescaping quotes; but there's no way in the XML spec to escape characters in tag names.


XML allows arbitrary strings as key names just fine.


No, you can't have quotes, spaces, etc., because with GGP's scheme it would be a tag name, and these are invalid in tag names.


The main issue I see with that is that if it's a true dictionary, then those elements will constantly be different, which is weird.

Now, if we're just encoding a dictionary that's an already an encoding of an object, then yeah, let's just encode the object directly like you are above.


Which works if you either expect a dictionary with a predefined set of fields (which isn't really a dictionary then), or parse your xml in a way that handles arbitrary tags. For a generic dictionary the shown approach is still the way to go, if you really want to use XML for that.


XML parsers generally allow arbitrary tags.


That is definitely the best to use with xpath


you've just invented plist, wonder if it can be validated by a schema..

  <dict> 
        <key>CFBundlePackageType</key>
        <string>FMWK</string>
        <key>CFBundleShortVersionString</key>
        <string>4.8</string>
...


No, GP didn't. They use tag names as keys.

But you answer your question, yes this format can be expressed with XML-Schema:

  <xsd:element name="dict">
    <xsd:complexType>
      <xsd:sequence minOccurs="0" maxOccurs="unbounded">
        <xsd:element name="key" type="xsd:string" />
        <xsd:choice>
          <xsd:element name="string" type="xsd:string" />
          <xsd:element name="integer" type="xsd:integer" />
          <!-- etc. -->
        </xsd:choice>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>


Or in RELAX-NG compact :)

    grammar {
      start = Dict
      Dict = element dict { DictItem* }
      DictItem =
        element key { text },
        (element string { text } | element number { xsd:integer })
    }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: