Hi Folks,
Yesterdays responses were outstanding and illuminating. Thank you.
I learned that a declarative program describes the relationship of the output to the input.
This raises new questions. Lets 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 doesnt 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?
Heres my implementation of f:substring-before-last():
<xsl:function name="f:substring-before-last" as="xs:string?">
<xsl:param name="string" as="xs:string" />
<xsl:param name="delimiter" as="xs:string" />
<xsl:if test="contains( $string, $delimiter )">
<xsl:variable name="url">
<xsl:value-of
select="substring-before( $string, $delimiter )"/>
<xsl:value-of select="$delimiter"/>
<xsl:value-of
select="f:substring-before-last(substring-after
( $string, $delimiter ), $delimiter)" />
</xsl:variable>
<xsl:value-of select="$url" />
</xsl:if>
</xsl:function>
/Roger