dataanalyst
dataanalyst

Reputation: 316

Why does my xpath result in attributes being reported in the wrong order?

Here is my sample xml data. Currently, I am using the following xpath

"//person[@id]|//plan[@selected='yes']//*[not(@max_dur)]" and the data is reported as

> head(z1)
        id mode dep_time trav_time arr_time  type  link           x             y   start_time end_time
1 10000062  car 03:32:01  00:00:47 03:32:48  <NA>  <NA>          <NA>          <NA>       <NA>     <NA>
2 10000062 <NA>     <NA>      <NA>     <NA> links  <NA> 335757.670587 3127766.05749   03:32:48 07:37:44
3 10000062 <NA>     <NA>      <NA>     <NA>  work 21218          <NA>          <NA>       <NA>     <NA>
4 10000062  car 07:37:44  00:08:52 07:46:36  <NA>  <NA>          <NA>          <NA>       <NA>     <NA>
5 10000062 <NA>     <NA>      <NA>     <NA> links  <NA> 336661.535923 3120921.80874   07:46:36 08:23:20
6 10000062 <NA>     <NA>      <NA>     <NA>  meal 21594          <NA>          <NA>       <NA>     <NA>

But, if I were to read all the attributes using the xpath "//person[@id]|//plan[@selected='yes']//*", the attributes are reported in the right order. By right order, I mean the sequence of occurrence of the attributes in the sample data. The attributes mode, dep_time, trav_time, and arr_time should be reported last as shown below.

head(z1)
        id  type  link             x             y start_time  max_dur end_time mode dep_time trav_time
1 10000062  home 21258 334867.243653 3126570.70778   00:00:00 03:32:01     <NA>  car 03:32:01  00:00:47
3 10000062 links  <NA>          <NA>          <NA>       <NA>     <NA> 07:37:44 <NA>     <NA>      <NA>
4 10000062  work 21218 335757.670587 3127766.05749   03:32:48     <NA>     <NA>  car 07:37:44  00:08:52
6 10000062 links  <NA>          <NA>          <NA>       <NA>     <NA> 08:23:20 <NA>     <NA>      <NA>
7 10000062  meal 21594 336661.535923 3120921.80874   07:46:36     <NA>     <NA>  car 08:23:20  00:07:53
9 10000062 links  <NA>          <NA>          <NA>       <NA>     <NA> 12:30:17 <NA>     <NA>      <NA>

In essence, I do not want to read the attribute max_dur but still retain the order as shown in the second table.

Upvotes: 0

Views: 65

Answers (1)

Michael Kay
Michael Kay

Reputation: 163418

Attribute order in XML is not considered to be significant. XML parsers are not required to report the attributes in the original order, and applications should therefore not depend on the order. Different XML parsers and different XPath processors will give you the attributes in different orders.

Upvotes: 1

Related Questions