2011-05-07 242 views
1

我需要使用XSLT将几个XML文件合并为一个。我有4个XML文件发布,接收,theatre1和theatre2。版本必须先被添加,然后其匹配的接收必须被放置在版本部分的内部。其他两个应该被添加。如何使用xslt将几个xml文件合并到一个xml文件中?

这里是文件的格式。 发布: 文本

接待: 文本

的结果应该是: < 文本 文本

这里是我到目前为止,但它不能正常工作完全

其他2个文件只需要在最后

+0

您没有提供任何的XML文件!请改正。 – 2011-05-07 16:14:09

回答

2

Reading Multiple Input Documents似乎回答这个问题以复加。

当你运行一个XSLT处理器,你告诉它在哪里可以找到源代码树中的文件 - 可能是在本地或远程计算机上的磁盘文件 - 和样式表适用于它。您不能让处理器一次将样式表应用于多个输入文档。然而,document()函数允许样式表命名一个附加文档来读入。您可以将整个文档插入到结果树中,或者根据XPath表达式描述的条件插入其中的一部分。您甚至可以使用xsl:key指令和key()函数在源文档之外的文档中查找关键值。

因此,在xslt中加载多个文档的关键是使用document()函数。

3

下面是如何进行的:

$ expand -t2 release.xml 
<release name="bla"/> 

$ expand -t2 reception.xml 
<receptions> 
    <reception name="bla"> 
    <blabla/> 
    </reception> 
    <reception name="blub"> 
    <blubbel/> 
    </reception> 
</receptions> 

$ expand -t2 theatre1.xml 
<theatre number="1"/> 

$ expand -t2 theatre2.xml 
<theatre number="2"/> 

$ expand -t2 release.xsl 
<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:strip-space elements="*"/><!-- make output look nice --> 
    <xsl:output indent="yes"/> 

    <xsl:template match="release"> 
    <xsl:variable name="rel-name" select="@name"/> 
    <xsl:copy> 
     <xsl:copy-of select="node()"/><!-- copy remainder of doc --> 
     <xsl:copy-of select="document('release.xml')"/> 
     <xsl:variable name="rcpt-doc" select="document('reception.xml')"/> 
     <xsl:copy-of select="$rcpt-doc/*/reception[ @name = $rel-name ]"/> 
     <xsl:copy-of select="document('theatre1.xml')"/> 
     <xsl:copy-of select="document('theatre2.xml')"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

这样称呼它:

xsltproc release.xsl release.xml 

这是结果:

<?xml version="1.0"?> 
<release> 
    <release name="bla"/> 
    <reception name="bla"> 
    <blabla/> 
    </reception> 
    <theatre number="1"/> 
    <theatre number="2"/> 
</release> 
相关问题