You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助: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)]) &gt; 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统一转换为同一大小写后再做匹配,以此忽略大小写差异。具体修改如下:

  1. 复用已有大小写变量:EmpDir.xsl中已经定义了lowercase和uppercase变量,无需重复定义。
  2. 修改匹配逻辑:把所有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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 14:37:32