如何在BizTalk映射中实现LDAP请求结果转平面文件?
LDAP查询结果XML到固定格式平面文件的映射方案
1. 补全源XML Schema定义
由于LDAP返回的XML无预定义Schema,需要基于样本数据调整候选Schema,确保能正确解析所有节点:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="LDAPSearchResults"> <xs:complexType> <xs:sequence> <xs:element name="ResultEntry" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="LDAPAttribute" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="Type" type="xs:string" use="required"/> <xs:attribute name="Value" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="DN" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
2. 明确字段映射规则
结合你给出的示例,先梳理目标平面文件与源XML属性的对应关系:
| 目标字段位置 | 源XML属性 | 说明 |
|---|---|---|
| 固定头部行 | 无 | 直接用常量生成,其中时间戳可动态生成 |
| 用户行第1位 | 需确认(如mobile/某extensionAttribute) | 示例中手机号未在源XML出现,需明确来源 |
| 用户行第2位 | LDAPAttribute[@Type="givenName"]/@Value | 名 |
| 用户行第3位 | LDAPAttribute[@Type="sn"]/@Value | 姓 |
| 用户行第4位 | LDAPAttribute[@Type="mail"]/@Value | 邮箱 |
| 用户行第5-12位 | 空字符串 | 固定8个空占位 |
| 用户行第13位 | LDAPAttribute[@Type="extensionAttribute3"]/@Value | 数字编码 |
3. 映射实现方案
集成工具实现(以BizTalk为例)
- 将补全后的源XML Schema和目标FF Schema导入项目
- 创建映射文件,配置循环映射:
- 把
LDAPSearchResults/ResultEntry节点循环映射到目标的用户行记录 - 对每个
ResultEntry,使用值映射函数匹配属性:- 匹配
Type="givenName"→ 目标「名」字段 - 匹配
Type="sn"→ 目标「姓」字段 - 匹配
Type="mail"→ 目标「邮箱」字段 - 匹配
Type="extensionAttribute3"→ 目标最后一个数字字段
- 匹配
- 中间8个空字段用常量
""填充,保证分号分隔格式正确 - 单独添加固定头部行的常量映射,确保输出首行是头部
- 把
自定义脚本实现(Python示例)
import xml.etree.ElementTree as ET from datetime import datetime # 解析LDAP响应XML tree = ET.parse('ldap_response.xml') root = tree.getroot() # 生成固定头部,时间戳用当前时间 timestamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") header = f"USER;xxx;1067;{timestamp};some_department@xx.se;F;\n" output_lines = [header] for entry in root.findall('ResultEntry'): # 收集当前用户所有属性 user_attrs = {} for attr in entry.findall('LDAPAttribute'): user_attrs[attr.get('Type')] = attr.get('Value') # 构建用户行(手机号需替换为实际对应属性) phone = user_attrs.get('mobile', '') # 替换为实际手机号属性名 first_name = user_attrs.get('givenName', '') last_name = user_attrs.get('sn', '') email = user_attrs.get('mail', '') ext3 = user_attrs.get('extensionAttribute3', '') # 拼接符合格式的用户行 user_line = f"{phone};{first_name};{last_name};{email};;;;;;;;{ext3};\n" output_lines.append(user_line) # 写入平面文件 with open('output_users.txt', 'w', encoding='utf-8') as f: f.writelines(output_lines)
4. 关键注意事项
- 源Schema需兼容所有可能的
LDAPAttribute类型,避免新增属性导致解析失败 - 处理属性缺失场景:若某个LDAP属性不存在,映射时填充空字符串,保证分号数量与目标格式一致
- 时间戳:若要求使用LDAP查询时间而非当前时间,需从请求日志或响应元数据中提取
- 手机号字段:需确认其在LDAP中的实际属性名,补充到映射逻辑中
内容的提问来源于stack exchange,提问作者Emperor 2052
相关产品推荐
相关产品推荐

