proc
procs (or functions) are scraps of templates that can be called from other parts of the stylesheet. The keyword takes no options other
than the proc name. Parameters are declared within the block that follows the proc declaration.
Syntax
<proc> ::= "proc" <proc name>
( "scope" <quote> <qname> <quote> )?
( "priority" <quote> <int> <quote> )?
"{"
<parameter>*
<block templates>+
"}"
Options
None.
Elements
Although there are no required elements within the proc block, it is meaningless to have a proc that is null and does not return
any values (string characters).
Examples
Consider a proc that returns a substring after the last occurrence of a character. The following would be an example of such a
(recursive) proc.
proc substring-after-last {
parameter string
parameter delimiter
choose {
when "contains($string, $delimiter)" {
call substring-after-last {
with string "substring-after($string, $delimiter)”
with delimiter "$delimiter"
}
}
otherwise {
value "$string"
}
}
}
which translates to the rather more expansive
<template name="substring-after-last">
<param name="string"/>
<param name="delimiter"/>
<choose>
<when test="contains($string, $delimiter)">
<call-template name="substring-after-last">
<with-param name="string" select="substring-after($string, $delimiter)"/>
<with-param name="delimiter" select="$delimiter"/>
</call-template>
</when>
<otherwise>
<value-of select="$string"/>
</otherwise>
</choose>
</template>
To use this within a variable to extract the final component in a filename and path.
variable idString {
call substring-after-last {
with string "substring-after($filename, '/')"
with delimiter "'/'"
}
}
Errors
Forgetting the proc name.
stylesheet {
version "1.0"
proc {
parameter string
parameter delimiter
choose {
when "contains($string, $delimiter)" {
call substring-after-last {
with string "substring-after($string, $delimiter)”
with delimiter "$delimiter"
}
}
otherwise {
value "$string"
}
}
}
}
will give the error
**** (115) Expected name after "proc" in line 4
Insert name.
**** (116) Could not find "substring-after-last" in line 9
Check "substring-after-last" is defined in current block/context.
Leaving out the opening bracket.
stylesheet {
version "1.0"
proc substring-after-last
parameter string
parameter delimiter
choose {
when "contains($string, $delimiter)" {
call substring-after-last {
with string "substring-after($string, $delimiter)”
with delimiter "$delimiter"
}
}
otherwise {
value "$string"
}
}
}
}
reports
**** (104) Missing "{" in line 5
Insert bracket.
The other main error that can occur is having the parameter declared in any other place than the start of the proc's block
statements. So for example
proc substring-after-last {
parameter string
choose {
when "contains($string, $delimiter)" {
call substring-after-last {
with string "substring-after($string, $delimiter)"
with delimiter "$delimiter"
}
}
otherwise {
value "$string"
}
}
parameter delimiter
}
will give the error report
**** (131) Parameter "delimiter" in "proc:substring-after-last" in line 17 must follow declaration.
Check order.
Copyright 2024 Hugh Field-Richards. All Rights Reserved.