Wednesday, July 9, 2008

Xsl function to testing if text only contains footnote marks

The situation come up were I had a table of data and in on of the columns it had just footnote marks. Well I had to merge these footnote marks over to the previous column, but in order to do this I needed to know if the only value in the cell were marks. So I realize there are many ways to do this but I thought this was a cool way. It reads in an external xml file with all the footnote refs I want to search for the ref value is a regex.
<footnotes>
<footnote ref="\*"/>
<footnote ref="\+"/>
<footnote ref="†"/>
<footnote ref="\([a-z]\)"/>
<footnote ref="\([1-9]\)"/>
</footnotes>

I then concat the refs together with a | and do a replace on the string value. If nothing is left then it is a footnote ref.


<!-- Reads all possible footnote ref marks from a footnotes.xml file and if pText only contains footnote marks returns true otherwise false -->
<xsl:function name="txt:IsFootnoteRef" as="xs:boolean">
<xsl:param name="pText" as="xs:string"/>
<xsl:variable name="vFootnotes" select=" document('./footnotes.xml')/footnotes/footnote"/>
<xsl:choose>
<xsl:when test="not($vFootnotes)">
<xsl:value-of select="false()"/>
</xsl:when>
<xsl:when test="not($pText)">
<xsl:value-of select="false()"/>
</xsl:when>
<xsl:when test="normalize-space($pText) = '' ">

<xsl:value-of select="false()"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vFootnoteRef">
<xsl:value-of select="$vFootnotes/@ref" separator="|"/>
</xsl:variable>

<xsl:choose>
<xsl:when test=" normalize-space(replace($pText, $vFootnoteRef , '' , 'i')) = '' ">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="false()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:function>

No comments: