22
A peek into the world of XSLT Author Neenu George

Xslt tutorial

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Xslt tutorial

A peek into the world of

XSLT• Author

• Neenu George

Page 2: Xslt tutorial

“Style is what gives value and currency to thoughts.”

Arthur Schopenhauer (1788-1860) German philosopher.

“Style is a simple way of saying complicated things”

Jean Cocteau (1889-1963) French author and filmmaker.

“The proper words in the proper places are the true definition of style.”

Jonathan Swift (1667-1745) Irish-born English satirist.

Page 3: Xslt tutorial

AGENDA

• What is XSLT ?• Programming with XSLT • Example• Use of XSLT in Polaris Application• Conclusion• Q&A

Page 4: Xslt tutorial

About XSLT

Page 5: Xslt tutorial

What is XSLT?XSL stands for EXtensible Stylesheet LanguageLanguage for transforming XML documentsA programming language for XML documentsA functional language, based on value substitutionAugmented with pattern matchingAnd also template substitution to construct output (based on namespacesUses XML syntax

Page 6: Xslt tutorial

XSLT in Motion

• Portals / web sites

• Standardization

• Games

• Document Management

• Enterprise Integrations

• RSS, SVG, UBL, LegalXML, HrXML, XBRL

• And its Everywhere ……………

Page 7: Xslt tutorial

Why transform?

• Convert one schema to another– I say potato, you say paragraph

• Rearrange data for formatting

Page 8: Xslt tutorial

Programming with XSLT

Page 9: Xslt tutorial

Correct Style Sheet Declaration

The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>

W3C XSLT Recommendation is:

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

Page 10: Xslt tutorial

<xsl:template name="myTemplateName"> ...body of template... </xsl:template>

set of rules that are called templatesA template contains rules to apply when a specified node is matched.Acts as functions in java<xsl:call-template name="myTemplateName"/>

XSLT <xsl:template> Element

Page 11: Xslt tutorial

used to extract the value of a selected node.<xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation<xsl:value-of select="XPath expression"/>selects the contents of an element and adds it to the output stream 

XSLT <xsl:value-of> Element

Page 12: Xslt tutorial

element allows you to do looping in XSLT

<xsl:for-each> element can be used to select every XML element of a specified node-set<xsl:for-each select="XPath expression"> 

XSLT <xsl:foreach> Elements

Page 13: Xslt tutorial

XSLT <xsl:sort> Element

<xsl:sort> element is used to sort the output.To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file:

xsl:sort select="author"/>

Page 14: Xslt tutorial

XSLT <xsl:if> Element<xsl:if> element is used to put a conditional test against the content of the XML file

test against the content of the XML file, add an <xsl:if> element to the XSL document.

<xsl:if test="expression">  <!-- Content: template --></xsl:if>

Page 15: Xslt tutorial

XSLT <xsl:choose> Element

The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.

<xsl:choose>  <xsl:when test="expression">    ... some output ...  </xsl:when>  <xsl:otherwise>    ... some output ....  </xsl:otherwise></xsl:choose>

Page 16: Xslt tutorial

XPath and its use in XSLT

• An expression language over XML trees• Used to identify sets of elements

– “all paragraphs”– “all paragraphs directly inside footnotes”– “the section with ID=“sec37”– “footnotes with author=‘Knuth’”– “first paragraph in each section”– “the parent of each caption”

• Then you can say what to do with them…

Page 17: Xslt tutorial

What’s inside an XSLT transform?

• Any number of “templates”• A template uses Xpath to match nodes• Highest priority matching template selected• Then the remplate takes over and

generates:– Literal output XML (based on

namespace)– Computational results (of XSLT functions)– Results of further template applications– Results of queries on the document

• Many options

Page 18: Xslt tutorial

Code Examples

Page 19: Xslt tutorial

Conclusion

Page 20: Xslt tutorial

Strategies for XSLT

Try to pick a single style as much as possible

May vary by project

Mixing may be necessary but can get confusing

Be sure you understand (and probably override the default rules)

Shorter patterns are better

<xsl:value-of> and <xsl:if> may be easier to deal with than a complex path

Page 21: Xslt tutorial

References

Key siteshttp://www.w3.org/Style/XSL http://www.mulberrytech.com/xsl/xsl-list http://www.oasis-open.org/cover/xsl.html

Interactive XSLT reference http://www.zvon.org/xxl/XSLTreference/Output/

XSLT: 2nd Edition Programmer’s Reference Michael Kay [Good reference; clear, but not really a tutorial]

XSLT & XPath On the EdgeTennison [And her other books]

Page 22: Xslt tutorial