You can declare the data types (validation methods), methods and variables used in the X-script in the auxiliary element "xd:declaration" and refer to them by name:
<xd:declaration>
type isbn int(10000000, 999999999);
type published gYear();
Date today = now(); /* actual date and time (when this X-definition started) */
/* This methos prints a line with string to system output. */
void message(String s) {
outln(s);
}
</xd:declaration>
<Book xd:script="init message('Started ' + today); finally message('Process finished');"
ISBN="isbn();"
published="optional published();" >
<Author xd:script="occurs *" >
string()
</Author>
<Title>
string()
</Title>
</Book>