使用XSLT过滤XML文档的动态XPath



我搜索StackOverflow已经有一段时间了,但没有成功。我的目标是过滤员工XML文档。Input XML有大约9000名员工,我有多个目标系统,他们希望根据每个目标系统的不同选择标准来获得这些员工记录的子集。样本输入:

<Employees>
<Employee>
<id>1</id>
<name>Name1</name>
</Employee>
<Employee>
<id>2</id>
<name>Name2</name>
</Employee>
<Employee>
<id>3</id>
<name>Name3</name>
</Employee>
<Employee>
<id>4</id>
<name>Name4</name>
</Employee>
</Employees>

输出应具有相同的结构,但应根据选择标准(XPATH(减少节点。如何将动态XPath传递给XSLT样式表,并将过滤后的员工作为输出请注意,完整的Xpath表达式以及筛选条件作为一个字符串传递。

期望输出:

<Employees>
<Employee>
<id>2</id>
<name>Name2</name>
</Employee>
<Employee>
<id>3</id>
<name>Name3</name>
</Employee>
</Employees>

使用XSLT3.0,xsl:evaluate进行动态XPath评估。您可以接受XPath作为xsl:param(显示为一个示例默认XPath值(,然后对其进行评估

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:output indent="yes"/>

<xsl:mode on-no-match="shallow-copy"/>

<xsl:param name="path" select="'/Employees/Employee[number(id)[. gt 1 and . lt 4]]'" />

<xsl:template match="Employees">
<xsl:copy>
<xsl:evaluate xpath="$path" context-item="."/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

最新更新