Closed charlietaylor98 closed 3 years ago
@charlietaylor98 Good question!
The length of the attribute name shouldn’t matter in terms of processing, although as it grows longer, the advantage of having something that’s human-readable diminishes, since it becomes harder to read. More importantly, though, if there is a risk that two things may happen to have the same name, you could wind up with a validation problem because @id
values have to be unique in the document.
XPath provides a method for working around this: there is a function called generate-id()
that creates an @id
value that is guaranteed to satisfy the following requirements:
@id
will have a value that is unique in the document.The downside of generate-id()
is that the value is not human-readable, so you can’t glance at it and look for obvious mistakes the way you can with your approach.
I’ve used both of these methods in my own work. I prefer to construct my own values when I can be confident that they will be unique and easy to read. I use generate-id()
in situations where I can’t come up with a good alternative, or where there’s a risk of creating duplicates.
In one of my assignments, I wanted to link titles to list items. To give each a unique
@id
value, I set@id
values equal to the title itself, using thetranslate()
function remove spaces. The expression was as follows: `So the title "Christy Carlson Romano, dans le rôle de Ren Stevens" became the attribute value "ChristyCarlsonRomano,danslerôledeRenStevens". Is it efficient to have
@id
and@href
values that are this long, and is there an xpath expression that could return, say, just the first word of the title? Maybe using thetokenize()
function?