使用XSL转换XML并调用外部XML动态插入员工ID列表
我来帮你搞定这个从外部XML导入员工ID列表的需求!首先得注意你提到的EmployeeList.xml带有Taleo的命名空间,这在XSLT处理里是关键细节,不然会找不到目标节点。下面是具体的实现方案:
实现步骤
1. 先处理命名空间声明
在你的XSLT样式表里,必须先声明外部XML的Taleo命名空间,这样XPath才能正确定位节点。示例如下:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:taleo="http://www.taleo.com/ws/integration/toolkit/2005/07" exclude-result-prefixes="taleo">
这里用taleo作为命名空间前缀,exclude-result-prefixes可以避免这个前缀出现在最终输出的XML里。
2. 使用document()函数加载外部XML
在目标XML需要插入员工ID的位置,调用document()函数引入EmployeeList.xml,然后遍历里面的员工节点提取ID。假设外部XML里员工节点是taleo:Employee,ID字段是taleo:ID,代码示例:
<!-- 在目标XML需要插入员工列表的位置添加这段逻辑 --> <xsl:for-each select="document('EmployeeList.xml')/taleo:ExportTXML/taleo:Employee"> <EmployeeID> <xsl:value-of select="taleo:ID"/> </EmployeeID> </xsl:for-each>
如果你的目标XML本身也有命名空间,记得调整输出节点的命名空间前缀或声明。
3. 完整XSLT示例(结合目标XML结构)
假设你的目标XML结构是这样的:
<?xml version="1.0" encoding="UTF-8"?> <TargetDocument> <EmployeeList> <!-- 这里需要插入外部的员工ID列表 --> </EmployeeList> </TargetDocument>
对应的完整XSLT代码如下:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:taleo="http://www.taleo.com/ws/integration/toolkit/2005/07" exclude-result-prefixes="taleo"> <!-- 匹配目标文档的根节点 --> <xsl:template match="/"> <TargetDocument> <EmployeeList> <!-- 加载外部XML并遍历员工节点提取ID --> <xsl:for-each select="document('EmployeeList.xml')/taleo:ExportTXML/taleo:Employee"> <EmployeeID> <xsl:value-of select="taleo:ID"/> </EmployeeID> </xsl:for-each> </EmployeeList> </TargetDocument> </xsl:template> </xsl:stylesheet>
关键注意事项
- 确保
EmployeeList.xml和XSLT文件在同一目录下,或者在document()函数里使用绝对路径(比如file:///C:/projects/EmployeeList.xml)。 - 如果外部XML的实际节点名称和示例不同(比如员工节点不是
taleo:Employee),需要根据真实的XML结构调整XPath表达式。 - 如果你使用的是XSLT 2.0及以上版本,也可以用
doc()函数替代document(),用法基本一致。
内容的提问来源于stack exchange,提问作者Ben Esteban
相关产品推荐
相关产品推荐

