SlideShare uma empresa Scribd logo
1 de 32
XML in a
Nutshell
Outline
• XML Basics
• Displaying XML with CSS
• Transforming XML with XSLT
• Serving XML to Web Users
• Resources
• Tips & Advice
Documents
• XML is expressed as “documents”,
whether an entire book or a database
record
• Must haves:
– At least one element
– Only one “root” element
• Should haves:
– A document type declaration; e.g., <?xml
version="1.0"?>
– Namespace declarations
• Can haves:
– One or more properly nested elements
Elements
• Must have a name; e.g., <title>
• Names must follow rules: no spaces or special
characters, must start with a letter, are case
sensitive
• Must have a beginning and end; <title></title>
or <title/>
• May wrap text data; e.g., <title>Hamlet</title>
• May have an attribute that must be quoted;
e.g., <title level=“main”>Hamlet</title>
• May contain other “child” elements; e.g.,
<title level=“main”>Hamlet <subtitle>
Prince of Denmark</subtitle></title>
Element Relationships
• Every XML document must have only
one “root” element
• All other elements must be contained
within the root
• An element contained within another
tag is called a “child” of the container
element
• An element that contains another tag is
called the “parent” of the contained
element
• Two elements that share the same
The Tree
<?xml version="1.0"?>
<book>
<author>
<lastname>Tennant</lastname>
<firstname>Roy</firstname>
</author>
<title>The Great American Novel</title>
<chapter number=“1”>
<chaptitle>It Was Dark and Stormy</chaptitle>
<p>It was a dark and stormy night.</p>
<p>An owl hooted.</p>
</chapter>
</book>
Root element
Parent of <lastname>
Siblings
Child of <author>
Comments & Processing
Instructions
• You can embed comments in your XML
just like in HTML:
<!-- Whatever is here (whether text or
markup) will be ignored on processing -->
• A processing instruction tells the XML
parser information it needs to know to
properly process an XML document:
<?xml-stylesheet type="text/css"
href="style2.css"?>
Well-Formed XML
• Follows general tagging rules:
– All tags begin and end
• But can be minimized if empty: <br/> instead of
<br></br>
– All tags are case sensitive
– All tags must be properly nested:
• <author> <firstname>Mark</firstname>
<lastname>Twain</lastname> </author>
– All attribute values are quoted:
• <subject scheme=“LCSH”>Music</subject>
• Has identification & declaration tags
Valid XML
• Uses only specific tags and rules as
codified by one of:
– A document type definition (DTD)
– A schema definition
• Only the tags listed by the schema or DTD
can be used
• Software can take a DTD or schema and
verify that a document adheres to the
rules
• Editing software can prevent an author
from using anything except allowed tags
Namespaces
• A method to keep metadata elements
from different schemas from colliding
• Example: the tag <name> may have a
very different meaning in different
standards
• A namespace declaration specifies from
which specification a set of tags is
drawn
<mets xmlns="http://www.loc.gov/METS/" xsi:schemaLocation=
"http://www.loc.gov/standards/mets/mets.xsd">
Character Encoding
• XML is Unicode, either UTF-8 or UTF-
16
• However, you can output XML into
other character encodings (e.g., ISO-
Latin1)
• Use <![CDATA[ ]]> to wrap any
special characters you don’t want to
be treated as markup (e.g., &nbsp;)
Displaying XML: CSS
• A modern web browser (e.g., MSIE, Mozilla) and
a cascading style sheet (CSS) may be used to
view XML as if it were HTML
• A style must be defined for every XML tag, or
the browser displays it in a default mode
• All display characteristics of each element must
be explicitly defined
• Elements are displayed in the order they are
encountered in the XML
• No reordering of elements or other
processing is possible
Displaying XML with CSS
• Must put a processing instruction at the
top of your XML file (but below the XML
declaration):
<?xml-stylesheet type="text/css"
href="style.css"?>
• Must specify all display characteristics
of all tags, or it will be displayed in
default mode (whatever the browser
wants)
CSS Demonstration
XML
Doc
Web Server
Cascading
Stylesheet
(CSS)
Transforming XML: XSLT
• XML Stylesheet Language —
Transformations (XSLT)
• A markup language and programming
syntax for processing XML
• Is most often used to:
– Transform XML to HTML for delivery to
standard web clients
– Transform XML from one set of XML tags to
another
– Transform XML into another syntax/system
XLST Primer
• XSLT is based on the process of
matching templates to nodes of the
XML tree
• Working down from the top, XSLT tries
to match segments of code to:
– The root element
– Any child node
– And on down through the document
• You can specify different processing for
each element if you wish
XSLT Processing Model
XML DocXML Doc
Source TreeSource Tree
XML ParserXML Parser
Result TreeResult Tree
Trans-Trans-
formationformation
Format-Format-
tingting
XSLTXSLT
StylesheetStylesheet
FormattedFormatted
OutputOutput
From Professional XSL, Wrox Publishers
Nodes and XPath
• An XML document is a collection of
nodesnodes that can be identified, selected,
and acted upon using an XpathXpath
statementstatement
• Examples of nodes: root, element,
attribute, text
• Sample statement:
//article[@name=‘test’] = Select all <article>
elements of the root node that have a name
attribute with the value ‘test’
Templates
• An XSLT stylesheet is a collection of
templates that act against specified
nodes in the XML source tree
• For example, this template will be
executed when a <para> element is
encountered:
<xsl:template match="para">
<p><xsl:value-of select="."/></p>
</xsl:template>
Calling Templates
• A template can call other templates
• By default (tree processing):
<xsl:apply-templates/> [processes all
children of the current node]
• Explicitly:
<xsl:apply-templates select=“title”/>
[processes all <title> elements of the current
node]
<xsl:call-template name=“title”/>
[processes the named template, regardless of
the source tree]
XSLT Structures
• Decision:
– Choose: when you want an “otherwise”
(default) condition
– If: when you don’t need a default condition
• Looping:
– For-each: processes each selected node in
turn
XSLT Primer: Doing HTML
• Typical way to begin:
<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="title"/></title>
<link type="text/css" rel="stylesheet" href="xslt.css" />
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
• Then, templates for each element
appear below
XSLT Demonstration
XML
Doc
XML Processor
(xsltproc)
Web Server
XSLT
Stylesheet
XHTML
representation
Cascading
Stylesheet
(CSS)CGI script
XML vs. Databases
(a simplistic formula)
• If your information is…
– Tightly structured
– Fixed field length
– Massive numbers of individual items
• You need a database
• If your information is…
– Loosely structured
– Variable field length
– Massive record size
• You need XML
Serving XML to Web Users
• Basic requirements: an XML doc and a web
server
• Additional requirements for simple method:
– A CSS Stylesheet
• Additional requirements for complex, powerful
method:
– An XSLT stylesheet
– An XML parser
– XML web publishing software or an in-house CGI or
Java program to join the pieces
– A CSS stylesheet (optional) to control how
it looks in a browser
XML Web Publishing
Software
• Software used to add XML serving
capability to a web server
• Makes it easy to join XML
documents with XSLT to output
HTML for standard web browsers
• A couple examples, both free…
Requires a Java servlet
container such as Tomcat
(free) or Resin (commercial)
Requires mod_perl
http://texts.cdlib.org/escholarship/
XML & XSLT Resources
• Eric Morgan’s “Getting Started with
XML” a good place to begin
• Many good web sites, and Google
searches can often answer specific
questions you may have
• Be sure to join the XML4Lib
discussion
Tips and Advice
• Begin transitioning to XML now:
– XHTML and CSS for web files, XML for
static documents with long-term worth
– Get your hands dirty on a simple XML
project
• Do not rely on browser support of XML
• DTDs? We don’t need no stinkin’ DTDs!
• Buy my book! (just kidding…)
Contact Information
Roy Tennant
California Digital Library
roy.tennant@ucop.edu
http://roytennant.com/
510-987-0476

Mais conteúdo relacionado

Mais procurados (17)

Cascading style sheets
Cascading style sheetsCascading style sheets
Cascading style sheets
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
 
XML
XMLXML
XML
 
Querring xml with xpath
Querring xml with xpath Querring xml with xpath
Querring xml with xpath
 
XML - SAX
XML - SAXXML - SAX
XML - SAX
 
Html (1)
Html (1)Html (1)
Html (1)
 
XSL - XML STYLE SHEET
XSL - XML STYLE SHEETXSL - XML STYLE SHEET
XSL - XML STYLE SHEET
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
 
Markup Languages
Markup Languages Markup Languages
Markup Languages
 
DOM-XML
DOM-XMLDOM-XML
DOM-XML
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handling
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Xml
XmlXml
Xml
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
XML
XMLXML
XML
 
Xml schema
Xml schemaXml schema
Xml schema
 
XML Databases
XML DatabasesXML Databases
XML Databases
 

Destaque

Sos And Nano Rk A Comparative Study
Sos And Nano Rk A Comparative StudySos And Nano Rk A Comparative Study
Sos And Nano Rk A Comparative StudySudharsan S
 
XML Presentation-2
XML Presentation-2XML Presentation-2
XML Presentation-2Sudharsan S
 
Xml Presentation-3
Xml Presentation-3Xml Presentation-3
Xml Presentation-3Sudharsan S
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1Sudharsan S
 

Destaque (7)

Sos And Nano Rk A Comparative Study
Sos And Nano Rk A Comparative StudySos And Nano Rk A Comparative Study
Sos And Nano Rk A Comparative Study
 
XML Presentation-2
XML Presentation-2XML Presentation-2
XML Presentation-2
 
Xml plymouth
Xml plymouthXml plymouth
Xml plymouth
 
Xml11
Xml11Xml11
Xml11
 
Xml Presentation-3
Xml Presentation-3Xml Presentation-3
Xml Presentation-3
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
 
Xml1111
Xml1111Xml1111
Xml1111
 

Semelhante a Xml (20)

Unit3wt
Unit3wtUnit3wt
Unit3wt
 
Unit3wt
Unit3wtUnit3wt
Unit3wt
 
Xml
XmlXml
Xml
 
1 xml fundamentals
1 xml fundamentals1 xml fundamentals
1 xml fundamentals
 
Xml unit1
Xml unit1Xml unit1
Xml unit1
 
XML-Extensible Markup Language
XML-Extensible Markup Language XML-Extensible Markup Language
XML-Extensible Markup Language
 
Xml
XmlXml
Xml
 
5 xsl (formatting xml documents)
5   xsl (formatting xml documents)5   xsl (formatting xml documents)
5 xsl (formatting xml documents)
 
Introduction to XML.ppt
Introduction to XML.pptIntroduction to XML.ppt
Introduction to XML.ppt
 
Introduction to XML.ppt
Introduction to XML.pptIntroduction to XML.ppt
Introduction to XML.ppt
 
XML, DTD & XSD Overview
XML, DTD & XSD OverviewXML, DTD & XSD Overview
XML, DTD & XSD Overview
 
XML Schema
XML SchemaXML Schema
XML Schema
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
 
Xml
XmlXml
Xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
02 xml schema
02 xml schema02 xml schema
02 xml schema
 
xml.pptx
xml.pptxxml.pptx
xml.pptx
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
XML
XMLXML
XML
 
XSLT
XSLTXSLT
XSLT
 

Mais de Sudharsan S (20)

Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Unix
UnixUnix
Unix
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
Unix
UnixUnix
Unix
 
C Lecture
C LectureC Lecture
C Lecture
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
C Introduction
C IntroductionC Introduction
C Introduction
 
College1
College1College1
College1
 
C Programming
C ProgrammingC Programming
C Programming
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Preface
PrefacePreface
Preface
 
Toc Sg
Toc SgToc Sg
Toc Sg
 
Les08
Les08Les08
Les08
 
Les06
Les06Les06
Les06
 
Les07
Les07Les07
Les07
 
Les04
Les04Les04
Les04
 
Les05
Les05Les05
Les05
 
Les03
Les03Les03
Les03
 
Les02
Les02Les02
Les02
 
Les01
Les01Les01
Les01
 

Último

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Último (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

Xml

  • 2. Outline • XML Basics • Displaying XML with CSS • Transforming XML with XSLT • Serving XML to Web Users • Resources • Tips & Advice
  • 3. Documents • XML is expressed as “documents”, whether an entire book or a database record • Must haves: – At least one element – Only one “root” element • Should haves: – A document type declaration; e.g., <?xml version="1.0"?> – Namespace declarations • Can haves: – One or more properly nested elements
  • 4. Elements • Must have a name; e.g., <title> • Names must follow rules: no spaces or special characters, must start with a letter, are case sensitive • Must have a beginning and end; <title></title> or <title/> • May wrap text data; e.g., <title>Hamlet</title> • May have an attribute that must be quoted; e.g., <title level=“main”>Hamlet</title> • May contain other “child” elements; e.g., <title level=“main”>Hamlet <subtitle> Prince of Denmark</subtitle></title>
  • 5. Element Relationships • Every XML document must have only one “root” element • All other elements must be contained within the root • An element contained within another tag is called a “child” of the container element • An element that contains another tag is called the “parent” of the contained element • Two elements that share the same
  • 6. The Tree <?xml version="1.0"?> <book> <author> <lastname>Tennant</lastname> <firstname>Roy</firstname> </author> <title>The Great American Novel</title> <chapter number=“1”> <chaptitle>It Was Dark and Stormy</chaptitle> <p>It was a dark and stormy night.</p> <p>An owl hooted.</p> </chapter> </book> Root element Parent of <lastname> Siblings Child of <author>
  • 7. Comments & Processing Instructions • You can embed comments in your XML just like in HTML: <!-- Whatever is here (whether text or markup) will be ignored on processing --> • A processing instruction tells the XML parser information it needs to know to properly process an XML document: <?xml-stylesheet type="text/css" href="style2.css"?>
  • 8. Well-Formed XML • Follows general tagging rules: – All tags begin and end • But can be minimized if empty: <br/> instead of <br></br> – All tags are case sensitive – All tags must be properly nested: • <author> <firstname>Mark</firstname> <lastname>Twain</lastname> </author> – All attribute values are quoted: • <subject scheme=“LCSH”>Music</subject> • Has identification & declaration tags
  • 9. Valid XML • Uses only specific tags and rules as codified by one of: – A document type definition (DTD) – A schema definition • Only the tags listed by the schema or DTD can be used • Software can take a DTD or schema and verify that a document adheres to the rules • Editing software can prevent an author from using anything except allowed tags
  • 10. Namespaces • A method to keep metadata elements from different schemas from colliding • Example: the tag <name> may have a very different meaning in different standards • A namespace declaration specifies from which specification a set of tags is drawn <mets xmlns="http://www.loc.gov/METS/" xsi:schemaLocation= "http://www.loc.gov/standards/mets/mets.xsd">
  • 11. Character Encoding • XML is Unicode, either UTF-8 or UTF- 16 • However, you can output XML into other character encodings (e.g., ISO- Latin1) • Use <![CDATA[ ]]> to wrap any special characters you don’t want to be treated as markup (e.g., &nbsp;)
  • 12. Displaying XML: CSS • A modern web browser (e.g., MSIE, Mozilla) and a cascading style sheet (CSS) may be used to view XML as if it were HTML • A style must be defined for every XML tag, or the browser displays it in a default mode • All display characteristics of each element must be explicitly defined • Elements are displayed in the order they are encountered in the XML • No reordering of elements or other processing is possible
  • 13. Displaying XML with CSS • Must put a processing instruction at the top of your XML file (but below the XML declaration): <?xml-stylesheet type="text/css" href="style.css"?> • Must specify all display characteristics of all tags, or it will be displayed in default mode (whatever the browser wants)
  • 15. Transforming XML: XSLT • XML Stylesheet Language — Transformations (XSLT) • A markup language and programming syntax for processing XML • Is most often used to: – Transform XML to HTML for delivery to standard web clients – Transform XML from one set of XML tags to another – Transform XML into another syntax/system
  • 16. XLST Primer • XSLT is based on the process of matching templates to nodes of the XML tree • Working down from the top, XSLT tries to match segments of code to: – The root element – Any child node – And on down through the document • You can specify different processing for each element if you wish
  • 17. XSLT Processing Model XML DocXML Doc Source TreeSource Tree XML ParserXML Parser Result TreeResult Tree Trans-Trans- formationformation Format-Format- tingting XSLTXSLT StylesheetStylesheet FormattedFormatted OutputOutput From Professional XSL, Wrox Publishers
  • 18. Nodes and XPath • An XML document is a collection of nodesnodes that can be identified, selected, and acted upon using an XpathXpath statementstatement • Examples of nodes: root, element, attribute, text • Sample statement: //article[@name=‘test’] = Select all <article> elements of the root node that have a name attribute with the value ‘test’
  • 19. Templates • An XSLT stylesheet is a collection of templates that act against specified nodes in the XML source tree • For example, this template will be executed when a <para> element is encountered: <xsl:template match="para"> <p><xsl:value-of select="."/></p> </xsl:template>
  • 20. Calling Templates • A template can call other templates • By default (tree processing): <xsl:apply-templates/> [processes all children of the current node] • Explicitly: <xsl:apply-templates select=“title”/> [processes all <title> elements of the current node] <xsl:call-template name=“title”/> [processes the named template, regardless of the source tree]
  • 21. XSLT Structures • Decision: – Choose: when you want an “otherwise” (default) condition – If: when you don’t need a default condition • Looping: – For-each: processes each selected node in turn
  • 22. XSLT Primer: Doing HTML • Typical way to begin: <xsl:template match="/"> <html> <head> <title><xsl:value-of select="title"/></title> <link type="text/css" rel="stylesheet" href="xslt.css" /> </head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> • Then, templates for each element appear below
  • 23. XSLT Demonstration XML Doc XML Processor (xsltproc) Web Server XSLT Stylesheet XHTML representation Cascading Stylesheet (CSS)CGI script
  • 24. XML vs. Databases (a simplistic formula) • If your information is… – Tightly structured – Fixed field length – Massive numbers of individual items • You need a database • If your information is… – Loosely structured – Variable field length – Massive record size • You need XML
  • 25. Serving XML to Web Users • Basic requirements: an XML doc and a web server • Additional requirements for simple method: – A CSS Stylesheet • Additional requirements for complex, powerful method: – An XSLT stylesheet – An XML parser – XML web publishing software or an in-house CGI or Java program to join the pieces – A CSS stylesheet (optional) to control how it looks in a browser
  • 26. XML Web Publishing Software • Software used to add XML serving capability to a web server • Makes it easy to join XML documents with XSLT to output HTML for standard web browsers • A couple examples, both free…
  • 27. Requires a Java servlet container such as Tomcat (free) or Resin (commercial)
  • 30. XML & XSLT Resources • Eric Morgan’s “Getting Started with XML” a good place to begin • Many good web sites, and Google searches can often answer specific questions you may have • Be sure to join the XML4Lib discussion
  • 31. Tips and Advice • Begin transitioning to XML now: – XHTML and CSS for web files, XML for static documents with long-term worth – Get your hands dirty on a simple XML project • Do not rely on browser support of XML • DTDs? We don’t need no stinkin’ DTDs! • Buy my book! (just kidding…)
  • 32. Contact Information Roy Tennant California Digital Library roy.tennant@ucop.edu http://roytennant.com/ 510-987-0476