2015-04-23 122 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>