我已经follwing文件集:xslt中的动态xpath?
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>
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>
Lookup.xml
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee id="1">
<department code="102">HR</department>
</Employee>
</Employees>
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>
输出应该是:
<?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>
我改变了下面的线Transform.xsl
<xsl:variable name="outerfiledetails" select="$outerfile/$SuppressSetting/lookupXpathfield"></xsl:variable>
到
然后我得到我的输出,但我想将SourceFile.xml
和Lookup.xml
的XPath表达式保留为ParamerterSettings.xml
,以便我可以编写更通用的脚本。这可以通过任何其他方式完成,然后动态xpath?任何想法或暗示推动相同将高度赞赏。
这是你已经简化您最初的几乎可怕的问题了良好的进展,但这个问题仍然过于复杂和不明确。尝试改变它并进一步简化它 - 我相信你不需要所有的细节。尤其是,如果要处理两个以上的文件,每个人都会放弃尝试理解问题。太复杂了:我永远不会用这种方式设计XSLT应用程序,并相信我,我有XSLT应用程序的复杂性非常具有挑战性,99%的开发人员不相信XSLT可能会这样做。 – 2011-01-07 20:58:20
嗨Dimitre,我想要的是从外部文件执行xpath值。原因是我有多个exeternal文件,我想从中获取数据并将该数据插回到主源文件中。我可以通过对多个模板进行硬编码来实现,但我想避免这种情况,并根据不同的连接或xpath值将多个文件中的一个模板定义为外部文件中的设置。 – atif 2011-01-08 02:26:49
@ Nick-Jones的答案是正确的:这不能在XSLT/XPath 2.0中完成,它可能由下一个版本提供。但是,我非常怀疑动态XPath评估的必要性 - 如果您很好地描述了您的问题,那么可能有一个解决方案不需要这样做。为什么不直接以最简单的形式提出问题:“我如何评估此XML文档中包含的这个表达式?”。虽然纯粹的XSLT解决方案是不可能的,但至少有三种不同的“混合”解决方案,我知道这个问题。 – 2011-01-10 14:18:30