I learned that a declarative program describes the relationship of the output to the input.

This raises new questions. Let’s take an example. Suppose that this is the input:

<XML-file>http://www.example.org/book.xml</XML-file>

I want the output to contain the URL and the filename separated:

<XML-file>

<URL>http://www.example.org/</URL>

<Filename>book.xml</Filename>

</XML-file>

This XSLT code describes the relationship of the output to the input:

<xsl:template match="XML-file">

<xsl:variable name="url"

select="f:substring-before-last(., '/')" />

<XML-file>

<URL>

<xsl:value-of select="$url" />

</URL>

<Filename>

<xsl:value-of select="substring-after(., $url)" />

</Filename>

</XML-file>

</xsl:template>

I believe this code is declarative. Do you agree?

Note my use of the function, f:substring-before-last(). There is no such built-in function, I created it. Below is how I implemented it. The implementation doesn’t seem descriptive. It seems quite recipe-like:

Get the substring before $delimiter and output it,

then output $delimiter, and then recurse.

It seems quite imperative. Do you agree?

If I stuff a bunch of imperative code into functions, and the “main” code is declarative, do I still have a declarative program?