LuckyLuke
LuckyLuke

Reputation: 49077

About IDs in XML

I just started on a xml document just for practicing xslt and I have a question about IDs. When using ID attribute, does the same rule apply about IDs as in "normal" HTML, only one pr. document? I added a book ID, but I want to have a ID on each author as well, then I can't use 1 again right? And another short question, the naming of tags as I did here, naming the collection in plural and keeping each book as in this case in singular is that ok? Or are there better naming conventions?

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book id="1">
        <title>An Introduction to XML and Web Technologies</title>
        <authors>
            <author>
                <name>Anders Møller</name>
            </author>
            <author>
                <name>Michael Schwartzbach</name>
            </author>
        </authors>
    </book>

    ...
</books>

Upvotes: 0

Views: 955

Answers (2)

Mat
Mat

Reputation: 206699

There is nothing in XML that prevents you from having multiple "objects" with the exact same attributes. XML doesn't define the meaning of tags or attributes, only the format in which they are supposed to be written. (Also note that the order in which your "objects" appear in the XML file doesn't have to be preserved by the parsers - neither during read-in or write-out.)

The interpretation/semantics if those tags/attributes is entirely up to the application to define.

Your naming convention looks fine to me, it's quite clear and seems usual enough.

Upvotes: 2

Antwan van Houdt
Antwan van Houdt

Reputation: 6991

an ID usually is a unique number tagged to a certain object or value.. but in the case of XML you can makeup your own attributes so it shouldn't really matter for whatever you are going to use it. in HTML it is important because its the way you hook it up with your CSS styles, this however has nothing to do with your XML.

I'd suggest using something that is more generic if you want more objects to have it, like an attribute called type or class

Upvotes: 1

Related Questions