XML Attributes and Elements Flashcards
What does XML stand for
eXtensible Markup Language (similar to HTML)
XML is designed to
support documents that contain structured information. It is commonly used for data interchange and data storage
What format was used before XML
CSV (comma separated values)
Problems associated with no standards for data interchange include:
Difficult to read, Hard to validate, Error prone
XML Code Example:
How is XML similar to HTML
they both came from SGML - standard generalized markup language
Markup language: the markup is separate from the content
HTML example
Well-Formed XML must have
a corresponding end tag for all of its start tags. Nesting of elements within each other in an XML document must be proper. For example,
<Order><id>12</id></Order>
is a correct way of nesting and
<Order><id>12</Order><id>id> is not
In each element two attributes must not have the same value. Example:
<Quantity>12</Quantity> is correct
<Quantity unit="in" unit'="lg">/Quantity> is incorrect
-Markup characters must be properly specified.
-An XML document can contain only one root element. So the root element of an XML document is an element that is preset only once in an XML document, and it does not appear as a child element within any other element.
-There are additional rules for well-formed XML documents as well https://www.w3resource.com/xml/well-formed.php for more info
</id></id></Order>
Parts of an XML Document - XML Declaration
Optional but recommended
-Version number: Required if declaration is present, often it is 1.0
-Encoding: Optional, XML parsers usually detect UTF-8 and UTF-16
<?xml version=”1.0” encoding = “utf-8”?>
Parts of an XML Document - XML Elements:
Hierarchical with start and end tags
<Item>Food Processor</Item>
Parts of an XML Document - XML Attributes:
Attributes appear in the start tag and must have a unique name. Attribute value must be enclosed in quotes
<Quantity>.
</Quantity>
Parts of an XML Document - Comments:
Comments cannot appear above the XML declaration and may span multiple lines. Provides extra information about the document.
<! - - This is a comment - - >
Parts of an XML Document - CDATA:
This is raw character data and allows the use of non-ascii characters.
<![CDATA[
Can use different characters for a description»_space;«_space;& and reserved characters
] ]>
Attributes Versus Elements
There are no rules when to use attributes versus elements, but many developers follow guidelines; for example, using elements for data and attributes for information that is peripheral or incidental to the data.
-Use elements instead of attributes
-Metadata should be stored as attributes
-Use an attribute if it influences and element value
-Do not use an attribute that influences another attribute
-Instead of using multiple attributes consider using child elements
Child Elements
<person>
<firstname>Ben</firstname>
<lastname>Solo</lastname>
<age>25</age>
</person>
One Attribute
<person>
<firstname>Ben</firstname>
<lastname>Solo</lastname>
</person>
All attributes
<person></person>
XML Element Relationships (with example)
Children are elements directly beneath a parent element. <book><title><author><year><price> are all children of <bookstore>.</bookstore></price></year></author></title></book>