Title | XML and Java XSLT - Lecture slides |
---|---|
Author | Mohammad Ahmad |
Course | Enterprise Programming |
Institution | Manchester Metropolitan University |
Pages | 20 |
File Size | 596.3 KB |
File Type | |
Total Downloads | 29 |
Total Views | 135 |
Lecture slides...
Transforming XML XMLNamespaces, XSLT
1
XML Namespaces • Sometimes it is necessary to mix XML elements – Different types of content – Use of markup to convey meta-information
• Some documents combine markup from different XML languages • But: – Elements and attributes from different XML languages may share the same name – Need to group elements for processing 2
1
XML Namespaces • XML Namespaces is the xml standard for distinguishing xml elements • Namespaces are represented by attributes • Elements from the same namespace can be recognised by software as a group • Unique namespaces are defined by a URI
3
URL, URN, URI • URL: a Uniform Resource Locator • specifies the mechanism by which a resource is accessed • e.g. http://www.comp.rgu.ac.uk/home.html
• URN: a Uniform Resource Name • a unique sequence of characters naming an internet resource e.g. urn:Turquoise.Inflatable.Walrus • the name has persistence even if the resource becomes unavailable
• URI – uniform resource identifier • a URL or a URN (see RFC 2396 at www.ietf.org)
4
2
the namespace prefix • • • •
short string representing the namespace URI distinguishes element and attribute names defined using an xmlns:prefix attribute a prefixed element name is called a qualified name, or QName, or a raw name • QName syntax prefix:local_part 5
example • SVG and MathML both contain a set element • Both SVG and MathML can be embedded in XHTML documents • prefixes svg and mathml are used to distinguish the set elements distinct from 6
3
example 2: xml with multiple namespaces
xhtml namespace associated to root Thhtml element and all descendants (no prefix needed)
namespace everywhere within the root element Ellipse and Rectangle
21
Richard P Feynman
physicist playing the bongoes
22
11
a simple XSLT example the transforming stylesheet
23
• xsl prefix identifies xsl QNames as belonging in the XSLT namespace associated to the given URI • the empty stylesheet • contains no template rules • will apply default rules (see later) 24
12
a simple XSLT example the output of the transform
25
Alan Turing computer scientist mathematician cryptographer Richard
default behaviour strips out the mark up and returns a text document that reproduces the content of the XML (including whitespace like tabs and carriage returns)
P Feynman physicist playing the bongoes
to modify the default behaviour, we add template rules that describe how to transform elements of the source document
26
13
template rules • a template rule is defined by an element • the match attribute contains a pattern identifying the input to which the rule is applied • the content of the element is a template for the output from the matched pattern
template
27
example 2
A Person
28
14
example 2 output
A Person
Each person element in the original document has been replaced entirely by the template.
A Person
The whitespace outside each person element has been preserved
29
example 3
A Person
elements used in a template must preserve well-formedness of the document
30
15
example 3 output
A Person
The and tags have also been copied over from the template
A Person
The whitespace outside each person element has been preserved
31
xsl:value-of • xsl element which extracts the string value of an element in the source XML – the string value is the text content after: • all tags have been removed • entity and character references have been resolved
• select attribute specifies the element whose value is taken 32
16
example 4
33
example 4 output Alan Turing
the full text content of the element after the , , and tags have been stripped out
Richard
The whitespace inside each name element has been preserved along with the rest of the text content
P Feynman
34
17
• xsl element that can affect the default order of processing – which elements should be processed next – process elements in the middle of processing another element – prevent particular elements from being processed
• select attribute contains a pattern identifying elements to be processed at that point 35
example 5
,
36
18
example 5 output Turing,
The order of processing has been changed.
Alan
The output for each consists of the full text content of the , followed by a comma and a new line, followed by the full text content of the .
Feynman, Richard
The and elements are never processed because bypasses them 37
XSLT processor • a software component that • reads a XML source document and a stylesheet • applies the transformation rules • outputs the transformed document
• standalone • SAXON • Apache Xalan (used in NetBeans)
• built into a browser or application server • MSXML (built in to IE6) • Apache Cocoon (built in to Apache server) 38
19
You need special software to run XSLT • But you don’t have to buy them • Free open-source, shareware, as well as commercial • New ones all the time • Look for more at: http://www.xml.com – Saxon (http://users.iclway.co.uk/mhkay/saxon/ ) – Xalan XSLT (http://xml.apache.org/xalan/index.html ) – Unicorn XSLT Processor • (http://www.unicorn-enterprises.com/ )
– XSLT C library for Gnome (http://xmlsoft.org/XSLT/ ) 39
20...