dbaker6
dbaker6

Reputation: 39

xsl function needs improvement

So I have this function here

<xsl:function name="fn:role-rank" as="xs:integer">
     <xsl:param name="role" as="xs:string"/> 
     <xsl:sequence select="index-of(('Faculty', 'Adjunct Faculty', 'Staff'), $role)"/>
    </xsl:function>

...and what is does is arranges employees by their title.. first Faculty is displayed, then Adjunct Faculty, and then finally Staff. It works great the only problem is if someone has a title that is different from those three the page breaks.

What I would like it to do is if someone has a different title it would just be next in line under staff.

Any help would be greatly appreciated!

Upvotes: 0

Views: 41

Answers (2)

Michael Kay
Michael Kay

Reputation: 163322

There's also the old technique of putting a copy of the sought value at the end of the sequence to be searched:

<xsl:function name="fn:role-rank" as="xs:integer">
     <xsl:param name="role" as="xs:string"/>
     <xsl:variable name="known-titles" as="xs:string*" 
                   select="('Faculty', 'Adjunct Faculty', 'Staff')"/> 
     <xsl:sequence select="index-of(($known-titles, $role), $role)[1]"/>
</xsl:function>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167516

You could check return the count plus 1 of the sequence of known values for any role not being found:

<xsl:function name="fn:role-rank" as="xs:integer">
     <xsl:param name="role" as="xs:string"/>
     <xsl:variable name="known-titles" 
                   as="xs:string*" 
                   select="('Faculty', 'Adjunct Faculty', 'Staff')"/> 
     <xsl:sequence select="(index-of($known-titles, $role), 
                            count($known-titles) + 1)[1]"/>
    </xsl:function>

Upvotes: 1

Related Questions