Copyright 2001, ActiveState
Mar 27, 2015
Copyright 2001, ActiveState
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
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, …
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
Copyright 2001, ActiveState
Programming Language?
– XSLT has• looping constructs• function calling• variables• parameters• math functions• module combination• …
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
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>
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>
Copyright 2001, ActiveState
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>
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”)
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>
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>
Copyright 2001, ActiveState
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
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
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
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
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.
Copyright 2001, ActiveState
XSLT Example
<xsl:template name="my_template"match ="section">
<h1><xsl:apply-templates select="title"/></h1>
…</xsl:template>
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())
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())
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…
Copyright 2001, ActiveState
How do you choose?
• Spoon!
• Fork!
• Spoon!
• Fork!
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!
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.
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.
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.
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.
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.
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.
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()
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
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();
}
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>
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
Copyright 2001, ActiveState
BSF-compatible Languages
• Rhino (ECMAScript/JavaScript)
• Jython (Python)
• Jacl (TCL)
• NetRexx (REXX)
• PerlScript (Perl – from ActiveState)
• VBScript/Jscript through ActiveScripting
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>
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
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
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>
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">
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.
Copyright 2001, ActiveState
ActiveState and XSLT
• ActiveState is the leading vendor of Visual Studio.NET plugins:– Visual Perl– Visual Python– Visual XSLT
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
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.
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.
Copyright 2001, ActiveState
http://aspn.activestate.com/ASPN/XSLT
http://www.w3.org/TR/xslt
http://www.xslt.com/
Copyright 2001, ActiveState
Copyright 2001, ActiveState