我有以下文件集:
SourceFile.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee id="1">
<firstname relationship="headnote">Atif</firstname>
<lastname relationship="lname">Bashir</lastname>
<age relationship="age">32</age>
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
ParamerterSettings.xml
<?xml version="1.0" encoding="utf-8"?>
<Settings>
<Employee id="1">
<sourceFile>Lookup1.xml</sourceFile>
<sourceXpathfield>Employees/Employee[@id</sourceXpathfield>
<lookupXpathfield>Employees/Employee[@id='1']</lookupXpathfield>
<elementstoinsert>xyz</elementstoinsert>
</Employee>
</Settings>
Run Code Online (Sandbox Code Playgroud)
Lookup.xml
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee id="1">
<department code="102">HR</department>
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
transform.xsl
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:include href="identity.xsl"/>
<xsl:param name="EmployeeId" select="'1,2'" />
<xsl:variable name="FileSettings" select="document('test3.xml')" />
<xsl:variable name="SuppressSetting" select="$FileSettings/Settings/Employee[@id = tokenize($EmployeeId, ',')]" />
<xsl:template match="Employee">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="publisher" />
<xsl:apply-templates select="node() except publisher"/>
<xsl:variable name="outerfile" select="document($SuppressSetting/sourceFile)"></xsl:variable>
<xsl:variable name="outerfiledetails" select="$outerfile/$SuppressSetting/lookupXpathfield"></xsl:variable>
<xsl:value-of select="$outerfiledetails"></xsl:value-of>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
输出应该是:
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee id="1">
<firstname relationship="headnote">Atif</firstname>
<lastname relationship="lname">Bashir</lastname>
<age relationship="age">32</age>
HR
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
我在Transform.xsl中更改了以下行
<xsl:variable name="outerfiledetails" select="$outerfile/$SuppressSetting/lookupXpathfield"></xsl:variable>
Run Code Online (Sandbox Code Playgroud)
成
<xsl:variable name="outerfiledetails" select="$outerfile/Employees/Employee[@id='1']"></xsl:variable>
Run Code Online (Sandbox Code Playgroud)
然后我得到我的输出,但我想保持的XPath epression两个SourceFile.xml和Lookup.xml成ParamerterSettings.xml,这样我可以写一个更通用的脚本.这可以通过动态xpath以任何其他方式完成吗?任何暗示相同的想法或暗示都将受到高度赞赏.
Dim*_*hev 11
纯XSLT 1.0或2.0中无法进行动态XPath评估.
在"混合"解决方案中至少有三种方法可以做到这一点:
I.使用EXSLT功能 dyn:evaluate()
不幸的是,很少有XSLT 1.0处理器实现dyn:evaluate().
II.使用XSLT处理XML文档并生成包含XPath表达式的新XSLT文件 - 然后执行新生成的转换.
很少有人这样做,在我看来,这比下一个解决方案更复杂.
III.该方式中的XPath可视化工具的工作原理
这个想法是:
在XSLT样式表中定义一个全局变量,如下所示:
<xsl:variable name="vExpression" select="dummy"/>
Run Code Online (Sandbox Code Playgroud)然后,加载样式表作为使用DOM的XML文档,并替换select属性的的vExpression与被包含在所述源XML文档中的实际的XPath表达式的变量.
最后,使用加载到内存和动态更新的xslt样式表启动转换.