350 likes | 492 Views
This technical introduction to XML by Felix Eickhoff delves into the essentials of XML, detailing its importance as a W3C-endorsed standard for data markup and sharing. We cover XML fundamentals, Document Type Definitions (DTD), and XML Schemas, emphasizing practical applications such as XML transformations and namespaces. The tutorial includes examples and explanations of well-formedness, elements, attributes, and more, making it a valuable resource for anyone looking to understand XML in depth.
E N D
XML Basics A technical introduction Felix Eickhoff felix.eickhoff@gmx.net
Overview • Why XML • XML Fundamentals • Document Type Definitions • XML Schemas • Namespaces in XML • XSL Transformations • XPath • XLinks • XPointers
Why XML • W3C endorsed standard • Generic syntax to mark up data • More flexible than HTML • Need for sharing data
XML Fundamentals • XML Version, Doctype • Elements • Start and End Tag • Attributes • Case sensitive • Contain other elements or data • Comments • Well formedness
XML Fundamentals - Example <?xml version=„1.0“?><?DOCTYPE address-book SYSTEM „address-book.dtd“?> <address-book> <entry> <PersonName honorific=„Ms.“> <SingleName>Doe</SingleName> <PersonName/> <address> <street>34 Fountain Square Plaza</street> <postal-code>45202</postal-code> </address> <tel prefered=„true“>515-73737</tel> <tel>515-28378</tel> <email href=„mailto:jdoe@mail.com“/> </entry> <entry> <PersonName honrorifc=„Mr.“><FirstName>Jack</FirstName> <MiddleName>Phillip</MiddleName> <LastName>Smith</LastName></PersonName> <tel>737-378477</tel> <email href=„mailto:jsmith@mail.com“/> </entry> </address-book>
Document Type Definitions - DTD • Defines language grammar for a particular XML application • What for: • Specific XML applications • Validation of XML-files
DTD - Example <!ELEMENT address-book (entry+)> <!ELEMENT entry (name, address*, tel*, fax*, email*)> <!ELEMENT PersonName (SingleName|(FirstName, MiddleName*, LastName))> <!ELEMENT FirstName (#PCDATA)> <!ELEMENT MiddleName (#PCDATA)> <!ELEMENT LastName (#PCDATA)> <!ELEMENT SingleName (#PCDATA)> <!ELEMENT address (street, postal-code> <!ELEMENT street (#PCDATA)> <!ELEMENT postal-code (#PCDATA)> <!ELEMENT tel (#PCDATA)> <!ATTLIST tel preferred (true | false) „false“> <!ELEMENT fax (#PCDATA)> <!ATTLIST fax preferred (true | false) „false“> <!ELEMENT email EMPTY> <!ELEMENT email href CDATA #REQUIRED preferred (true|false) „false“>
Document Type Defintions • Element Declaration • #PCDATA, EMPTY, ANY • Sequences • Using regular Expressions (* , + ,| ) • Attribute Declaration • CDATA, NMTOKEN, ENUMERATION, ENTITY, ID, IDREF, NOTATION, #REQUIRED, #IMPLIED, #FIXED • Entities • Internal and External
XML Schemas • Alternative to DTD • XML Syntax • Data Typing • Content Models • Extensible • Dynamic Schemas • Self Documenting
XML - Example <?xml version=„1.0“?><?DOCTYPE address-book SYSTEM „address-book.xsd“ <address-book> <entry> <PersonName honorific=„Ms.“> <SingleName>Doe</SingleName> <PersonName/> <address> <street>34 Fountain Square Plaza</street> <postal-code>45202</postal-code> </address> <tel prefered=„true“>515-73737</tel> <tel>515-28378</tel> <email href=„mailto:jdoe@mail.com“/> </entry> <entry> <PersonName honorific=„Mr.“><FirstName>Jack</FirstName> <MiddleName>Phillip</MiddleName> <LastName>Smith</LastName></PersonName> <tel>737-378477</tel> <email href=„mailto:jsmith@mail.com“/> </entry> </address-book>
XML Schema - Example <?xml version=„1.0“ ?> <schema xmlns=„http://www.w3.org/2000/10/XMLSchema“> <simpleType name=„Person Title“> <restriction base=„string“> <enumeration value=„Mr.“/> <enumeration value=„Ms.“/> </restriction> </simpleType> <complexType name=„Text“ content=„textonly“> <restriction base=„string“ /> </complexType>
XML Schema – Example (2) <element name=„PersonName“> <complexType content=„element“> <choice> <element name=„Single Name“ type=„Text“ minOccurs=„1“ maxOccurs=„1“/> <sequence> <element name=„FirstName“ type=„Text“ minOccurs=„1“ maxOccurs=„1“/> <element name=„MiddleName“type=„Text“ minOccurs=„1“ maxOccurs=„unbounded“/> <element name=„LastName“ type=„Text“ minOccurs=„1“ maxOccurs=„1“/> </sequence> </choice> <attribute name=„honorific“ ref=„PersonTitle“/> </complexType> </element> ... </schema>
Namespaces in XML • Distinguish between elements and attributes from different XML applications • Group related elements and attributes together • Based on unique Domain-Names
Namespace - Example <?xml version=„1.0“ ?> <catalog> <painting> <title>Memory of the Garden of Etten</title> <artist>Vincent Van Gogh</artist> <date>November, 1888</date> <description> Two women ... </description> </painting> <!– Many more paintings... --> </catalog>
Namespace – Example(2) <?xml version=„1.0“ ?> <RDF> <Description about=„http://name.com/impressionists.html“> <title>Impressionists Paintings</title> <creator>Joe Doe</creator> <description> A list of famous impressionist paintings organized by painter and date </description> <date>2001-10-05</date> </Description> </RDF>
Namespace – Example(3) <?xml version=„1.0“ ?> <catalog> <RDF> <Description about=„http://name.com/impressionists.html“> <title>Impressionists Paintings</title> <creator>Joe Doe</creator> <description> A list of famous impressionist paintings organized by painter and date </description> <date>2001-10-05</date> </Description> </RDF> <painting> <title>Memory of the Garden of Etten</title> <artist>Vincent Van Gogh</artist> <date>November, 1888</date> <description> Two women ... </description> </painting> </catalog>
Namespace – Example(4) <?xml version=„1.0“ ?> <catalog> <rdf:RDF xmlns:rdf=„http://www.w3.org./TR/REC-rdf-synstax#“> <rdf:Description xlmns:dc=„http://purl.org/dc“ about=„http://name.com/impressionists.html“> <dc:title>Impressionists Paintings</dc:title> <dc:creator>Joe Doe</dc:creator> <dc:description> A list of famous impressionist paintings organized by painter and date </dc:description> <dc:date>2001-10-05</dc:date> </rdf:Description> </rdf:RDF> <painting> <title>Memory of the Garden of Etten</title> <artist>Vincent Van Gogh</artist> <date>November, 1888</date> <description> Two women ... </description> </painting> </catalog>
XSL Transformations • High-level language for defining XML Transformations • Format information for Display • Useful for managing data exchange • Report Writing • Work done by Stylesheet processors
Stylesheet – Example Input <?xml version=„1.0“ ?> <people> <person born=„1912“ died=„1954“> <name> <first_name>Alan</first_name> <last_name>Turing</last_name> </name> <profession>computer scientist</profession> <profession>mathematican</profession> <profession>cryptographer</profession> </person> <person born=„1918“ died=„1988“> <name> <first_name>Richard</first_name> <middle_initial>M</middle_initial> <last_name>Feyman</last_name> </name> <profession>physicist</profession> <hobby>Playing the bongoes</hobby> </person> </people>
Stylesheet – Example <?xml version=„1.0“?> <xsl:stylesheet version=„1.0“ xmlns:xsl=„http://www.w3.org/1999/XSL/Transform“> <xsl:template match=„people“> <html> <head><title>Famous Scientists</title></head> <body> <dl> <xls:apply-templates/> </dl> </body> </html> </xsl:template>
Stylesheet – Example (2) <xsl:template match=„person“> <dt><xsl:apply-templates select=„name“/></dt> <dd><ul> <li>Born: <xsl:apply-templates select=„@born“/></li> <li>Died: <xsl:apply-templates select=„@died“/></li> </ul></dd> </xsl:template> <xsl:template match=„name“> <xsl:value-of select=„last_name“/> <xsl:value-of select=„first_name“/> </xsl:template> </xsl:stylesheet>
Stylesheet – Example Output <html> <head> <title>Famous Scientists</title> </head> <body> <dl> <dt> Alan Turing </dt> <dd> <ul> <li>Born: 1912</li> <li>Died: 1954</li> </ul> </dd> ... </dl> </body </html>
XSL – More features • Uses XPath functions • If-statements, For each loops • Parameters for templates • => Query language
XPath • Identifies particular parts of XML documents • Navigate on the tree structures of XML documents like in a UNIX file system • Many built-in functions • e.g.:round, starts-with, substring, ... • Used by XSLT, XPointer
XPath – Example <xsl:template match=„people“> <xsl:apply-template select=„person“/> </xsl:template> <xsl:template match=„person“> <xsl:value-of select=„name“/> </xsl:template>
XPath – Example (2) <xsl:template match=„person“> <xsl:apply-templates select=„@*“/> </xsl:template> <xsl:template match=„last_name|profession“> <xsl:value-of select=„.“/> </xsl:template>
XPath – Example (3) /people/person/name/first_name //last_name/../first_name <xsl:template match=„//profession[.=‚Computer Scientist‘]“> <xsl:apply-template select=„//person[@born<=1980“/>
XLink • Simple Links • Like Links in HTML • Extended Links • More than one target possible • Directed labeled graph • Representation undecided yet • Arcs • Links between other recources
XLink – Simple Link Example <?xml version=„1.0“?> <novel xmlns:xlink=„http://www.w3.org/1999/xlink“ xlink:type=„simple“ xlink:href=„ftp://someserver.com/books/novels/wizoz.txt“ xlink:actuate=„onRequest“ xlink:show=„replace“ xlink:title=„A Link to the wonderful wizard ...“ xlink:role=„http://moreinfo.com“> <title>The wonderful wizard of oz</title> <author>Frank Baum</author> </novel>
XLink – Extended Link Example <?xml version=„1.0“?> <novel xmlns:xlink=„http://www.w3.org/1999/xlink“ xlink:type=„extended“> <title>The wonderful wizard of oz</title> <author>Frank Baum</author> <edition xlink:type=„locator“ xlink:href=„urn:isbn:123456“ xlink:title=„William Morrow“ xlink:role=„http://www.williammorrow.com/“ xlink:label=„ISBN123456“/> <edition xlink:type=„locator“ xlink:href=„urn:isbn:234567“ xlink:title=„Oxford University Press“ xlink:role=„http://www.oup-usa.org/“ xlink:label=„ISBN234567“/> </novel>
XLink – Arcs Example <?xml version=„1.0“?> <series xlink:type=„extended“ xmlns:xlink=„http://www.w3.org/1999/xlink“> <author>Frank Baum</author> <!–- locator elements --> <novel xlink:type=„locator“ xlink:label=„oz1“ xlink:href=„ftp://someserver.com/wizoz.txt“> <title>The wonderful wizard of oz</title> </novel> <novel xlink:type=„locator“ xlink:label=„oz2“ xlink:href=„ftp://someserver.com/ozland.txt“> <title>The marvelous land of oz</title> </novel> <novel xlink:type=„locator“ xlink:label=„oz3“ xlink:href=„ftp://someserver.com/ooz.txt“> <title>Ozma of oz</title> </novel>
XLink – Arcs Example (2) <!–- arcs --> <next xlink:type=„arc“ xlink:from=„oz1“ xlink:to=„oz2“ /> <next xlink:type=„arc“ xlink:from=„oz2“ xlink:to=„oz3“ /> <next xlink:type=„arc“ xlink:from=„oz2“ xlink:to=„oz1“ /> <next xlink:type=„arc“ xlink:from=„oz3“ xlink:to=„oz2“ /> </series> The wonderful world of oz Ozma of oz The marvelous land of oz
XPointer • Locates points in XML documents • Based on XPath In XLinks: http://www.someserver.com/xml/people.xml#xpointer (//name[position()=1]) xpointer(/) to the root xpointer(//first_name) Xpointer(//profession[.=„physicist“])
References • XML in a NutshellElliotte Rusty Harold & W. Scott MeansO‘Reilly • Professional XML DatabasesKevin WilliamsWROX • http://xml.coverpages.org