2015-02-24 38 views
0

我的目的是爲了獲得不同的值,我的意思是,第二個xml中的td值(xml文件通過'document'函數),它不應該在源xml中可用,這個值我認爲是'dintinct'值。我怎麼能得到這個?我試過上面的代碼,它並沒有提供我期望的完全不同的值。任何幫助?XSLT:如何從兩個xml之間獲取不同的值

XSLT: How to compare two xml files and how to get distinct value from it根據這個問題,我嘗試了下面的輸入xml文件,但是,即使我無法獲得兩個文件之間的不同文本。

例如,根據正確的結果,在這裏,這<distint>Meat and edible meat offal.</distint>不應該來。但是,現在,它來了,這是錯誤的。

XSL文件

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> 

<xsl:output method="xml" indent="yes" encoding="UTF-8"/> 
<xsl:variable name="check" select="normalize-space(substring(//document[@indent='-2']/documenttitle,3))"/> 

<xsl:template match="/">  
<xsl:for-each select="document('Main2.xml')//*[local-name()='tr']/*[local-name()='td'][10][./preceding-sibling::td[5][. != '']]"> 
<xsl:choose> 
<xsl:when test=". = $check"> 
</xsl:when> 
<xsl:otherwise> 
<xsl:text> 

</xsl:text> 
<distint><xsl:value-of select="."/></distint> 
</xsl:otherwise> 
</xsl:choose> 
</xsl:for-each> 

</xsl:template> 
</xsl:stylesheet> 

源XML

<XML> 
    <documents> 
    <document indent="-3"> 
     <document_prefixtitle>Some text</document_prefixtitle> 
<document indent="-2"><documenttitle>1 Live animals.</documenttitle></document> 
<document indent="-2"><documenttitle>2 Meat and edible meat offal.</documenttitle></document> 
<document indent="-2"><documenttitle>3 Fish and crustaceans, molluscs and other aquatic invertebrates.</documenttitle></document> 
<document indent="-2"><documenttitle>4 Dairy produce; birds eggs; natural honey; edible products of animal origin, not elsewhere specified or included.</documenttitle></document> 
<document indent="-2"><documenttitle>5 Products of animal origin, not elsewhere specified or included.</documenttitle></document> 
<document indent="-2"><documenttitle>6 Live trees and other plants; bulbs, roots and the like; cut flowers and ornamental foliage.</documenttitle></document> 
<document indent="-2"><documenttitle>7 Edible vegetables and certain roots and tubers.</documenttitle></document> 
<document indent="-2"><documenttitle>8 Edible fruit and nuts; peel of citrus fruit or melons.</documenttitle></document> 
<document indent="-2"><documenttitle>9 Coffee, tea mat, and spices.</documenttitle></document> 
<document indent="-2"><documenttitle>10 Cereals.</documenttitle></document> 
<document indent="-2"><documenttitle>11 Products of the milling industry; malt; starches; inulin; wheat gluten.</documenttitle></document> 
<document indent="-2"><documenttitle>12 Oil seeds and oleaginous fruits; miscellaneous grains, seeds and fruit; industrial or medicinal plants; straw and fodder.</documenttitle></document> 
<document indent="-2"><documenttitle>13 Example of distint chaptrr fruits; miscellaneous grains</documenttitle></document> 
<document indent="-2"><documenttitle>14 1Example of distint chaptrr fruits; miscellaneous grains</documenttitle></document> 
</document></documents></XML> 

是通過 '文件' 功能拍攝XML是:

<table> 
<tbody> 
<tr> 
<td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Live animals.</td></tr> 

<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Meat and edible meat offal.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Fish and crustaceans, molluscs and other aquatic invertebrates.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Dairy produce; birds eggs; natural honey; edible products of animal origin, not elsewhere specified or included.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Products of animal origin, not elsewhere specified or included.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Live trees and other plants; bulbs, roots and the like; cut flowers and ornamental foliage.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Edible vegetables and certain roots and tubers.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Edible fruit and nuts; peel of citrus fruit or melons.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Coffee, tea mat, and spices.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Cereals.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Products of the milling industry; malt; starches; inulin; wheat gluten.</td></tr> 
<tr><td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>1</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Oil seeds and oleaginous fruits; miscellaneous grains, seeds and fruit; industrial or medicinal plants; straw and fodder.</td></tr> 
<tr> 
<td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>17</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Live animals new sample.</td></tr> 
<tr> 
<td>Yes</td> 
<td>2</td> 
<td>2</td> 
<td>1</td> 
<td>18</td> 
<td>_24</td> 
<td>20141210</td> 
<td>20141210</td> 
<td></td><td>Active Live animals new sample.</td></tr> 

</tbody> 
</table> 

截至目前,我得到的結果如下

<?xml version="1.0" encoding="UTF-8"?> 
<distint>Meat and edible meat offal.</distint> 
<distint>Fish and crustaceans, molluscs and other aquatic invertebrates.</distint> 
<distint>Dairy produce; birds eggs; natural honey; edible products of animal origin, not elsewhere specified or included.</distint> 
<distint>Products of animal origin, not elsewhere specified or included.</distint> 
<distint>Live trees and other plants; bulbs, roots and the like; cut flowers and ornamental foliage.</distint> 
<distint>Edible vegetables and certain roots and tubers.</distint> 
<distint>Edible fruit and nuts; peel of citrus fruit or melons.</distint> 
<distint>Coffee, tea mat, and spices.</distint> 
<distint>Cereals.</distint> 
<distint>Products of the milling industry; malt; starches; inulin; wheat gluten.</distint> 
<distint>Oil seeds and oleaginous fruits; miscellaneous grains, seeds and fruit; industrial or medicinal plants; straw and fodder.</distint> 
<distint>Live animals new sample.</distint> 
<distint>Active Live animals new sample.</distint> 
+0

http://stackoverflow.com/help/someone-answers – 2015-03-17 03:33:50

回答

1

爲什麼這是行不通的振振有辭是,你的一切比較$check - 但$check變量只包含這個:

"Live animals." 

這是因爲子()函數(像所有字符串函數)僅在由第一個參數指定的節點集中的第一個節點上運行。

如果您想從<documenttitle>節點中剝離編號,則必須爲每個節點執行此操作。你不能使用substring(., 3),因爲你的數字高於9.

+0

請你糾正我的代碼上面,否則給這個編碼樣本。 – Sakthivel 2015-02-24 11:48:16

+1

@Sakthivel不,我恐怕這是太多的工作。 – 2015-02-24 11:50:11

+0

其實,我沒有得到你對子串的解釋的最後一行,你能否詳細說明一下? – Sakthivel 2015-02-24 11:55:03

相關問題