连接xml文件 [英] Concatenating xml files
问题描述
我有几个 xml 文件,它们的名称存储在另一个 xml 文件中.
I have several xml files, the names of which are stored in another xml file.
我想使用 xsl 生成 xml 文件组合的摘要.我记得有一种方法可以使用 msxml 扩展来做到这一点(我使用的是 msxml).
I want to use xsl to produce a summary of the combination of the xml files. I remember there was a way to do this with the msxml extensions (I'm using msxml).
我知道我可以使用 select="document(filename)"
获取每个文件的内容,但我不确定如何将所有这些文件合并为一个.
I know I can get the content of each file using select="document(filename)"
but I'm not sure how to combine all these documents into one.
2008 年 10 月 21 日我应该提到我想对组合的 xml 进行进一步处理,因此仅从转换中输出它是不够的,我需要将其存储为变量中的节点集.
21-Oct-08 I should have mentioned that I want to do further processing on the combined xml, so it is not sufficient to just output it from the transform, I need to store it as a node set in a variable.
推荐答案
这里只是一个小例子,说明您可以做什么:
Here is just a small example of what you could do:
file1.xml:
<foo>
<bar>Text from file1</bar>
</foo>
file2.xml:
<foo>
<bar>Text from file2</bar>
</foo>
index.xml:
<index>
<filename>file1.xml</filename>
<filename>file2.xml</filename>
summarize.xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:variable name="big-doc-rtf">
<xsl:for-each select="/index/filename">
<xsl:copy-of select="document(.)"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="big-doc" select="exsl:node-set($big-doc-rtf)"/>
<xsl:template match="/">
<xsl:element name="summary">
<xsl:apply-templates select="$big-doc/foo"/>
</xsl:element>
</xsl:template>
<xsl:template match="foo">
<xsl:element name="text">
<xsl:value-of select="bar"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
将样式表应用到 index.xml 会给你:
Applying the stylesheet to index.xml gives you:
<?xml version="1.0" encoding="UTF-8"?><summary><text>Text from file1</text><text>Text from file2</text></summary>
诀窍是使用 document 函数(几乎所有 XSLT 1.0 处理器都支持的扩展函数)加载不同的文档,将内容作为变量体的一部分输出,然后将变量转换为节点集以供进一步处理.
The trick is to load the different documents with the document function (extension function supported by almost all XSLT 1.0 processors), to output the contents as part of a variable body and then to convert the variable to a node-set for further processing.
这篇关于连接xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!