News for hackers. Stuff that matters.
Re:XML is almost always misused by robofanatic on Tuesday October 29, 2019 @04:51PM (#21391862)
I guess the correct answer depends upon the requirement.

I like this way

  <root>
   <item key="name">John</item>
   <item key="city">London</item>
  </root>
So I can use this xpath to get the person's name:

  //root/item[@key="name"]/text()

Not sure what would be the xpath to get the name if the XML was

  <root>
   <item>
    <key>Name</key>
    <value>John</value>
   </item>
   <item>
    <key>City</key>
    <value>London</value>
   </item>
  </root>

This is a better example:

  <employees>
   <employee id="1">
     <field name="name">John</field>
     <field name="city">London</field>
   </employee>
   <employee id="2">
     <field name="name">Jack</field>
     <field name="city">Boston</field>
   </employee>
  <employees>
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.
Re:XML is almost always misused by scandox on Tuesday October 29, 2019 @05:03PM (#21392005)
If you accept the author's premise then all of these (including his) are wrong uses of XML.
Re:XML is almost always misused by elFarto on Tuesday October 29, 2019 @05:40PM (#21392377)
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()
Re:XML is almost always misused by benibela on Tuesday October 29, 2019 @06:35PM (#21392837)
You usually do not need to use text() in xpath, so this should work the same:

  //root/item[key="name"]/value