Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?"— Presentation transcript:

1 Copyright 2001, ActiveState

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

3 Copyright 2001, ActiveState Background: ActiveState ActiveState creates tools for open source and standards-based high-level languages: –Perl –Python –PHP –XSLT –Tcl

4 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, …

5 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

6 Copyright 2001, ActiveState Programming Language? –XSLT has looping constructs function calling variables parameters math functions module combination …

7 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

8 Copyright 2001, ActiveState Example of Iterating: XML Paul Prescod Snake Charmer Gisle Aas Camel Herder

9 Copyright 2001, ActiveState Example of Iterating: XSLT

10 Copyright 2001, ActiveState

11 HTML Output Paul Prescod Snake Charmer Gisle Aas Camel Herder

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

13 Copyright 2001, ActiveState Functions, Parameters, Variables <xsl:value-of select="$studio * $everything"/>

14 Copyright 2001, ActiveState Template Call

15 Copyright 2001, ActiveState

16 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

17 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

18 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

19 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

20 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.

21 Copyright 2001, ActiveState XSLT Example <xsl:template name= " my_template " match ="section"> …

22 Copyright 2001, ActiveState Explicit Code Equivalent class my_template{ XPath match = "section" method action(context){ output(" ) nodes = XPathEngine.evaluate( "paragraph", context) for(node in nodes){ template = rules.lookup(node) template.evaluate(node) } output(" ) } }rules.addrule(new my_template())

23 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())

24 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…

25 Copyright 2001, ActiveState How do you choose? Spoon! Fork! Spoon! Fork!

26 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!

27 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.

28 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.

29 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.

30 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.

31 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.

32 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.

33 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(" ") run("xslttrans conv.xsl tempfile outfile") data =

34 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

35 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(); }

36 Copyright 2001, ActiveState Using an Extension Function We have logged your enquiry and will respond by.

37 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. s/bsf

38 Copyright 2001, ActiveState BSF-compatible Languages Rhino (ECMAScript/JavaScript) Jython (Python) Jacl (TCL) NetRexx (REXX) PerlScript (Perl – from ActiveState) VBScript/Jscript through ActiveScripting

39 Copyright 2001, ActiveState Inline Extensions function getdate(numdays){ var d = new Date(); … return d.toLocaleString(); }

40 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

41 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

42 Copyright 2001, ActiveState What about Standardization? XSLT 1.1 defines function f(x) {... } function g(y,z) {... }

43 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.

44 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.

45 Copyright 2001, ActiveState ActiveState and XSLT ActiveState is the leading vendor of Visual Studio.NET plugins: –Visual Perl –Visual Python –Visual XSLT

46 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

47 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.

48 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.

49 Copyright 2001, ActiveState Resources

50 Copyright 2001, ActiveState


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

Similar presentations

Ads by Google