2009-10-02 27 views
8

Tôi có hai tệp xml. Tôi cần phải hợp nhất chúng lại với nhau trong đó phần tử "myid" trùng khớp giữa hai. Hãy có một cái nhìn tại những tập tin ví dụ ...XSLT: Một cách đơn giản để hợp nhất các tệp xml

File1.xml:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<catalog> 
    <data> 
    <title>Title1</title> 
    <description>Description1</description> 
    <myid>1</myid> 
    </data> 

    <data> 
    <title>Title2</title> 
    <description>Description2</description> 
    <myid>2</myid> 
    </data> 
</catalog> 

File2.xml:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<catalog> 
    <data> 
    <author>Author1</author> 
    <date>12/34/5678</date> 
    <myid>1</myid> 
    </data> 

    <data> 
    <author>Author2</author> 
    <date>87/65/4321</date> 
    <myid>2</myid> 
    </data> 
</catalog> 

Các tập tin kết quả sẽ trông giống như:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<catalog> 
    <data> 
    <title>Title1</title> 
    <description>Description1</description> 
    <myid>1</myid> 
    <author>Author1</author> 
    <date>12/34/5678</date> 
    </data> 

    <data> 
    <title>Title2</title> 
    <description>Description2</description> 
    <myid>2</myid> 
    <author>Author2</author> 
    <date>87/65/4321</date> 
    </data> 
</catalog> 
+0

Liên quan: http://stackoverflow.com/questions/1430710/two-xml-in-one-xslt –

+1

@dacracot: Các tệp đầu vào cũng không được định dạng. -------- @ nicholas.alipaz: Có phải chúng ta đang xem đoạn trích, chứ không phải toàn bộ tập tin? –

+0

Tôi đã cập nhật bài đăng của mình. Xin lỗi vì sự nhầm lẫn. –

Trả lời

4

Tôi đã nghiên cứu một chút và tìm thấy một câu hỏi tương tự ở đây: http://forums.tizag.com/showthread.php?p=76699

Dưới đây là những gì tôi đã đưa ra, điều này dường như chủ yếu là làm việc ngoại trừ Firefox không nhận ra nó như một tệp xml mặc dù tôi đã thêm xml: output.

File1.xml (lưu ý dòng hai, tham khảo sự biến đổi của chúng tôi):

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="merge.xsl"?> 
<catalog> 
    <data> 
    <title>Title1</title> 
    <description>Description1</description> 
    <myid>1</myid> 
    </data> 

    <data> 
    <title>Title2</title> 
    <description>Description2</description> 
    <myid>2</myid> 
    </data> 
</catalog> 

File2.xml:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<catalog> 
    <data> 
    <author>Author1</author> 
    <date>12/34/5678</date> 
    <myid>1</myid> 
    </data> 

    <data> 
    <author>Author2</author> 
    <date>87/65/4321</date> 
    <myid>2</myid> 
    </data> 
</catalog> 

merge.xsl:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" /> 
    <xsl:variable name="with" select="'File2.xml'" /> 

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

    <xsl:template match="scene"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()" /> 
     <xsl:variable name="info" select="document($with)/catalog/data[myid=current()/myid]/." /> 
     <xsl:for-each select="$info/*"> 
     <xsl:if test="name()!='myid'"> 
      <xsl:copy-of select="." /> 
     </xsl:if> 
     </xsl:for-each> 
    </xsl:copy> 
    </xsl:template> 
</xsl:transform> 

Output xml khi xem File1.xml:

<catalog> 
    <data> 
    <title>Title1</title> 
    <description>Description1</description> 
    <myid>1</myid> 
    <author>Author1</author> 
    <date>12/34/5678</date> 
    </data> 

    <data> 
    <title>Title2</title> 
    <description>Description2</description> 
    <myid>2</myid> 
    <author>Author2</author> 
    <date>87/65/4321</date> 
    </data> 
</catalog> 
+0

404; bài viết không tồn tại nữa –

+0

hi C# code xin vui lòng cho điều này – Lijo

+0

@AdamLynch xin lỗi, tôi đã tìm một phiên bản được lưu trong bộ nhớ cache nhưng đã xuất hiện trống. Nó không thực sự quá quan trọng vì tôi phác họa mọi thứ cần thiết ở trên. Tốt! –

Các vấn đề liên quan