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 namespace We saw that there is a module providing access to regular expressions: –Regular Expressions EXSLT, 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
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
Example usage of test() Testing addresses This address appears fine Error: this address lacks a 2nd level domain
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
Support for the regular expression module, part 2 The EXSLT regular- expression module also appears not to be supported in PHP5
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
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
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 We can access PHP functions from inside XSLT because the processor also supports this namespace which includes a function, called function, that allows us to access any PHP function
How this works, part 2 We can use function() inside XSL if we make the stylesheet refer to this namespace: <xsl:transform version="1.0" xmlns:xsl=" xmlns:fred=“ > … … …
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=" xmlns:php=“ > … … …
How this works, part 4 Let’s assume we have implemented, in PHP, a boolean function called isCorrect Address() We can now use that in our stylesheet: <xsl:transform xmlns:xsl=" version="1.0" xmlns:php=" > … This address appears fine Error: this address lacks a 2nd level domain
How this works, part 5 Our address-checking function in PHP: function isCorrect Address($string) { if ( { return true; } else { return false; } } We must include this function in our PHP program, but we must also …
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 isCorrect Address($string) { if ( { 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); ?>