XSLT按peci:Usage_Behavior_ID区分提取当前/永久地址
问题根因
原有XSLT出现地址重复、重叠的核心原因是地址节点筛选逻辑不完整:
- 外层
xsl:when仅判断了是否存在Usage_Behavior_ID为Current的地址,没有精准绑定到对应地址节点 - 内部取值的XPath仅用
peci:Usage_Type='HOME'作为筛选条件,而示例中Current、Permanent两个地址的Usage_Type均为HOME,因此XPath会同时选中两个地址节点,取字段值时自动拼接两个节点的内容,导致数据错乱。
修复方案
- 筛选地址节点时,必须同时匹配
peci:Usage_Type='HOME'和目标peci:Usage_Behavior_ID两个条件,确保只选中唯一的目标地址节点 - 使用XSLT变量存储匹配到的目标地址节点,后续字段直接从变量取值,既简化代码,也避免重复写筛选条件导致的错误
- 补充Permanent地址的提取分支,覆盖两类地址的提取需求
修正后代码
<xsl:choose> <!-- 提取当前地址(Current) --> <xsl:when test="peci:Person_Communication/peci:Address[peci:Usage_Type='HOME' and peci:Usage/peci:Usage_Behavior_ID ='Current']"> <!-- 存储匹配到的当前地址节点,避免重复筛选 --> <xsl:variable name="currentAddress" select="peci:Person_Communication/peci:Address[peci:Usage_Type='HOME' and peci:Usage/peci:Usage_Behavior_ID ='Current']"/> <Current_Flat_House_Wing_Number xtt:quotes="always"> <xsl:value-of select="$currentAddress/peci:Address_Line_1"/> </Current_Flat_House_Wing_Number> <Current_Street_Locality_Area xtt:quotes="always"> <xsl:value-of select="$currentAddress/peci:Address_Line_2"/> </Current_Street_Locality_Area> <Current_Landmark xtt:quotes="always"> <xsl:value-of select="$currentAddress/peci:Address_Line_2"/> </Current_Landmark> <Current_Pincode xtt:quotes="always"> <xsl:value-of select="$currentAddress/peci:Postal_Code"/> </Current_Pincode> <Current_Country xtt:quotes="always"> <xsl:value-of select="$currentAddress/peci:Country"/> </Current_Country> <Current_State xtt:quotes="always"> <xsl:value-of select="$currentAddress/peci:State_Province"/> </Current_State> <Current_City xtt:quotes="always"> <xsl:value-of select="$currentAddress/peci:City"/> </Current_City> </xsl:when> <!-- 提取永久地址(Permanent) --> <xsl:when test="peci:Person_Communication/peci:Address[peci:Usage_Type='HOME' and peci:Usage/peci:Usage_Behavior_ID ='Permanent']"> <!-- 存储匹配到的永久地址节点 --> <xsl:variable name="permanentAddress" select="peci:Person_Communication/peci:Address[peci:Usage_Type='HOME' and peci:Usage/peci:Usage_Behavior_ID ='Permanent']"/> <Permanent_Flat_House_Wing_Number xtt:quotes="always"> <xsl:value-of select="$permanentAddress/peci:Address_Line_1"/> </Permanent_Flat_House_Wing_Number> <Permanent_Street_Locality_Area xtt:quotes="always"> <xsl:value-of select="$permanentAddress/peci:Address_Line_2"/> </Permanent_Street_Locality_Area> <Permanent_Landmark xtt:quotes="always"> <xsl:value-of select="$permanentAddress/peci:Address_Line_2"/> </Permanent_Landmark> <Permanent_Pincode xtt:quotes="always"> <xsl:value-of select="$permanentAddress/peci:Postal_Code"/> </Permanent_Pincode> <Permanent_Country xtt:quotes="always"> <xsl:value-of select="$permanentAddress/peci:Country"/> </Permanent_Country> <Permanent_State xtt:quotes="always"> <xsl:value-of select="$permanentAddress/peci:State_Province"/> </Permanent_State> <Permanent_City xtt:quotes="always"> <xsl:value-of select="$permanentAddress/peci:City"/> </Permanent_City> </xsl:when> </xsl:choose>
注意事项
- 原代码中
Current_Street_Locality_Area、Current_Landmark字段均取了Address_Line_1的值,上述代码已调整为取Address_Line_2,可根据实际业务的字段映射规则修改取值路径 - 如果需要同时输出两类地址,去掉外层
xsl:choose判断,分别定义两个地址变量直接取值即可,不需要互斥判断
内容的提问来源于stack exchange,提问作者Chris Tucker 2016
相关产品推荐
相关产品推荐

