Presentation is loading. Please wait.

Presentation is loading. Please wait.

Yet more on XSLT. Regular expression handling in EXSLT We saw that the EXSLT extensions are divided into a group of modules, each of which has its own.

Similar presentations


Presentation on theme: "Yet more on XSLT. Regular expression handling in EXSLT We saw that the EXSLT extensions are divided into a group of modules, each of which has its own."— Presentation transcript:

1 Yet more on XSLT

2 Regular expression handling in EXSLT We saw that the EXSLT extensions are divided into a group of modules, each of which has its own namespace We saw that there is a module providing access to regular expressions: –Regular Expressions EXSLT, http://exslt.org/regular-expressions The module provides three functions: –boolean test(…) Tests to see whether a string matches a specified regular expression –object match(…) performs regular expression matching on a string, returning the submatches found –string replace(…) replaces the portions of a string that match a given regular expression with the contents of another string

3 test() in EXSLT regular expression module Syntax boolean somePrefix:test(target, regexp, flags) Semantics –The function returns true if the string given as the first argument matches the regular expression given as the second argument. Arguments –The first argument is the string to be analysed –The second argument is a regular expression that follows the Javascript regular expression syntax. –The third argument is a string consisting of flags to be used by the test. If a character is present then that flag is true. The flags are: i: case insensitive – if present, the regular expression is treated as case insensitive; if absent, the regular expression is case sensitive. g: global test - no effect on this function, but is retained for consistency with the match() and replace() functions

4 Example usage of test() Testing email addresses This address appears fine Error: this email address lacks a 2nd level domain

5 Support for the regular expression module, part 1 The EXSLT regular-expression module is supported in Firefox However, it appears not to be supported in other browsers

6 Support for the regular expression module, part 2 The EXSLT regular- expression module also appears not to be supported in PHP5

7 Can we use regular expressions on the server-side? Yes, we can use regular expressions in server-side XSLT processing Because we can call PHP functions from inside XSLT

8 Using regular expressions on the server-side The PHP program allows the stylesheet below to use the power of regular expressions to process the XML document Therefore, the result is available in all main browsers

9 How this works, part 1 We have already seen that the XSLT Processor implemented in PHP5 supports certain external namespaces For example, we have seen it supports http://exslt.org/common We can access PHP functions from inside XSLT because  the processor also supports this namespace http://php.net/xsl  which includes a function, called function, that allows us to access any PHP function

10 How this works, part 2 We can use function() inside XSL if we make the stylesheet refer to this namespace: http://php.net/xsl <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fred=“http://php.net/xsl" > … … …

11 How this works, part 3 We can use any prefix we like for the namespace But it’s probably better to use something meaningful, such as php <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php=“http://php.net/xsl" > … … …

12 How this works, part 4 Let’s assume we have implemented, in PHP, a boolean function called isCorrectEmailAddress() We can now use that in our stylesheet: <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:php="http://php.net/xsl" > … This address appears fine Error: this email address lacks a 2nd level domain

13 How this works, part 5 Our address-checking function in PHP: function isCorrectEmailAddress($string) { if ( preg_match('/@\w+\.\w+/',$string)) { return true; } else { return false; } } We must include this function in our PHP program, but we must also …

14 How this works, part 6 We must also ensure that our PHP program tells the XSLT processor to “register” PHP functions: <?php //header('Content-type: text/xml'); function isCorrectEmailAddress($string) { if ( preg_match('/@\w+\.\w+/',$string)) { return true; } else { return false; } } $xmldoc = new DOMDocument(); $xmldoc->load("demo301.xml"); $xsldoc = new DOMDocument(); $xsldoc->load("demo301.xsl"); $xsl = new XsltProcessor(); $xsl->registerPHPFunctions(); $xsl->importStyleSheet($xsldoc); echo $xsl->transformToXML($xmldoc); ?>


Download ppt "Yet more on XSLT. Regular expression handling in EXSLT We saw that the EXSLT extensions are divided into a group of modules, each of which has its own."

Similar presentations


Ads by Google