XSLT:如何从两个xml之间获取不同的值

时间:2015-02-24 10:02:39

标签: xml xslt

我的目标我只是希望得到不同的值,我的意思是,第二个xml(通过'document'函数的xml文件)中可用的第10个td值,并且它不应该是可用的在源xml中,这个值我被视为'dintinct'值。我怎么能得到这个?我尝试了上面的代码,它没有提供我期望的完全不同的值。有什么帮助吗?

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>

通过'document'函数获取的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>

1 个答案:

答案 0 :(得分:1)

为什么这不起作用的共鸣是你将所有内容与$check进行比较 - 但$check变量仅包含此内容:

"Live animals."

这是因为substring()函数(与所有字符串函数一样)仅在第一个参数指定的节点集中的第一个节点上运行。

如果要从<documenttitle>节点中删除编号,则必须对每个节点执行此操作。并且您无法使用substring(., 3),因为您的数字超过了9。