14
Yet more on XSLT

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

Embed Size (px)

Citation preview

Page 1: 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

Yet more on XSLT

Page 2: 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

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

Page 3: 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

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

Page 4: 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

Example usage of test()

<?xml version="1.0"?><xsl:transform version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:regexp="http://exslt.org/regular-expressions" ><xsl:output method="html" version="4.0" indent="yes"/><xsl:template match="/"><html><head><title>Testing email addresses</title></head><body><xsl:for-each select="info/emailAddresses/emailAddress"><xsl:value-of select="."/><xsl:text> </xsl:text><xsl:choose><xsl:when test=“regexp:test(., '@\w+\.\w+','')" >This address appears fine</xsl:when><xsl:otherwise><strong>Error: this email address lacks a 2nd level domain</strong></xsl:otherwise></xsl:choose><br/></xsl:for-each></body></html></xsl:template></xsl:transform>

Page 5: 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

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

Page 6: 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

Support for the regular expression module, part 2

• The EXSLT regular-expression module also appears not to be supported in PHP5

Page 7: 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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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" >

<xsl:when test=“fred:function( … )" >

</xsl:when>

</xsl:transform>

Page 11: 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

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" >

<xsl:when test=“php:function( … )" >

</xsl:when>

</xsl:transform>

Page 12: 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

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:

<?xml version="1.0"?><xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:php="http://php.net/xsl" >

…<xsl:for-each select="info/emailAddresses/emailAddress"><xsl:value-of select="."/><xsl:text> </xsl:text><xsl:choose>

<xsl:when test="php:function(‘isCorrectEmailAddress',string(.))" >This address appears fine</xsl:when><xsl:otherwise> <strong>Error: this email address lacks a 2nd level domain</strong></xsl:otherwise></xsl:choose><br/></xsl:for-each>

Page 13: 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

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 …

Page 14: 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

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);?>