2017-03-09 98 views
1

我有一个大的XML文件,我希望根据多个条件来过滤掉特定的元素,特别是:XSLT过滤元件与多个条件

<title> contains 'K17' AND <category> contains 'APPLE' 

于是我开始是这样的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:g="http://base.google.com/ns/1.0"> 
<xsl:output omit-xml-declaration="no" indent="yes" method="xml"/> 

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

<xsl:template match="item[not(title/contains(text(),'K17')) and not(category/contains(text(),'APPLE'))]" /> 

</xsl:stylesheet> 

测试XML:

<channel> 
    <item> 
     <title>K170001 Test 1</title> 
     <category><![CDATA[APPLE &gt; MOBILE &gt; IPHONE]]></category> 
    </item> 
    <item> 
     <title>K170002 Test 2</title> 
     <category><![CDATA[APPLE &gt; MOBILE &gt; IPHONE]]></category> 
    </item> 
    <item> 
     <title>K1x70003 Test 3</title> 
     <category><![CDATA[APPLE &gt; MOBILE &gt; IPHONE]]></category> 
    </item> 
    <item> 
     <title>K170004 Test 4</title> 
     <category><![CDATA[APxPLE &gt; MOBILE &gt; IPHONE]]></category> 
    </item> 
</channel> 

最后两个元素应该是不会在最终的XML,贝科使用'K1x70003'和'APxPLE'。

而这正是我的XSLT失败,它看起来像AND运算符可以作为一个OR操作。

回答

2

您当前的表达有错误的逻辑......“没有(X),而不是(Y)”是一样的“不(X或Y)”当你想“不(X和Y)”(其是一样的 “没有(X)或没有(Y)”)

试试这个(注意text()是不是真的有必要在这里)

<xsl:template match="item[not(contains(title,'K17') and contains(category,'APPLE'))]" /> 

或者即将....

<xsl:template match="item[not(contains(title,'K17')) or not(contains(category,'APPLE'))]" /> 

请注意,语法title/contains(text(),'K17'))应该只在XSLT 2.0中有效。