2015-04-23 203 views
0

我已经为我的问题寻找解决方案。我发现了一些类似的问题和答案,但没有一个适合我的问题。将两个XML文件与XSLT合并

我是一个XML新手,从未使用过XSLT。我有Linux,可以使用xsltproc或xmllint(或其他最好的)。

问题很简单。我必须使用相同布局的XML文件。开始时是包含在一个文件中的节点的计数器。我只需要添加这两个文件的计数器,然后将这两个文件中的所有节点作为单个列表。 (排序就更好了。)

例子: A.XML

<?xml version="1.0" standalone="yes"?> 
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/List.xsd"> 
    <publshInformation> 
    <Publish_Date>12/17/2014</Publish_Date> 
    <Record_Count>115</Record_Count> 
    </publshInformation> 
    <Entry> 
    <uid>9639</uid> 
    <firstName>Bob</firstName> 
.... 
    </Entry> 
</List> 

B.XML

<?xml version="1.0" standalone="yes"?> 
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/List.xsd"> 
    <publshInformation> 
    <Publish_Date>12/17/2014</Publish_Date> 
    <Record_Count>100</Record_Count> 
    </publshInformation> 
    <Entry> 
    <uid>4711</uid> 
    <firstName>John</firstName> 
.... 
    </Entry> 
</List> 

结果: out.xml

<?xml version="1.0" standalone="yes"?> 
<List xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/List.xsd"> 
    <publshInformation> 
    <Publish_Date>12/17/2014</Publish_Date> 
    <Record_Count>215</Record_Count> 
    </publshInformation> 
    <Entry> 
    <uid>4711</uid> 
    <firstName>John</firstName> 
.... 
    </Entry> 
    <Entry> 
    <uid>9639</uid> 
    <firstName>Bob</firstName> 
.... 
    </Entry> 
</List> 

哪有我管理?我不会在这里发布我的XSLT,因为他们不工作,这是因为我的技能有限。感谢您的任何建议!

+0

http://stackoverflow.com/help/someone-answers –

回答

0

试试这个方法。这里的想法是,您将XSL转换应用于文档a.xml,并将该路径作为参数传递给b.xml文件。

您可能会想要更改节点以排序更合理。

请注意,使用前缀来解决XML源中的节点问题,因为它们都在名称空间中。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns1="http://tempuri.org/List.xsd"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="doc2" select="'b.xml'" /> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="ns1:Record_Count"> 
    <xsl:copy> 
     <xsl:value-of select=". + document($doc2)/ns1:List/ns1:publshInformation/ns1:Record_Count" /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="ns1:List"> 
    <xsl:copy> 
     <xsl:apply-templates select="*|document($doc2)/ns1:List/ns1:Entry"> 
      <xsl:sort select="ns1:firstName" data-type="text" order="ascending"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet>