7 ms·
Hmm...somewhat disagree with the "correct" way to express a dictionary. I prefer: <root> <Name>John</Name> <City>London</City> </root> Remo
by mpweiher 7y ago
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.
- progval 7y agoThat'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.
- mpweiher 7y agoXML allows arbitrary strings as key names just fine.
- progval 7y agoNo, 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.
- sorenjan 7y agoBut 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?
- progval 7y agoIt 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.
- Felk 7y agoWhich 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.
- mpweiher 7y agoXML parsers generally allow arbitrary tags.
- srott 7y agoyou'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> ...
- progval 7y agoNo, 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>
- spiralx 7y agoOr in RELAX-NG compact :) grammar { start = Dict Dict = element dict { DictItem* } DictItem = element key { text }, (element string { text } | element number { xsd:integer }) }
- jimktrains2 7y agoThe 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.
- Sharlin 7y agoThis 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.
- mpweiher 7y agoHmm...you seem to be mapping programming language concepts 1:1 to XML. That is a mistake.
- Sharlin 7y agoI'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.
- deleted 7y ago[deleted]
- benibela 7y agoThat is definitely the best to use with xpath