(这里的所有代码都是从它的原始版本简化的)
我的公司有一个在 XSLT (1.0) 中使用的函数,它从我们的文件系统的文件中返回内容。我需要能够使用apply-templates
. 考虑以下示例:
主要 XML 文件:
<exhibit>
<exhibitTitle>Exhibit</exhibitTitle>
<linkedAsset href="path/to/file.xml" />
</exhibit>
外部 XML 文件:
<externalAsset editable="true" id="U10250926378W6C">
<img src="path/to/image.png" />
<caption>Some default image</caption>
<externalAsset>
我尝试将以下 XSLT 应用于主 XML 文件:
XSLT:
<xsl:template match="linkedAsset">
<xsl:apply-templates select="cus:getFileByUri(./@href)" />
</xsl:template>
<xsl:template match="img">
<xsl:text>|-- Begin Image Source --|</xsl:text>
<xsl:value-of select="./src" />
</xsl:text>|-- End Image Source --|</xsl:text>
</xsl:template>
结果只是“一些默认图像”。
为了确保我得到一个 XML 结构,而不仅仅是我尝试的所有节点(或其他东西)的值:
<xsl:template match="linkedAsset">
<xsl:copy-of select="cus:getFileByUri(./@href)" />
</xsl:template>
它返回了原始的外部 XML 文件结构:
<externalAsset editable="true" id="U10250926378W6C">
<img src="path/to/image.png" />
<caption>Some default image</caption>
<externalAsset>
我也试过:
<xsl:template match="linkedAsset">
<xsl:value-of select="cus:getFileByUri(./@href)//img/@src" />
</xsl:template>
它按预期返回“path/to/image.png”。
最后,根据这个问题的答案,我尝试了以下 XSLT:
<xsl:template match="linkedAsset">
<xsl:call-template name="renderExternal">
<xsl:with-param name="asset" select="cus:getFileByUri(./@href)" />
</xsl:call-template>
</xsl:template>
<xsl:template name="renderExternal">
<xsl:param name="asset" select="." />
<xsl:apply-templates select="$asset" />
</xsl:template>
输出与原始的相同apply-template
。
有什么方法可以应用于apply-templates
从函数返回的值?我可以清楚地将字符串发送到copy-of
, value-of
,甚至对其执行 xpaths;我可以根本不使用它apply-templates
吗?
选择答案的解释
事实证明,我的问题的解决方案非常具体(我将模板应用到与该相同模板匹配的节点,而这在我提供的代码的简化版本中并不清楚)。我真的在这个上赢得了我的-1。 无论如何,我觉得keshlam的回答对将来访问这个问题的人最有帮助,因为它回答了我认为我的问题是什么。