Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transforming XML XMLNamespaces, XSLT. XML Namespaces Sometimes it is necessary to mix XML elements –Different types of content –Use of markup to convey.

Similar presentations


Presentation on theme: "Transforming XML XMLNamespaces, XSLT. XML Namespaces Sometimes it is necessary to mix XML elements –Different types of content –Use of markup to convey."— Presentation transcript:

1 Transforming XML XMLNamespaces, XSLT

2 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

3 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

4 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)

5 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

6 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

7 example 2: xml with multiple namespaces <html xmlns=http://www.w3.org/1999/xhtml xmlns:xlink=http://www.w3.org/1999/xlink> Three Namespaces Ellipse and Rectangle xlink prefix associated to the xlink namespace everywhere within the root element xhtml namespace associated to root html element and all descendants (no prefix needed) all (blue) elements are in the xhtml namespace xml declaration

8 xml with multiple namespaces <svg xmlns=http://www.w3.org/2000/svg width = 12cm height = 10cm> <rect x=4cm y=1cm width=3cm height=6cm> svg namespace associated to root svg element and all descendants (no prefix needed) all (red) elements are in the svg namespace

9 xml with multiple namespaces <p xlink:type=simple xlink:href=ellipses.html> More about ellipses <p xlink:type=simple xlink:href=rectangles.html> More about rectangles Last Modified 7th October 2003 all (blue) elements are in the xhtml namespace prefixed QNamed attributes (green) are in the xlink namespace

10 more on namespaces namespace can be defined in the element where it is used or in the root namespaces are identified by the URI, not the prefix used in a particular document the parser doesnt look up the URI – it is only there as a unique identifier!

11 more on namespaces namespaces are completely independent of DTDs QNames, if used, must be defined as elements in the DTD for them to be valid –parameter entities are used to get round this ingenious but awkward kludge not required for this module! namespaces important in XSLT documents

12 Introduction to XSLT

13 what is XSL? XML & client/server model –XML sits on server but does not do anything –XSL provides client views of data XSL: eXtensible Stylesheet Language –two separate namespaces XSL-FO (Formatting Objects) XSLT (Transformations) –X-Path used to navigate XML defines rules for transforming a source XML document into a target document

14 what is XSLT? Transforms source tree to results tree by: –Selecting elements –Selecting attributes –Rearranging elements –Sorting elements –Applying conditional tests XML/XSTL Similar to HTML/CSS

15 the XSLT transformation process XSLT document XSLT processor XML source output document set of template rules match elements and replace using template rules

16 a simple XSLT example the source

17 Alan Turing computer scientist mathematician cryptographer

18 Richard P Feynman physicist playing the bongoes

19 a simple XSLT example the transforming stylesheet

20 <xsl:stylesheet version = 1.0 xmlns:xsl= http ://www.w3.org/1999/XSL/Transform> 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)

21 a simple XSLT example the output of the transform

22 Alan Turing computer scientist mathematician cryptographer Richard P Feynman physicist playing the bongoes 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) to modify the default behaviour, we add template rules that describe how to transform elements of the source document

23 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

24 example 2 <xsl:stylesheet version = 1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> A Person

25 Alan Turing computer scientist mathematician cryptographer Richard P Feynman physicist playing the bongoes

26 example 2 output A Person Each person element in the original document has been replaced entirely by the template. The whitespace outside each person element has been preserved

27 example 3 <xsl:stylesheet version = 1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> A Person elements used in a template must preserve well-formedness of the document

28 example 3 output A Person The and tags have also been copied over from the template The whitespace outside each person element has been preserved

29 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

30 example 4 <xsl:stylesheet version = 1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>

31 Alan Turing computer scientist mathematician cryptographer Richard P Feynman physicist playing the bongoes

32 example 4 output Alan Turing Richard P Feynman the full text content of the element after the,, and tags have been stripped out The whitespace inside each name element has been preserved along with the rest of the text content

33 example 4a <xsl:stylesheet version = 1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> -

34 example 4a output 1912 - Alan Turing 1918 - Richard P Feynman the value of attribute associated with element is added to the output

35 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

36 example 5 <xsl:stylesheet version = 1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>,

37 example 5 output Turing, Alan Feynman, Richard The order of processing has been changed. 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. The and elements are never processed because bypasses them

38 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)

39 Stylesheet Example – XML (catalog.xml) Empire Burlesque Bob Dylan USA Columbia 10.90 1985 ESSSSSSSSS Bruce Uk Cola 12.90 1988

40 Stylesheet Example –XSL (cdcatalog.xsl) My CD Collection Title: Artist:

41 Stylesheet Example –Linking XML to Stylesheet Empire Burlesque Bob Dylan USA Columbia 10.90 1985 ESSSSSSSSS Bruce Uk Cola 12.90 1988

42 My CD Collection Title: Empire Burlesque Artist: Bob Dylan Title: ESSSSSSSSS Artist: Bruce Stylesheet Example – HTML Output

43 Summary Namespaces – allow elements from different XML languages to be included in same XML –xmlns:xlink=http://www.w3.org/1999/xlink XSLT – –Create templates with –Select content with –Control content with

44 Useful websites Standards: –www.w3.org/Style/XSL/- administrates xsl std www.w3.org/TR/xslt www.w3.org/TR/xpath www.w3.org/TR/xsl/ Tutorials/Forums –www.w3schools.com/xsl –www.learn-xslt-tutorial.com/ –www.xml.com –www.tizag.com/xmlTutorial/xslttutorial.php


Download ppt "Transforming XML XMLNamespaces, XSLT. XML Namespaces Sometimes it is necessary to mix XML elements –Different types of content –Use of markup to convey."

Similar presentations


Ads by Google