Xslt by asfak mahamud

Preview:

DESCRIPTION

Seminar Slide.

Citation preview

Apr 10, 2023Apr 10, 2023 11

XSLTXSLT

Md. Asfak MahamudMd. Asfak Mahamud

Associate Software EngineerAssociate Software EngineerKAZ Software Ltd.KAZ Software Ltd.

Dhaka, BangladeshDhaka, Bangladesh

February 19, 2008February 19, 2008

Apr 10, 2023 2

AgendaAgenda

► TrailerTrailer

► CastCast XMLXML XPathXPath XSL/XSLTXSL/XSLT

► DirectorDirector XSLT ProcessorsXSLT Processors Java APIJava API

► ProducerProducer TransformationTransformation

► ProductionProduction Html/XHtmlHtml/XHtml XMLXML TextText

► DiscussionDiscussion

James Stewart Donna Reed Lionel Barrymore

Frank Capra

It’s Wonderful Life

Liberty Film Inc.

Apr 10, 2023 3

AgendaAgenda

► TrailerTrailer

► CastCast XMLXML XPathXPath XSL/XSLTXSL/XSLT

► DirectorDirector XSLT ProcessorsXSLT Processors Java APIJava API

► ProducerProducer TransformationTransformation

► ProductionProduction Html/XHtmlHtml/XHtml XMLXML TextText

► DiscussionDiscussion

James Stewart Donna Reed Lionel Barrymore

Frank Capra

It’s Wonderful Life

Liberty Film Inc.

Apr 10, 2023 4

XSLTprocessor

Trailer (continued)Trailer (continued)

hello.xml

hello.xsl

hello.html

Apr 10, 2023 5

XSLTprocessor

Trailer (continued)Trailer (continued)

Apr 10, 2023 6

Transform.java

Trailer (continued)Trailer (continued)

Apr 10, 2023 7

Trailer (continued)Trailer (continued)

Streamsource of XSL File

XSL File

StreamSource of XML File

XML File

transformer

StreamResultof Html File

Apr 10, 2023 8

Trailer (continued)Trailer (continued)

Streamsource of XSL File

XSL File

StreamSource of XML File

XML File

StreamResultof Html File

Html FileStreamSource

of XML File

StreamResultof Html File

Apr 10, 2023 9

Trailer (continued)Trailer (continued)

TransformerFactory tFactory = TransformerFactory.newInstance();

StreamSource xslStreamSource = new StreamSource(xslDoc);

Transformer transformer = tFactory.newTransformer(stylesource);

StreamSource xmlStreamSource = new StreamSource(new File(xmlDoc));

StreamResult htmlStreamResult = new StreamResult(new FileOutputStream(htmlDoc));

transformer.transform(xmlStreamSource, htmlStreamResult);

Apr 10, 2023 10

AgendaAgenda

► TrailerTrailer

► CastCast XMLXML XPathXPath XSL/XSLTXSL/XSLT

► DirectorDirector XSLT ProcessorsXSLT Processors Java APIJava API

► ProducerProducer TransformationTransformation

► ProductionProduction Html/XHtmlHtml/XHtml XMLXML TextText

► DiscussionDiscussion

James Stewart Donna Reed Lionel Barrymore

Frank Capra

It’s Wonderful Life

Liberty Film Inc.

Apr 10, 2023 11

CastCast XML XML

► stands for Estands for EXXtensible tensible MMarkup arkup LLanguage.anguage.► was designed to was designed to transporttransport and and storestore data. data.

Apr 10, 2023 12

CastCast XML XML

Apr 10, 2023 13

CastCast XML XML

Apr 10, 2023 14

CastCast XML XML with CSSwith CSS

Apr 10, 2023 15

CastCast XML XML with XSLwith XSL

Apr 10, 2023 16

CastCast

XPathXPath

Apr 10, 2023 17

CastCast XPath XPath

Apr 10, 2023 18

CastCast XPath XPath

Apr 10, 2023 19

CastCast XPath XPath

Apr 10, 2023 20

CastCast XPath XPath

Apr 10, 2023 21

CastCast XPath XPath

Apr 10, 2023 22

CastCast XPath XPath

Apr 10, 2023 23

CastCast XPath XPath

Apr 10, 2023 24

CastCast

XSL/XSLTXSL/XSLT

Apr 10, 2023 25

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 26

CastCast XSL/XSLT XSL/XSLT

►XSLT elements………………..XSLT elements………………..

Apr 10, 2023 27

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 28

CastCast XSL/XSLT XSL/XSLT FunctionsFunctions

• For the functions required for XPath 2.0, XQuery 1.0 and XSLT 2.0.

http://www.w3schools.com/xpath/xpath_functions.asp

Apr 10, 2023 29

CastCast XSL/XSLT XSL/XSLT

• Simplest

Apr 10, 2023 30

CastCast XSL/XSLT XSL/XSLT

• Adding some style

Apr 10, 2023 31

CastCast XSL/XSLT XSL/XSLT

• use of <xsl:apply-templates> element

Apr 10, 2023 32

CastCast XSL/XSLT XSL/XSLT

• Getting location paths……..

Apr 10, 2023 33

► mode in <xsl:apply-templates>mode in <xsl:apply-templates>

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 34

► If we need variable and If we need variable and ► for loopfor loop

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 35

► If we need conditionsIf we need conditions

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 36

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 37

► <xsl-apply-imports> works only for templates imported with xsl:import, not <xsl-apply-imports> works only for templates imported with xsl:import, not for templates included with xsl:includefor templates included with xsl:include

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 38

1.1. Extension ElementsExtension Elements

2.2. Extension FunctionExtension Function

3.3. Fallback ProcessingFallback Processing

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 39

Two Important Functions:

Boolean element-available(string)

Boolean function-available(string)

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 40

CastCast XSL/XSLT XSL/XSLT Extention-Elements Extention-Elements

Apr 10, 2023 41

Example: Generating multiple output file

Apr 10, 2023 42

CastCast XSL/XSLT XSL/XSLT Extention FunctionExtention Function

► If we want to compute a path expression on the flyIf we want to compute a path expression on the fly

<xsl:variable name="element">foo/bar</xsl:variable><xsl:variable name="element">foo/bar</xsl:variable>

wrong: wrong: <xsl:apply-templates select="$element"/> <xsl:apply-templates select="$element"/>

► saxon:evaluate() extension function:saxon:evaluate() extension function:

<xsl:apply-templates select="<xsl:apply-templates select="saxon:evaluatesaxon:evaluate($element)"/>($element)"/>

Apr 10, 2023 43

To write a stylesheet that will work with multiple XSLT processors?

<xsl:stylesheet version="1.0" xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"

xmlns:saxon="http://icl.com/saxon"

xmlns:xt="http://www.jclark.com/xt"

extension-element-prefixes="redirect saxon xt">

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 44

Writing extensions in other languages

CastCast XSL/XSLT XSL/XSLT

<lxslt:component prefix="javascript-extension“ functions="cos sin toRadians"> <lxslt:script lang="javascript"> function cos(d) { return Math.cos(d); } function sin(d) { return Math.sin(d); } function toRadians(d) { return d * Math.PI / 180; } </lxslt:script>

</lxslt:component>

Apr 10, 2023 45

Fallback Processing

CastCast XSL/XSLT XSL/XSLT

Apr 10, 2023 46

AgendaAgenda

► TrailerTrailer

► CastCast XMLXML XPathXPath XSL/XSLTXSL/XSLT

► DirectorDirector XSLT ProcessorsXSLT Processors Java APIJava API

► ProducerProducer TransformationTransformation

► ProductionProduction Html/XHtmlHtml/XHtml XMLXML TextText

► DiscussionDiscussion

James Stewart Donna Reed Lionel Barrymore

Frank Capra

It’s Wonderful Life

Liberty Film Inc.

Apr 10, 2023 47

DirectorDirector XSLT Processors XSLT Processors

► Apache XalanApache Xalanhttp://xml.apache.org/xalanhttp://xml.apache.org/xalan

► SAXONSAXONhttp://saxon.sourceforge.nethttp://saxon.sourceforge.net

► Microsoft’s XML Parser 4.0 (MSXML)Microsoft’s XML Parser 4.0 (MSXML)http://www.microsoft.com/xmlhttp://www.microsoft.com/xml

Apr 10, 2023 48

DirectorDirector Java API Java API

► JDK 1.4 contains all necessary classesJDK 1.4 contains all necessary classes javax.xml.transform package.javax.xml.transform package.

► Lower versions require downloading XSLT processor and SAX Lower versions require downloading XSLT processor and SAX parser. parser.

Apr 10, 2023 49

ProducerProducer Transformation Transformation

Apr 10, 2023 50

AgendaAgenda

► TrailerTrailer

► CastCast XMLXML XPathXPath XSL/XSLTXSL/XSLT

► DirectorDirector XSLT ProcessorsXSLT Processors Java APIJava API

► ProducerProducer TransformationTransformation

► ProductionProduction Html/XHtmlHtml/XHtml XMLXML TextText

► DiscussionDiscussion

James Stewart Donna Reed Lionel Barrymore

Frank Capra

It’s Wonderful Life

Liberty Film Inc.

Apr 10, 2023 51

ProductionProduction

► Stage setup and action !!Stage setup and action !!

Let’s see some demos…………Let’s see some demos…………

Apr 10, 2023 52

AgendaAgenda

► TrailerTrailer

► CastCast XMLXML XPathXPath XSL/XSLTXSL/XSLT

► DirectorDirector XSLT ProcessorsXSLT Processors Java APIJava API

► ProducerProducer TransformationTransformation

► ProductionProduction Html/XHtmlHtml/XHtml XMLXML TextText

► DiscussionDiscussion

James Stewart Donna Reed Lionel Barrymore

Frank Capra

It’s Wonderful Life

Liberty Film Inc.

Apr 10, 2023 53

DiscussionDiscussion

► Something from XSLT 2.0 Something from XSLT 2.0 GroupingGrouping Function DefinitionsFunction Definitions Result DocumentsResult Documents TypingTyping

Apr 10, 2023 54

Discussion Discussion XSLT 2.0 - GroupingXSLT 2.0 - Grouping

<paper> <title>XML and PDF in Publishing Workflows</title> <author>Myers, Charles</author></paper><paper> <title>On the Way to XML</title> <author>Parsons, Jonathan</author> <author>Caisley, Phil</author></paper>

<author> <name>Caisley, Phil</name> <paper>On the Way to XML</paper></author><author> <name>Myers, Charles</name> <paper>XML and PDF in Publishing Workflows</paper></author><author> <name>Parsons, Jonathan</name> <paper>On the Way to XML</paper></author>

Apr 10, 2023 55

Discussion Discussion XSLT 2.0 - GroupingXSLT 2.0 - Grouping

► Usually Muenchian Method (keys) in XSLT 1.0 is used.Usually Muenchian Method (keys) in XSLT 1.0 is used.

► XSLT 2.0 has XSLT 2.0 has <xsl:for-each-group><xsl:for-each-group> select select attribute identifies items to groupattribute identifies items to group grouping by value calculates grouping keygrouping by value calculates grouping key

►group-by group-by groups all itemsgroups all items►group-adjacent group-adjacent groups adjacent itemsgroups adjacent items

grouping in sequence identifies start/end of groupgrouping in sequence identifies start/end of group►group-starting-with group-starting-with identifies start of groupidentifies start of group►group-ending-with group-ending-with identifies end of group identifies end of group

Use Use current-group()current-group() to get members of current group, to get members of current group, current-current-grouping-key()grouping-key() to get value for current group to get value for current group

Apr 10, 2023 56

<paper> <title>XML and PDF in Publishing Workflows</title> <author>Myers, Charles</author></paper><paper> <title>On the Way to XML</title> <author>Parsons, Jonathan</author> <author>Caisley, Phil</author></paper>

<author> <name>Caisley, Phil</name> <paper>On the Way to XML</paper></author><author> <name>Myers, Charles</name> <paper>XML and PDF in Publishing Workflows</paper></author><author> <name>Parsons, Jonathan</name> <paper>On the Way to XML</paper></author>

<xsl:for-each-group select="paper" group-by="author"> <xsl:sort select="current-grouping-key()" /> <author> <name> <xsl:value-of select="current-grouping-key()" /> </name> <xsl:for-each select="current-group()"> <paper> <xsl:value-of select="title" /> </paper> </xsl:for-each> </author>

</xsl:for-each-group>

Apr 10, 2023 57

Discussion Discussion XSLT 2.0 - Function DefinitionsXSLT 2.0 - Function Definitions

Apr 10, 2023 58

Discussion Discussion XSLT 2.0 - XSLT 2.0 - Multiple Result DocumentsMultiple Result Documents

► Many XSLT 1.0 processors have extension elements to create Many XSLT 1.0 processors have extension elements to create multiple output documentsmultiple output documents

► XSLT 2.0 has XSLT 2.0 has <xsl:result-document><xsl:result-document>::

<xsl:for-each select="section"> <xsl:result-document href="{@id}.html"> <xsl:apply-templates select="." mode="html" /> </xsl:result-document></xsl:for-each>

Apr 10, 2023 59

Discussion Discussion XSLT 2.0 - XSLT 2.0 - TypingTyping

► XPath 2.0 is strongly typedXPath 2.0 is strongly typed

the type of a value determines how it is treated by some the type of a value determines how it is treated by some operators (e.g. +, =)operators (e.g. +, =)

if the wrong type of value is passed to a function, you will if the wrong type of value is passed to a function, you will get an errorget an error

► Similarly, in XSLT 2.0:Similarly, in XSLT 2.0: the type of a sort key determines how values are sortedthe type of a sort key determines how values are sorted if the wrong type of value is passed as a parameter to a if the wrong type of value is passed as a parameter to a

template, you will get an errortemplate, you will get an error

Apr 10, 2023 60

Discussion Discussion XSLT 2.0 - XSLT 2.0 - TypingTyping

► Declare the type of variables and parameters with Declare the type of variables and parameters with asas attributeattribute holds a SequenceTypeholds a SequenceType

► item test plus occurrence indicatoritem test plus occurrence indicator►xs:integer+xs:integer+ means "one or more integers" means "one or more integers"►element()?element()? means "an optional element" means "an optional element"

► Error if value doesn't comply with typeError if value doesn't comply with type

<xsl:function name="math:power"> <xsl:param name="base" as="xs:decimal" /> <xsl:param name="power" as="xs:integer" /> …</xsl:function>

Apr 10, 2023 61

And at last…………And at last…………

Apr 10, 2023 62

DiscussionDiscussionXSLT Advantages & DisadvantagesXSLT Advantages & Disadvantages

Advantages:Advantages:► Easy display formatted XML Easy display formatted XML

data in browser.data in browser.

► Easier to modify when XML Easier to modify when XML data format changes than to data format changes than to modify DOM and SAX modify DOM and SAX parsing code.parsing code.

► Can be used with database Can be used with database queries that return XML. queries that return XML.

Disadvantages:Disadvantages:► Memory intensive, Memory intensive,

performance hit.performance hit.

► Difficult to implement Difficult to implement complex business rules.complex business rules.

► Have to learn new Have to learn new language.language.

Apr 10, 2023 63

ReferencesReferences

► Features of XSLT 2.0Features of XSLT 2.0 Jeni Tennison Jeni Tennison http://www.jenitennison.comhttp://www.jenitennison.com

► Most of the xslt and xpath examples are fromMost of the xslt and xpath examples are from http://www.zvon.orghttp://www.zvon.org

► Above AllAbove All http://www.w3.org/Style/XSL/http://www.w3.org/Style/XSL/ http://www.w3schools.com/xsl/default.asphttp://www.w3schools.com/xsl/default.asp http://www.w3schools.com/xpath/default.asphttp://www.w3schools.com/xpath/default.asp

Apr 10, 2023 64

Recommended