Top Banner
Copyright 2001, ActiveState
51

Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Mar 27, 2015

Download

Documents

Avery Galloway
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Page 2: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

XSLT and Scripting Languages

or…XSLT: what is everyone so hot and bothered about?

Page 3: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Background: ActiveState

• ActiveState creates tools for open source and standards-based high-level languages:– Perl– Python– PHP– XSLT– Tcl

Page 4: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

What is XSLT?

• A language for transforming between XML vocabularies.

• Standardized by the World Wide Web Consortium

• Integrated with .NET, Apache, IE, Java, Oracle, Perl, Python, …

Page 5: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

XSLT

• XSLT is a slightly new twist:– Not a scripting language– Not, strictly-speaking, “open source”

• But not radically different:– Typically interpreted– Open source implementations– Not vendor dominated

Page 6: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Programming Language?

– XSLT has• looping constructs• function calling• variables• parameters• math functions• module combination• …

Page 7: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Looping in XSLT

• xsl:for-each lets us do the same thing for many nodes

• The body of the xsl:for-each is a full template

• It is instantiated once for each match

• Each time a different node is the "current" node

Page 8: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Example of Iterating: XML

<people>

<person><name>Paul Prescod</name>

<role>Snake Charmer</role>

</person>

<person><name>Gisle Aas</name>

<role>Camel Herder</role>

</person>

</people>

Page 9: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Example of Iterating: XSLT

<xsl:template match=“people"> <TABLE> <xsl:for-each select=“person"> <TR> <TD><xsl:value-of select=“name"/></TD> <TD><xsl:value-of select=“role"/></TD> </TR> </xsl:for-each> </TABLE> </xsl:template>

Page 10: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Page 11: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

HTML Output

<TABLE> <TR><TD>Paul Prescod</TD> <TD>Snake Charmer</TD> </TR> <TR><TD>Gisle Aas</TD> <TD>Camel Herder</TD> </TR></TABLE>

Page 12: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Calling Functions

• What if you wanted to do this in XSLT:

func myfunction(arg1, arg2){

…do something with params…

}

myfunction(“54”, “42”)

Page 13: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Functions, Parameters, Variables

<xsl:template name="mytemplate"> <xsl:param name="studio"/> <xsl:param name="everything"/> <life_the_universe_disco> <xsl:value-of select="$studio * $everything"/> </life_the_universe_disco></xsl:template>

Page 14: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Template Call

<xsl:template match="/"><xsl:call-template name="mytemplate">

<xsl:with-param name='studio' select="54"/>

<xsl:with-param name='everything' select="42"/>

</xsl:call-template> </xsl:template>

Page 15: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Page 16: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Bottom Line

• XSLT can be used to compute anything that can be computed

• It is “Turing complete”

• But….– Certain coding paradigms are VERY awkward– Non-textual Input/Output is typically not possible

Page 17: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Scripting Languages

• Scripting languages are designed to be general purpose

• “Modern” scripting languages go well beyond “scripting”

• They are general purpose multi-paradigm languages– But XSLT wins for specificity

Page 18: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

XSLT XML Support

• XSLT has deep native support for XML

• “Built-in”, highly consistent parser

• “XPath” XML navigation (“query”) language

• Special syntax for working with elements

Page 19: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

XML Recursion

• XSLT makes input-based recursion easy:– Sections within sections within …– Part descriptions within part descriptions …

• XSLT automatically selects the right rule to go with the right element in the input document

Page 20: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Context

• XSLT keeps track of context: namespaces, current node, current node list etc.

• Relevant contexts are in both the stylesheet and the document.

• In most traditional programming languages, the programmer would have to be explicit.

Page 21: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

XSLT Example

<xsl:template name="my_template"match ="section">

<h1><xsl:apply-templates select="title"/></h1>

…</xsl:template>

Page 22: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Explicit Code Equivalent

class my_template{ XPath match = "section" method action(context){

output("<h1>“) nodes = XPathEngine.evaluate(

"paragraph", context) for(node in nodes){

template = rules.lookup(node) template.evaluate(node) } output("</h1>“) } }rules.addrule(new my_template())

Page 23: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

With a little detail-hiding

class my_template{ XPath match = "section“ method action(context){

start_tag(“h1”)applyTemplates(“paragraph”, context)

end_tag(“/h1”) }} rules.addrule(new my_template())

Page 24: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Bottom line

• Given a good OO language, you can emulate XSLT

• But it requires a lot of infrastructure

• And it still requires you to take care of a lot of details…

Page 25: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

How do you choose?

• Spoon!

• Fork!

• Spoon!

• Fork!

Page 26: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Connectivity

• XSLT is only good at talking to “XML systems”

• Scripting languages can talk to anything

• This is typically the deciding factor.

• If you need to build a GUI, you need to do it in a language with a GUI library!

Page 27: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Navigation

• XSLT is really good at walking around the document tree.

• Scripting languages are typically not as concise at navigation – but very flexible.

• XSLT strongly “encourages” you to walk the tree top-down, front to back.

Page 28: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Data Structures

• XSLT makes it very difficult to create structured data containers

• The idea is that the only data you need is in the input document: it already has a data structure!

• But some computations are much easier if

you can restructure the information

yourself.

Page 29: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Community

• There are dozens of APIs for processing XML.

• Popular ones are not as powerful as XSLT.• Powerful ones are not as popular as XSLT.• Nobody gets fired for using XSLT – but you

can get fired for inventing your own XML

API.

Page 30: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Implementations: XSLT

• XSLT presents a lot of opportunity for optimization

• Smart implementers pre-compile XPaths and some even compile templates.

• There are many different XSLT implementations with different tradeoffs.

Page 31: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Implementations: Scripting

• Scripting code is hard to optimize.• On the other hand, the programmer can

choose algorithms and datastructures!• Plus you could write a SAX application

that does not use an in-memory tree at all.

• This is efficient but difficult.

Page 32: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Standardization

• XSLT is installed in – browsers, – database engines, – directory engines,– programming languages,– and operating systems!– JavaScript is the only language that even

comes close.

Page 33: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Best of Both Worlds?

• The easy way to split work is to use general purpose languages to generate or consume XML used by XSLT

tempfile.write("<xml …>")

run("xslttrans conv.xsl tempfile outfile")

data = outfile.read()

Page 34: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

More Sophisticated Approach

• XSLT has a concept of “extension functions”

<xsl:value-of

select=“myns:myfunc(.)"/>• If the implementation is smart enough,

functions can be defined in any scripting

language

Page 35: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Example Extension Functions

function getdate(numdays) {

var d = new Date();

var totalDays = parseInt(numdays) * multiplier;

d.setDate(d.getDate() + totalDays);

return d.toLocaleString();

}

Page 36: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Using an Extension Function

<xsl:template match="deadline"> <p>We have logged your enquiry and will respond by <xsl:value-of select="my-ext:getdate(string(@numdays))"/>.</p>

</xsl:template>

Page 37: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Extension Elements in Xalan-J

• “Xalan-Java uses the Bean Scripting Framework (BSF), an architecture for incorporating scripting into Java applications and applets.”

http://oss.software.ibm.com/developerworks/projects/bsf

Page 38: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

BSF-compatible Languages

• Rhino (ECMAScript/JavaScript)

• Jython (Python)

• Jacl (TCL)

• NetRexx (REXX)

• PerlScript (Perl – from ActiveState)

• VBScript/Jscript through ActiveScripting

Page 39: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Inline Extensions

<lxslt:component prefix="my-ext" functions="getdate">

<lxslt:script lang="javascript">

function getdate(numdays){

var d = new Date();

return d.toLocaleString();

} </lxslt:script>

</lxslt:component>

Page 40: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

In the Microsoft Universe

• Microsoft has a concept of “ActiveScripting Host” – used by MSXSL– VBScript– JScript– Python (ActivePython)– PerlScript (ActivePerl)– REXX– Tcl

Page 41: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

In the Linux/C world?

• Nothing yet…but maybe some day: ActiveScripting for XPCOM

• XPCOM is a portable variant of COM from the Mozilla project

• ActiveScripting for XPCOM would allow languages to be embedded in Linux/C apps like Xalan-C

Page 42: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

What about Standardization?

• XSLT 1.1 defines <xsl:script><xsl:script implements-prefix="pref" language="lang">

function f(x) { ... }

function g(y,z) { ... }

</xsl:script>

Page 43: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Supported Languages

• Out of the box, XSLT 1.1 has explicit support for ECMAScript (JavaScript) and Java

• Other languages are allowed but not predefined.

<xsl:script implements-prefix=“py" language=“Python" src=“foo.py">

Page 44: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

“From the spec”

• XSLT processors are not required to support any particular language binding.

• xsl:script elements with language values not supported by the processor are ignored.

Page 45: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

ActiveState and XSLT

• ActiveState is the leading vendor of Visual Studio.NET plugins:– Visual Perl– Visual Python– Visual XSLT

Page 46: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Visual XSLT

• Visual XSLT brings the features of Visual Studio to XSLT stylesheet and transformation development

• Improved productivity in creating XSLT transformations

Page 47: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Komodo XSLT

• Komodo is a multi-language IDE for Windows and Linux

• Komodo has the same XSLT feature set as Visual XSLT.

• Visual XSLT is appropriate for “Visual Studio” shops.

• Komodo is best for cross-platform shops.

Page 48: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Summary

• XSLT and scripting languages work together.

• XSLT engines can integrate them through extension functions (inline or out of line).

• Our debuggers and IDEs can bring them into a common development interface.

Page 49: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

[email protected]

http://aspn.activestate.com/ASPN/XSLT

http://www.w3.org/TR/xslt

http://www.xslt.com/

Page 50: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState

Page 51: Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?

Copyright 2001, ActiveState