Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zurfyx/xml-in-10-minutes

Basics of XML DTD XSD XSL in 10 minutes
https://github.com/zurfyx/xml-in-10-minutes

documentation dtd xml xsd xsl

Last synced: 7 days ago
JSON representation

Basics of XML DTD XSD XSL in 10 minutes

Awesome Lists containing this project

README

        

# XML DTD XSD XSL in 10 minutes

## Table of contents

- [Introduction](#introduction)
- [XML](#xml-1)
- [DTD](#dtd-1)
- [XSD](#xsd-1)
- [XSL](#xsl-1)

## Introduction

### XML

- XML stands for eXtensible Markup Language
- XML is a markup language like HTML
- XML is designed to carry data
- XML simplifies data sharing
- XML tags are not predefined
- XML is designed to be self-descriptive
- XML is a W3C recommendation
- XML is plain text

### XML and HTML
- XML is not a replacement for HTML
- XML and HTML were designed with different goals (XML to transport and store data, HTML to display data)

### DTD

Document Type Definition (DTD) are a set of rules which a specific XML document has to conform to.

### XSD

XML Schema Definition (XSD) is used to define the elements of an XML document, just like DTD.

Defines the following:
- Elements that can appear in a document
- Attributes that can appear in a document
- Child elements
- Order of child elements
- Number of child elements
- Empty elements or not
- Data type for elements and attributes
- Default and fixed values for elements

XSD is the successor of DTD:

- Extensible future additions
- Richer
- Written in XML
- Support data types
- Support namespaces

### XSL

EXtensible Stylesheet Language (XSL) is a style sheet language for XML documents.

Describes how to XML document should be displayed.

Consists of:

- XSLT (a language that transforms XML documents)
- XPath (a language for defining parts of an XML document)
- XSL-FO (XSL Formatting Objects)

It can:
- Transform XML in XHTML
- Filter and sort XML data
- Define parts of an XML documents
- Format XML data based on its values
- Output XML data

## XML

### Example

Content example:

```XML

Ben
Lara
Hello
Hey there Lara!

```

### Declaration

```

```

or simply

```

```

**Encodings:** UTF-8, UTF-16, ISO-8859-1, ...

### Root element

XML documents always have to start with a root element, that can have as many other elements inside it.

```XML


...


...

```

### Attributes

XML elements can have attributes which can contain extra data, normally to [convey METADATA](http://stackoverflow.com/questions/1096797/should-i-use-elements-or-attributes-in-xml).

```XML


Book name

```

### Special characters

Some characters have a special meaning in XML. Thus, if you want them to be displayed as plain text you will need to escape them by using entity references.

- ```<``` less than
- ```>``` greater than
- ```&``` ampersand
- ```'``` apostrophe
- ```"``` quotation mark

### Comments
Same as HTML.

```HTML

```

### Spaces

XML, unlike HTML, preserves multiple spaces.

### Other syntax

- XML tags are case sensitive
- XML elements have to be properly nested (parent always starting before children and ending after children)
- Attributes always have to be quoted

## DTD

### Declaration

#### DTD in an external file (recommended):

On XML file (books.xml):

```

```

On DTD file (books.dtd):

_DTD file has no declaration._

#### DTD in the same XML file:

```

... (xml) ...
```

### Example

books.xml

```XML


Book name
2015-01-01

Ben
40

```

books.dtd

```DTD







```

### Terminology

**!DOCTYPE books** defines that the root element is books.

**!ELEMENT** defines an ELEMENT.

First param: indicates the element name

Second param: what it contains (which can be a value or another element).
- **(book*)** 0 or more books
- **(book+)** 1 or more books
- **(#PCDATA)** text that will be parsed by a parser*
- **(#CDATA)** text that will not be parsed by a parser*

**!ATTLIST** defines an attribute.

First param: element name

Second param: attribute name

Third param: type of attribute value
- **CDATA** text that will not be parse by a parser*

Fourth param: attribute value
- **value** default value of the attribute
- **#REQUIRED** attribute is required
- **#IMPLIED** attribute is optional
- **#FIXED value** attribute value is fixed

\* [Difference between PCDATA and CDATA](http://stackoverflow.com/questions/918450/difference-between-pcdata-and-cdata-in-dtd)

### Validation

XML documents can be validated with **xmllint** command-line tool.

```
xmllint --valid books.xml --noout
```

## XSD

### Declaration

On XML file (books.xml):

```

```

**xmlns**: namespace

**xmlns:xsi**: where elements and data type come from

**xsi:schemaLocation**: schema location. Can also be a relative URL.

On XSD file (books.xsd):

```

```

No namespace is possible as well:

```
xsi:noNamespaceSchemaLocation="books.xsd"
```

### Example

books.xml

```XML


Book name
2015-01-01

Ben
40

```

books.xsd

```XML













```

### Terminology

**xs:element**: xml element

**xs:complexType**: contains other elements

**xs:sequence**: elements must appear in a sequence

### Data types

Most common data types:
- string
- decimal
- integer
- boolean
- date
- time

### Validate

XML documents can be validated with **xmllint** command-line tool.

```
xmllint --schema books.xsd books.xml --noout
```

## XSL

### Declaration

On XML file (books.xml):

```

```

On XSL file (books.xsl):

```

...

```

### Example

books.xml

```XML


Book name
2015-01-01

Ben
40

```

books.xsl

```XML



Books




Title
Publish date
Author






(age )





```

![XSL output on browser](https://raw.githubusercontent.com/zurfyx/XML-in-10-minutes/master/assets/books_xsl.png)

### Filter

XML output can be filter by adding a select attribute, such as:

```

```

#### Operators
- ```=``` (equal)
- ```!=``` (not equal)
- ```<``` (less than)
- ```>``` (greater than)

### Sort

```

```

### Conditional

```

Teenager

```

#### Operators

- or
- and