如何在XSLT中按分隔符增量赋值层级部门路径变量?
XSLT 实现累积部门路径序列
实现步骤
拆分原始字符串为Token序列
用tokenize函数把带+分隔的部门字符串拆成单个元素的序列,原字符串开头的+会生成一个空首元素:<xsl:variable name="tokens" select="tokenize(/EmpJob/EmpJob/ConcatDepartment, '\+')"/>拆分后的
$tokens序列为:('', 'Local_Functions', 'Group_Functions', 'Group_Owners', 'Group_Managers', 'Group_Leads', 'Interim_Services')生成累积路径序列
通过for循环遍历Token序列的位置,对每个位置拼接前面所有非空部门元素,同时保留首位置的空字符串:<xsl:variable name="departments" select=" '', for $pos in 2 to count($tokens) return string-join($tokens[2 to $pos], '/') "/>
效果验证
调用<xsl:value-of>访问序列不同位置,结果完全匹配需求:
<xsl:value-of select="$departments[1]"/>→ 返回空字符串<xsl:value-of select="$departments[2]"/>→ 返回Local_Functions<xsl:value-of select="$departments[3]"/>→ 返回Local_Functions/Group_Functions- ...
<xsl:value-of select="$departments[7]"/>→ 返回完整层级路径Local_Functions/Group_Functions/Group_Owners/Group_Managers/Group_Leads/Interim_Services<xsl:value-of select="$departments[8]"/>及之后 → 返回空字符串
注意事项
该方案需要XSLT 2.0及以上版本支持,因为用到了tokenize、for表达式、string-join等特性。
内容的提问来源于stack exchange,提问作者Pavan Kumar
相关产品推荐
相关产品推荐

