This is implementation-depended, so one can not generalize this for all XML databases. Although in this simple case, I guess it is the same for all databases: It does not matter.
I am going to explain for BaseX what will be happening here. Lets say you use the first structure and you want to get the <A/>
element. So you use an XPath like
//A[Name = "John"]
This will be optimized to the following query:
db:text("your-database", "John")/parent::*:Name/parent::*:A
Whereas an XPath for your second data structure would probably look something like this:
//A[Name/@n = "John"]
which will be optimized to be
db:attribute("your-database", "John")/self::*:n/parent::*:Name/parent::*:A
As you can see, apart from the one path step more (because you have to access the attribute), which is very cheap, the major difference is using db:text()
vs. db:attribute()
. But as documented, both of this functions will use the value index if present (which it is by default), and will be quite fast thanks to the index lookup.
In reality, if you are designing an XML-based application and want to later retrieve information using XQuery, you will most certainly have other bottlenecks, e.g. non-index using queries or nested for loops.