我要合并 XML 文件(并添加元信息),其相对路径在我的输入 XML 文件中指定。我要合并的文件位于名为"file"的子目录中 输入文件的结构如下
<files>
<file>
<path>files/firstfile.xml</path>
</file>
<file>
<path>files/secondfile.xml</path>
</file>
</files>
firstfile.xml 和 secondfile.xml 的结构如下
<tables>
<table name = "...">
...
</table>
...
<tables>
我想将一个文件的所有表节点放在一个组中,并向其中添加元信息。所以我编写了以下 XSLT 样式表:
<xsl:template match="/">
<tables>
<xsl:apply-templates/>
</tables>
</xsl:template>
<xsl:template name = "enrichWithMetaInformation" match = "file">
<xsl:apply-templates select="document(./path)/tables">
<xsl:with-param name="file" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="tables">
<group>
<meta>
...Some meta data ...
</meta>
<xsl:copy-of select="./table"/>
</group>
</xsl:template>
对于每个文件我都会得到一个错误:
The system could not find the file specified.
它表示已返回一个空节点集(因此无法加载文件)。有人知道如何解决这个问题吗?
干杯
最佳答案
检查源文档的基本 URI 是否已知,例如通过执行 base-uri(/)。在参数作为节点(或节点集)提供的情况下,此值用于解析传递给 document() 的相对 URI。
基本 URI 未知的两种常见情况是:
(a) 如果您将源文档作为内存中的 DOM 提供
(b) 如果您将源文档作为输入流或没有已知 URI 的阅读器提供。
关于xml - XSLT 打开其他 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18469398/