Title: XML - how to decide wether to put same nodes in parent node
Tags: xml
Question: I have an XML ```bookstore``` and I am not sure how to structure it.

The thing I am focussing on is that a book can have multiple authors.

I usually see XMLs putting elements with the same name in a parent element.

What's the idea behind that? What are the advantages and disadvantages in the following two slightly different approaches?

1) Authors are not in a parent element: 

```<?xml version="1.0" encoding="UTF-8"?&gt;
<bookstore&gt;
    <book category="web"&gt;
        <title lang="en"&gt;Superbook</title&gt;
        <author&gt;
            <name&gt;Clark</name&gt;
            <lastname&gt;Kent</lastname&gt;
        </author&gt;
        <author&gt;
            <name&gt;Lois</name&gt;
            <lastname&gt;Lane</lastname&gt;
        </author&gt;
        <author&gt;
            <name&gt;Lex</name&gt;
            <lastname&gt;Luther</lastname&gt;
        </author&gt;
        <year&gt;2003</year&gt;
        <price&gt;49.99</price&gt;
    </book&gt;
    <book category="web" cover="paperback"&gt;
        <title lang="en"&gt;Batman</title&gt;
        <author&gt;
            <name&gt;Bruce</name&gt;
            <lastname&gt;Wayne</lastname&gt;
        </author&gt;
        <year&gt;2003</year&gt;
        <price&gt;39.95</price&gt;
    </book&gt;
</bookstore&gt;
```

2) Authors are in a parent element:

```<?xml version="1.0" encoding="UTF-8"?&gt;
<bookstore&gt;
    <book category="comic"&gt;
        <title lang="en"&gt;Superbook</title&gt;
        <authors&gt;
            <author&gt;
                <name&gt;Clark</name&gt;
                <lastname&gt;Kent</lastname&gt;
            </author&gt;
            <author&gt;
                <name&gt;Lois</name&gt;
                <lastname&gt;Lane</lastname&gt;
            </author&gt;
            <author&gt;
                <name&gt;Lex</name&gt;
                <lastname&gt;Luther</lastname&gt;
            </author&gt;
        </authors&gt;
        <year&gt;2003</year&gt;
        <price&gt;49.99</price&gt;
    </book&gt;
    <book category="comic" cover="paperback"&gt;
        <title lang="en"&gt;Batman</title&gt;
        <authors&gt;
            <author&gt;
                <name&gt;Bruce</name&gt;
                <lastname&gt;Wayne</lastname&gt;
            </author&gt;
        </authors&gt;
        <year&gt;2003</year&gt;
        <price&gt;39.95</price&gt;
    </book&gt;
</bookstore&gt;
```

Comment: That's primarily opinion based. My personal opinion is that grouping same elements in a super-element makes the XML more readable/handy. For example, you could simply "mask" the `` element (with all `` sub-elements) in a capable editor by clicking on the `+`/`-` on the left site. But this is just one minor advantage.

Comment: I really like the advantage of "masking". I've tried it with the Brackets editor and I find it surprisingly useful!


Here is another answer: It's up to you as the W3 specification sites you can use elements / attributes as you see fit.
I use an attribute based layout, and avoid "collection" elements like "authors" that really don't serve a purpose

```<xml&gt;
    <book category='web' year='2003' price='49.99'&gt;
        <title lang='en'&gt;Superbook</title&gt;
        <author name='Clark' lastname='Kent'/&gt;
        <author name='Lois' lastname='Lane'/&gt;
        <author name='Lex' lastname='Luther'/&gt;
    </book&gt;

    <book category='web' cover='paperback' year='2003' price='49.99'&gt;
        <title lang='en'&gt;Batman</title&gt;
        <author name='Bruce' lastname='Wayne'/&gt;
    </book&gt;
</xml&gt;
```

Comment for this answer: The data argument is well known, and a common approach.  It extends the approach used for HTML.  However, XML is much bigger than HTML.  In HTML, data (elements) is "visible", and attributes are not.  In a XML document, there's no distinction between "visible" and "invisible", everything is "data".  With that in mind, the attribute based XML camp starts designs with everything as an attribute, and uses elements to group attributes together.  There is no "right" answer here elements or attributes.  Do avoid extra grouping, and never put numbers in element / attribute names : ).
Comment for this answer: I thought about the use of attributes too and came across an [IBM developer article](https://www.ibm.com/developerworks/xml/library/x-eleatt/index.html) that's worthwile reading: The takeaway for me was "`data goes in elements, metadata in attributes`", and for me "name" as well as "last name" is data.
Comment for this answer: Thank's for the insight! From some recent experience with attributes I tend to shy away from using them for names or anything I would usually put inside CDATA. Just imagine: `name='Antoine Jérôme'` or `title='Wallace & Gromit: The Curse of the Were-Rabbit (2005)'`

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:04/01/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<xml>', 'Title': 'XML - how to decide wether to put same nodes in parent node', 'ViewCount': 27, 'id': 60974939, 'user_AboutMe': '<p><strong>Javascript</strong> programmer with a strong interest in <strong>DVB</strong>, <strong>video</strong> and <strong>graphics</strong>.</p>\n\n<p>I have learned so much from the Stack*Community in recent years! I\'d like to continue learning as well as helping other people out as best as I can.</p>\n\n<p>You\'re welcome to contact me <a href="http://mailto:wolf.bauer@me.com" rel="nofollow noreferrer">wolf.bauer@me.com</a></p>\n\n<pre><code>                             __                             \n                          .\' TO$;\\                           \n                         /  : TP._;                           \n                        / _.;  :Tb|                           \n                       /   /   ;j$j                           \n                   _.-"       d$$$$                           __ ____    __                               ______                                           \n                 .\' ..       d$$$$;         _      __ ____   / // __/   / /_   ____ _ __  __ ___   _____ / ____ \\ ____ ___   ___     _____ ____   ____ ___ \n                /  /P\'      d$$$$P. |\\     | | /| / // __ \\ / // /_    / __ \\ / __ `// / / // _ \\ / ___// / __ `// __ `__ \\ / _ \\   / ___// __ \\ / __ `__ \\\n               /   "      .d$$$P\' |\\^"l    | |/ |/ // /_/ // // __/_  / /_/ // /_/ // /_/ //  __// /   / / /_/ // / / / / //  __/_ / /__ / /_/ // / / / / /\n             .\'           `T$P^"""""  :    |__/|__/ \\____//_//_/  (_)/_.___/ \\__,_/ \\__,_/ \\___//_/    \\ \\__,_//_/ /_/ /_/ \\___/(_)\\___/ \\____//_/ /_/ /_/\n         ._.\'      _.\'                ;                                                                 \\____/                                             \n      `-.-".-\'-\' ._.       _.-"    .-"                           \n    `.-" _____  ._              .-"                           \n   -(.g$$$$$$$b.              .\'                           \n     ""^^T$$$P^)            .(:                           \n       _/  -"  /.\'         /:/;                           \n    ._.\'-\'`-\'  ")/         /;/;                           \n `-.-"..--""   " /         /  ;                           \n.-" ..--""        -\'          :                           \n..--""--.-"         (\\      .-(\\                           \n  ..--""              `-\\(\\/;`                           \n    _.                      :                           \nArt by Blazej Kozlowski\n</code></pre>\n', 'user_AccountId': 8603890.0, 'user_DisplayName': 'drake7', 'user_Reputation': 670.0, 'user_Views': 122.0, 'user_id': 6444590.0}}