diff options
author | Suzane Sant Ana <tetestonaldo@gmail.com> | 2017-12-31 14:27:06 -0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-31 14:27:06 -0200 |
commit | 42f9329bb3a028d374d6397991ac48b44064741e (patch) | |
tree | 1e75e2b3e122aeb863e3ffa037f6f64c4027fbf8 /xml.html.markdown | |
parent | e6b77595f2669d66ac7be43c6e6083cbff80a9a7 (diff) | |
parent | 70a36c9bd970b928adde06afb2bd69f6ba8e5d5c (diff) |
Merge pull request #1 from adambard/master
update
Diffstat (limited to 'xml.html.markdown')
-rw-r--r-- | xml.html.markdown | 160 |
1 files changed, 100 insertions, 60 deletions
diff --git a/xml.html.markdown b/xml.html.markdown index 94fc93f4..b4b54330 100644 --- a/xml.html.markdown +++ b/xml.html.markdown @@ -3,18 +3,77 @@ language: xml filename: learnxml.xml contributors: - ["João Farias", "https://github.com/JoaoGFarias"] + - ["Rachel Stiyer", "https://github.com/rstiyer"] + - ["Deepanshu Utkarsh", "https://github.com/duci9y"] --- -XML is a markup language designed to store and transport data. +XML is a markup language designed to store and transport data. It is supposed to be both human readable and machine readable. -Unlike HTML, XML does not specifies how to display or to format data, just carry it. +Unlike HTML, XML does not specify how to display or to format data, it just carries it. -* XML Syntax +Distinctions are made between the **content** and the **markup**. In short, content could be anything, markup is defined. + +## Some definitions and introductions + +XML Documents are basically made up of *elements* which can have *attributes* describing them and may contain some textual content or more elements as its children. All XML documents must have a root element, which is the ancestor of all the other elements in the document. + +XML Parsers are designed to be very strict, and will stop parsing malformed documents. Therefore it must be ensured that all XML documents follow the [XML Syntax Rules](http://www.w3schools.com/xml/xml_syntax.asp). ```xml -<!-- Comments in XML are like this --> +<!-- This is a comment. It must not contain two consecutive hyphens (-). --> +<!-- Comments can span + multiple lines --> + +<!-- Elements --> +<!-- An element is a basic XML component. There are two types, empty: --> +<element1 attribute="value" /> <!-- Empty elements do not hold any content --> +<!-- and non-empty: --> +<element2 attribute="value">Content</element2> +<!-- Element names may only contain alphabets and numbers. --> + +<empty /> <!-- An element either consists an empty element tag… --> +<!-- …which does not hold any content and is pure markup. --> + +<notempty> <!-- Or, it consists of a start tag… --> + <!-- …some content… --> +</notempty> <!-- and an end tag. --> + +<!-- Element names are case sensitive. --> +<element /> +<!-- is not the same as --> +<eLEMENT /> + +<!-- Attributes --> +<!-- An attribute is a key-value pair and exists within an element. --> +<element attribute="value" another="anotherValue" many="space-separated list" /> +<!-- An attribute may appear only once in an element. It holds just one value. + Common workarounds to this involve the use of space-separated lists. --> + +<!-- Nesting elements --> +<!-- An element's content may include other elements: --> +<parent> + <child>Text</child> + <emptysibling /> +</parent> +<!-- Standard tree nomenclature is followed. Each element being called a node. + An ancestor a level up is the parent, descendants a level down are children. + Elements within the same parent element are siblings. --> + +<!-- XML preserves whitespace. --> +<child> + Text +</child> +<!-- is not the same as --> +<child>Text</child> +``` +## An XML document + +This is what makes XML versatile. It is human readable too. The following document tells us that it defines a bookstore which sells three books, one of which is Learning XML by Erik T. Ray. All this without having used an XML Parser yet. + +```xml <?xml version="1.0" encoding="UTF-8"?> +<!-- This is called an XML prolog. Optional, but recommended. --> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> @@ -35,83 +94,49 @@ Unlike HTML, XML does not specifies how to display or to format data, just carry <price>39.95</price> </book> </bookstore> - -<!-- Above is a typical XML file. - It starts with a declaration, informing some metadata (optional). - - XML uses a tree structure. Above, the root node is 'bookstore', which has - three child nodes, all 'books'. Those nodes has more child nodes, and so on... - - Nodes are created using open/close tags, and childs are just nodes between - the open and close tags.--> - - -<!-- XML carries two kind of data: - 1 - Attributes -> That's metadata about a node. - Usually, the XML parser uses this information to store the data properly. - It is characterized by appearing in parenthesis within the opening tag - 2 - Elements -> That's pure data. - That's what the parser will retrieve from the XML file. - Elements appear between the open and close tags, without parenthesis. --> - - -<!-- Below, an element with two attributes --> -<file type="gif" id="4293">computer.gif</file> - - ``` -* Well-Formated Document x Validation - -A XML document is well-formated if it is syntactically correct. -However, it is possible to inject more constraints in the document, -using document definitions, such as DTD and XML Schema. +## Well-formedness and Validation -A XML document which follows a document definition is called valid, -regarding that document. - -With this tool, you can check the XML data outside the application logic. +A XML document is *well-formed* if it is syntactically correct. However, it is possible to add more constraints to the document, using Document Type Definitions (DTDs). A document whose elements are attributes are declared in a DTD and which follows the grammar specified in that DTD is called *valid* with respect to that DTD, in addition to being well-formed. ```xml - -<!-- Below, you can see an simplified version of bookstore document, - with the addition of DTD definition.--> - +<!-- Declaring a DTD externally: --> <?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE note SYSTEM "Bookstore.dtd"> +<!DOCTYPE bookstore SYSTEM "Bookstore.dtd"> +<!-- Declares that bookstore is our root element and 'Bookstore.dtd' is the path + to our DTD file. --> <bookstore> <book category="COOKING"> - <title >Everyday Italian</title> + <title lang="en">Everyday Italian</title> + <author>Giada De Laurentiis</author> + <year>2005</year> <price>30.00</price> </book> </bookstore> -<!-- This DTD could be something like:--> - -<!DOCTYPE note -[ +<!-- The DTD file: --> <!ELEMENT bookstore (book+)> -<!ELEMENT book (title,price)> +<!-- The bookstore element may contain one or more child book elements. --> +<!ELEMENT book (title, price)> +<!-- Each book must have a title and a price as its children. --> <!ATTLIST book category CDATA "Literature"> +<!-- A book should have a category attribute. If it doesn't, its default value + will be 'Literature'. --> <!ELEMENT title (#PCDATA)> +<!-- The element title must only contain parsed character data. That is, it may + only contain text which is read by the parser and must not contain children. + Compare with CDATA, or character data. --> <!ELEMENT price (#PCDATA)> ]> - -<!-- The DTD starts with a declaration. - Following, the root node is declared, requiring 1 or more child nodes 'book'. - Each 'book' should contain exactly one 'title' and 'price' and an attribute - called 'category', with "Literature" as its default value. - The 'title' and 'price' nodes contain a parsed character data.--> - <!-- The DTD could be declared inside the XML file itself.--> <?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE note -[ +<!DOCTYPE bookstore [ <!ELEMENT bookstore (book+)> -<!ELEMENT book (title,price)> +<!ELEMENT book (title, price)> <!ATTLIST book category CDATA "Literature"> <!ELEMENT title (#PCDATA)> <!ELEMENT price (#PCDATA)> @@ -119,8 +144,23 @@ With this tool, you can check the XML data outside the application logic. <bookstore> <book category="COOKING"> - <title >Everyday Italian</title> + <title>Everyday Italian</title> <price>30.00</price> </book> </bookstore> -```
\ No newline at end of file +``` + +## DTD Compatibility and XML Schema Definitions + +Support for DTDs is ubiquitous because they are so old. Unfortunately, modern XML features like namespaces are not supported by DTDs. XML Schema Definitions (XSDs) are meant to replace DTDs for defining XML document grammar. + +## Resources + +* [Validate your XML](http://www.xmlvalidation.com) + +## Further Reading + +* [XML Schema Definitions Tutorial](http://www.w3schools.com/schema/) +* [DTD Tutorial](http://www.w3schools.com/xml/xml_dtd_intro.asp) +* [XML Tutorial](http://www.w3schools.com/xml/default.asp) +* [Using XPath queries to parse XML](http://www.w3schools.com/xml/xml_xpath.asp) |