How to

#XML #DTD #XSD #XML parsing #XML namespaces

XML

What is XML?

XML, or eXtensible Markup Language, is a markup language designed to encode documents in a format that is both human-readable and machine-readable. Its simple formal syntax makes it well-suited for creating and manipulating structured documents through software, while also being easy for humans to read and write.

XML is highly extensible, allowing developers to create markup tailored to their specific needs, constrained only by the syntax rules of the language.

Back to Table of Contents

What is DTD?

DTD, or Document Type Definition, is a set of markup declarations that define a document type for an XML document. It establishes relationships between elements and attributes.

For instance, a DTD for HTML specifies that the DIV tag must reside within the BODY tag, and can occur multiple times; the TITLE must be inside the HEAD, and can appear only once, while the SCRIPT tag can appear any number of times in both sections.

A DTD is typically described in the document itself with a declaration starting with <!DOCTYPE ... > or in a separate file.

Back to Table of Contents

How does well-formed XML differ from valid XML?

Based on the level of adherence to standards, a document may be classified as either “well-formed” or “valid”.

The core characteristics of well-formed XML stem from the formal descriptions within the standard:

A document is considered valid if it is formed according to the syntax rules of the specified XML standard and matches the corresponding DTD.

Well-formed XML is syntactically correct (able to be parsed by a parser), while valid XML is correct both syntactically and semantically (it adheres to the rules defined by a given DTD).

Back to Table of Contents

What is a “namespace” in XML?

An XML namespace is a collection of names used in XML documents to distinguish among elements and attributes. It is identifiable via a URI reference. Unlike traditional programming namespaces, XML namespaces have an internal structure and are not considered a set mathematically.

Namespaces are declared using the XML attribute xmlns, where the value must be a URI that uniquely identifies the namespace for each element.

All element names within a namespace must be unique.

Generally, an XML namespace does not require an associated DTD.

An XML document may contain element and attribute names from multiple XML vocabularies. Each vocabulary defines its own namespace, resolving name-clashing issues.

Back to Table of Contents

What is XSD? What are its advantages over XML DTD?

XSD, or XML Schema Definition, is a language for defining the structure of an XML document. Specifically, XML Schema describes:

Advantages of XSD over DTD include the following:

Back to Table of Contents

What types exist in XSD?

Simple types refer to definitions for values that may be used as content within an element or attribute. This data type cannot contain elements or have attributes.

<xsd:element name='price' type='xsd:decimal'/>
...
<price>45.50</price>

Complex types refer to definitions for elements that may contain attributes and other elements.

<xsd:element name='price'>
    <xsd:complexType base='xsd:decimal'>
        <xsd:attribute name='currency' type='xsd:string'/>
    </xsd:complexType>
</xsd:element>
...
<price currency='US'>45.50</price>

Back to Table of Contents

What methods of reading XML do you know? Describe the strengths and weaknesses of each method.

DOM (Document Object Model) is an object-oriented method that reads XML while reconstructing it in memory as an object structure, representing the document as a set of tags—nodes. Each node can have an unlimited number of child nodes. Ultimately, this creates a tree structure.

➖ Low processing speed.

➖ High memory consumption.

➕ Simple to program.

➕ Works well if the XML contains multiple objects with references to one another; it can make two passes over the document: first to create objects without links and fill a “name-object” dictionary, then a second pass to restore those links.

➕ If there is an error in the XML, a partially constructed XML structure remains in memory, which will be automatically cleaned up.

➕ Suitable for both reading and writing.

SAX (Simple API for XML) is an event-driven method that reads XML and reacts to events (opening or closing tags, strings, attributes) by invoking application-provided event handlers. Unlike DOM, it does not store the document in memory.

➕ High processing speed.

➕ Low memory consumption.

➗ Fairly complex to program.

➖ If there are many interlinked objects in the XML, temporary storage of string references is required to convert them into pointers after the document is read.

➖ If there is an error in the XML, a partially constructed application domain structure remains in memory; the programmer must manage its cleanup.

➖ Suitable only for reading.

StAX (Stream API for XML) is a streaming method comprising two sets of APIs for processing XML that provide different levels of abstraction. The cursor-based API allows applications to interact with XML as a stream of tokens (or events); the application can check the parser’s status and obtain information about the last parsed token before moving to the next. The second high-level API uses event iterators, letting applications process XML as a series of event objects related to XML structure components. It requires the application to identify the specific types of syntax-analyzed events and utilize the appropriate methods for retrieving relevant information.

➗ Retains the advantages of SAX over DOM.

➕ Not based on callbacks; applications do not have to manage the parser’s emulated state as they do when using SAX.

➖ Suitable only for reading.

Back to Table of Contents

When to use DOM vs. SAX or StAX parsers?

DOM is the natural choice when the XML structure itself is the subject of interest, especially when modification of the document’s structure is needed or if information from the document is used repeatedly.

For quick, one-time reads, SAX or StAX is optimal.

Back to Table of Contents

What methods of writing XML do you know?

Direct writing entails writing XML tag by tag, attribute by attribute.

➕ High processing speed.

➕ Memory-efficient: does not create intermediate objects.

➖ Suitable only for writing.

Writing via DOM (Document Object Model) creates a complete XML structure before any writing occurs.

➖ Low processing speed.

➖ Non-optimal memory usage.

➕ Suitable for both writing and reading.

Back to Table of Contents

What is JAXP?

JAXP, or The Java API for XML Processing, is a set of APIs designed to simplify XML data processing in Java applications. It includes implementations for DOM, SAX, and StAX parsers, supports XSLT, and enables work with DTD.

Back to Table of Contents

What is XSLT?

XSLT, or eXtensible Stylesheet Language Transformations, is a language for transforming XML documents.

XSLT was developed for use in XSL (eXtensible Stylesheet Language), the stylesheet language for XML. During XSL transformations, an XSLT processor reads the XML document and its corresponding XSLT stylesheets. Based on the instructions found in the stylesheets, the processor produces a new XML document or fragments thereof.

Back to Table of Contents

Conclusion

XML is a powerful tool for defining structured data formats. Understanding the core concepts of XML, including DTDs, XSDs, and namespaces, is essential for developers working on data interchange and web applications.

If you want to gain a deeper understanding of XML structures and processing techniques, familiarizing yourself with best practices will help you leverage XML to its full potential. From XML parsing methods to writing techniques and processing APIs, each foundational element of XML plays a vital role in modern data handling.

By broadening your knowledge of XML, including how it is validated and transformed using XSLT, you can enhance the functionality and interoperability of your applications.

For more information on XML fundamentals, consider researching topics such as XML processing frameworks and the differences between various XML parsers. Embracing XML in your projects will empower you to create flexible and maintainable data structures.