2011-04-05 65 views
1

專家您好 我有2個XML文件,例如:如何比較兩個xml文件與xslt?

<college> 
    <student> 
     <name>amit</name> 
     <file>/abc/kk/final.c</file> 
     <rollno>22</rollno> 
     <function>a()</function> 
    </student> 
    <student> 
     <name>sumit</name> 
     <file>/abc/kk/up.h</file> 
     <rollno>23</rollno> 
     <function>b()</function> 
    </student> 
    <student> 
     <name>nikhil</name> 
     <file>/xyz/up.cpp</file> 
     <rollno>24</rollno> 
     <function>c()</function> 
    </student> 
    <student> 
     <name>bharat</name> 
     <file>/abc/kk/down.h</file> 
     <rollno>25</rollno> 
     <function>d()</function> 
    </student> 
    <student> 
     <name>ajay</name> 
     <file>/simple/st.h</file> 
     <rollno>27</rollno> 
     <function>e()</function> 
    </student> 
</college> 

第二XML文件

<college> 
    <student> 
     <name>amit</name> 
     <file>/abc/kk/final.c</file> 
     <function>a()</function> 
    </student> 
    <student> 
     <name>nikhil</name> 
     <file>/xyz/up.cpp</file> 
     <function>c()</function> 
    </student> 
    <student> 
     <name>ajay</name> 
     <file>/simple/st.h</file> 
     <function>e()</function> 
    </student> 
</college> 

我想比較兩個XML文件,這樣,我們會得到這些節點的輸出這不常見。由於我是xslt新手,請爲我提供解決方案。 我使用:

<xsl:for-each select="document('1.xml')/college/student[ 
         starts-with(file, '/abc/kk') 
         ]"> 
    <xsl:for-each select="document('2.xml')/college/student"> 
     <xsl:if test="string(document('1.xml')/college/student/function) 
        != string(document('2.xml')/college/student/function)"> 
      <tr> 
       <td> 
        <xsl:value-of 
        select="document('1.xml')/college/student/name"/> 
       </td> 
       <td> 
        <xsl:value-of 
        select="document('1.xml')/college/student/file"/> 
       </td> 
       <td> 
        <xsl:value-of 
        select="document('1.xml')/college/student/function"/> 
       </td> 
      </tr> 
     </xsl:if> 
    </xsl:for-each> 
</xsl:for-each> 
+0

你要具有鮮明的字符串值的節點。在這裏有很多關於此事的問題。一些甚至發佈輸入樣本! – 2011-04-05 11:56:44

+0

這是一個BAD問題的例子 - 必須提供兩個XML文檔。我已經投票給這個問題。請編輯它並顯着提高! – 2011-04-05 13:02:05

回答

0

使用XSLT將兩個XML文件轉換成包含只是要比較的信息兩個滑動的文本文件。

然後只是diff這兩個純文本列表。

不要忘記sortdiff比較之前的名單。

+0

感謝您的回覆......但我可以在.xsl文件上做到這一點,而無需轉換成文本。我在xsl文件中使用兩個for循環(for-each),然後使用「if」檢查條件,但沒有得到結果..plz建議。 – kuldeep 2011-04-05 12:36:28

+0

你可以發佈源代碼嗎? – Giacomo 2011-04-05 12:41:16

0

你想眼前這個XPath表達式:

document('1.xml')/college/student[ 
    starts-with(file, '/abc/kk') 
][ 
    not(
     function = document('2.xml')/college/student/function 
    ) 
] 

爲了證明這一點,這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <table> 
      <xsl:apply-templates 
      select="college/student[ 
         starts-with(file, '/abc/kk') 
        ][ 
         not(
          function=document('2.xml')/college/student/function 
         ) 
        ]"/> 
     </table> 
    </xsl:template> 
    <xsl:template match="student"> 
     <tr> 
      <xsl:apply-templates/> 
     </tr> 
    </xsl:template> 
    <xsl:template match="student/*"> 
     <td> 
      <xsl:apply-templates/> 
     </td> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<table> 
    <tr> 
     <td>sumit</td> 
     <td>/abc/kk/up.h</td> 
     <td>23</td> 
     <td>b()</td> 
    </tr> 
    <tr> 
     <td>bharat</td> 
     <td>/abc/kk/down.h</td> 
     <td>25</td> 
     <td>d()</td> 
    </tr> 
</table>