Python XML Elements vs. Attributes & Best Practices
In XML, data can be stored in child elements (<price>29.99</price>) or attributes (<item price="29.99">). A foundational XML architectural principle is: store domain data as elements and store metadata (IDs, types, encoding) as attributes.
"Elements are the cargo inside a shipping container (the actual goods you want). Attributes are the barcode and tracking number stamped on the outside of the container."
Deep Dive: How It Works
Elements: Can contain sub-elements, multiple values, and complex structures.
Attributes: Cannot contain multiple values, cannot contain tree hierarchies, and are harder to extend.
Best Practice: Use attributes for unique IDs (<order id="ord-101">) and elements for extensible payload fields.
Syntax Blueprint
<element attribute="metadata">Payload Data</element>
Attributes describe metadata about the element; child elements store structured data.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Putting complex domain data into attributes.Why it happens: <person name="John Doe" street="123 Main" city="Boston" /> is difficult to query and schema-validate.
How to fix: Break complex attributes into child elements: <name>, <address><street>, etc.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Model a server node configuration
Create a <server id="srv-01" region="us-east"> element.
Add child <hostname>worker.internal</hostname> and <port>9000</port>.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.