Re:XML is almost always misused
by commandlinefan on Tuesday October 29, 2019 @05:47PM
(#21392439)
Well, actually, an even better example is:
<employees>
<employee id="1">
<name>John</name>
<city>London</city>
</employee>
<employee id="1">
<name>Jack</name>
<city>Boston</city>
</employee>
I know what you’re trying to do there - you’re trying to “future-proof” your schema by allowing introduction of arbitrary new elements. Which means that there’s no standard way to guard against somebody omitting a required field (like “name”) or adding a new field like “creditCardNumber” - other than to document your acceptable key values in a non-standard format and add defensive code that a validating parser would have given you. You’re better off taking as much advantage of the format as you can.If you accept the author's premise then all of these (including his) are wrong uses of XML.
The XPath for the second one would be:
//root/item/key[text()="Name"]/../value/text()
Re:XML is almost always misused
by icebraining on Tuesday October 29, 2019 @06:03PM
(#21392582)
Alternatively
//root/item[key[text()="Name"]]/value/text()
I like this way
So I can use this xpath to get the person's name: Not sure what would be the xpath to get the name if the XML was This is a better example: