11.3 Examples of ECMAScripts with Policies

The following examples use the ECMAScript file demo.js with different policies. The demo.js file contains three ECMAScript function definitions.

11.3.1 DirXML Script Policy Calling an ECMAScript Function

The DirXML Script policy converts an attribute that is a URL reference to a photo to the Base64 encoded photo data by calling the ECMAScript function getB64ImageFromURL().The policy can be used as an Input Transformation or Output Transformation policy.

The function reads an image from a URL and returns the content as a Base64 encoded string.

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE policy PUBLIC "policy-builder-dtd" "C:\Program Files\Novell\Designer\eclipse\plugins\com.novell.designer.idm.policybuilder_1.2.0.200612180606\DTD\dirxmlscript.dtd"><policy>	<rule>		<description>Reformat photo from URL to octet</description>		<conditions/>		<actions>			<do-reformat-op-attr name="photo">				<arg-value type="octet">					<token-xpath expression="es:getB64ImageFromURL(string($current-value))"/>				</arg-value>			</do-reformat-op-attr>		</actions>	</rule></policy>

Function: <static> String getB64ImageFromURL(<String> urlString)

Parameters: urlString (URL of the image file)

Returns: Base64 encoded content of the image (or empty string if error)

The file ReformatPhoto.xml calls the ECMAScript function getB64ImageFromURL from a DirXML Script policy. The file phototest.xml is a sample input document that shows the policy in action.

Figure 11-7 Reformat Photo Example

The ECMAScript calls the getB64ImageFromURL function, which then returns the current value as a string.

11.3.2 XSLT Policy Calling an ECMAScript Function at the Driver Level

The XSLT policy either splits a single comma-delimited value into multiple values, or joins multiple values into a single comma-delimited value. The XSLT policy is defined at the driver level and can be used as an Input Transformation or Output Transformation policy.

NOTE:DirXML Script has the split and join functionality built into it, but XSLT does not. This type of function allows XSLT to have the split and join functionality.

There are two functions:

Join

The Join function joins the text values of Nodes in a NodeSet into a single string

<!--  template that joins the joinme attribute values into a single value --><xsl:template match="*[@attr-name='joinme']//*[value] | *[@attr-name='joinme'][value]">   <xsl:copy>      <xsl:apply-templates select="@*|node()[not(self::value)]"/>	   <value>    	  <xsl:value-of select="es:join(value)"/>	   </value>   </xsl:copy></xsl:template>

Function: <static> String join(<NodeSet> nodeSet, <string> delimiter)

Parameters: nodeSet (the input NodeSet) and delimiter (the delimiter to split on (optional: default = none))

Returns: The concatenation of the string values of the Nodes in the nodeSet, separated by the delimiter.

Split

The Split function splits a string into a NodeSet.

<!--  template that splits the splitme attribute values into multiple values --><xsl:template match="*[@attr-name='splitme']//value">     <xsl:for-each select="es:split(string(.))">	   <value>    	  <xsl:value-of select="."/>	   </value>     </xsl:for-each></xsl:template>

Function: <static> NodeSet split(<String> inputString, <String> delimiter)

Parameters: inputString (the script to split) and delimiter (the delimiter to split on (optional: default = “,”))

Returns: A NodeSet containing text nodes.

The file SplitJoin.xsl calls the join or split functions in an XSLT style sheet. The file splitjointest.xml is an input document that shows the style sheet in action.

11.3.3 XSLT Policy Calling an ECMAScript Function in the Style Sheet

The XSLT policy demonstrates embedding ECMAScript function definitions with the XSLT style sheet. The functions convert a string to uppercase.

<!-- define ecmascript functions --><es:script>function uppercase(input){	return String(input).toUpperCase();}</es:script>

The file uppercase.xsl defines the ECMAScript function with the XSLT style sheet. The file uppercasetest.xml is an input document that shows the style sheet in action.