在 XSLT 2.0 中动态创建 XPath



我想使用位于我的输入 xml 中的一些动态 xpath 从查找 xml 中获取部分 xml。 当我尝试添加硬编码的路径时,它运行良好并为我获得了所需的结果。 但是当我尝试对变量进行相同的操作时,它不起作用。

输入

<root>
<path
location="/legitem/front[1]/title[1]">
<msg>TITLE</msg>
</path>
<path
location="/legitem/front[1]/longtitle[1]">
<msg>LONGTITLE</msg>
</path>
</root>

XSLT2.0

<xsl:variable name="fetch_content" select="document('source.xml')/legitem/front[1]/title[1]" as="node()"/>
<!-- working for above fixed xpath -->
<xsl:copy-of select="$fetch_content"/>
<!-- OP : correct -->
--copies the content of <title>

但是在尝试使用变量进行动态时不获取内容

<xsl:variable name="LookUpXml" select="document('source.xml')" />
<xsl:variable name="loc" select="substring-after(@location, '/legitem/')"/>
<xsl:copy-of select="$main_file//$loc"/>
<!-- OP : wrong -->
front[1]/title[1]
front[1]/longtitle[1]

如果有人对我如何实现这一目标有任何想法,将不胜感激!谢谢。。。

您可以通过匹配节点的 name(( 来做到这一点,但这取决于路径中有多少是动态的

<xsl:copy-of select="$main_file//front[1]/*[name()=$loc][1]" />

最新更新