求助:XSLT实现员工目录兼容首字母大小写姓氏显示
问题描述
公司员工目录存在大小写匹配问题:姓氏首字母为小写的员工无法显示在对应字母列表中。系统按姓氏首字母自动拉取员工,但仅支持首字母大写的情况。因CMS限制无法修改alphaValue参数,需要实现同时匹配大小写(如V和v),让"deAngelo"或"Devine"这类姓氏的员工显示在同一列表。
现有代码
单个字母页面的XSLT代码
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" extension-element-prefixes="faculty" version="1.0" xmlns:faculty="ext1" xmlns:xalan="http://xml.apache.org/xalan"> <xsl:output encoding="UTF-8" indent="yes" method="html" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/> <xsl:include href="/_internal/formats/EmpDir/EmpDir.xsl"/> <xsl:param name="alphaValue">v</xsl:param> <xsl:template match="/"> <xsl:call-template name="Employees"> <xsl:with-param name="alphaValue"/> </xsl:call-template> </xsl:template> <xsl:param name="alphaValue">V</xsl:param> <xsl:template match="/"> <xsl:call-template name="Employees"> <xsl:with-param name="alphaValue"/> </xsl:call-template> </xsl:template> </xsl:stylesheet>
EmpDir.xsl中相关处理代码
<xsl:choose> <xsl:when test="count(//directory[starts-with(last, $alphaValue)]) > 0"> <p style="text-align: center;">There are = <xsl:value-of select="count(//directory[starts-with(last, $alphaValue)])"/> entries. </p> <div style="float:right; width: calc(100% - 40px);"> <xsl:for-each select="//directory[starts-with(last, $alphaValue)]"> <xsl:sort order="ascending" select="last"/> <xsl:sort order="ascending" select="first"/> <xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/> <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/> <xsl:variable name="Photo"> <xsl:value-of select="translate(nmcid, $uppercase, $lowercase)"/> <xsl:text>.jpg</xsl:text> </xsl:variable>
已尝试的无效方案
- 转换
alphaValue为小写:
<!--add variables for upper and lower case to translate to lowercase --> <xsl:variable name="lowercaseV" select="'v'"/> <xsl:variable name="uppercaseV" select="'V'"/> <xsl:variable name="alphaValue" select="translate(., $uppercaseV, $lowercaseV)"/>
- 添加第二个
alphaValue参数匹配小写v:
<!--add secondary param for little v--> <xsl:param name="alphaValue">v</xsl:param>
解决方案
由于你使用的是XSLT 1.0(无原生大小写不敏感匹配函数),可以通过translate()将姓氏和alphaValue统一转换为同一大小写后再做匹配,以此忽略大小写差异。具体修改如下:
- 复用已有大小写变量:EmpDir.xsl中已经定义了
lowercase和uppercase变量,无需重复定义。 - 修改匹配逻辑:把所有
starts-with(last, $alphaValue)的判断,替换为将两者都转成小写后的匹配:
<!-- 替换原count判断 --> <xsl:when test="count(//directory[starts-with(translate(last, $uppercase, $lowercase), translate($alphaValue, $uppercase, $lowercase))]) > 0"> <p style="text-align: center;">There are = <xsl:value-of select="count(//directory[starts-with(translate(last, $uppercase, $lowercase), translate($alphaValue, $uppercase, $lowercase))])"/> entries. </p> <div style="float:right; width: calc(100% - 40px);"> <!-- 替换原for-each选择器 --> <xsl:for-each select="//directory[starts-with(translate(last, $uppercase, $lowercase), translate($alphaValue, $uppercase, $lowercase))]"> <xsl:sort order="ascending" select="last"/> <xsl:sort order="ascending" select="first"/> <!-- 保留原Photo变量及后续代码 -->
同时,注意到你单个字母页面中重复定义了alphaValue参数和匹配/的模板,这会导致只有最后一个模板生效,建议只保留一组参数和模板,比如:
<xsl:param name="alphaValue">V</xsl:param> <xsl:template match="/"> <xsl:call-template name="Employees"> <xsl:with-param name="alphaValue"/> </xsl:call-template> </xsl:template>
因为转换逻辑已经处理了大小写,不需要重复定义多组参数。
内容的提问来源于stack exchange,提问作者katprohas
相关产品推荐
相关产品推荐

