XSLT在多种条件下过滤元素



我有一个大XML文件,我想根据多个条件过滤指定的元素,特别是:

<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"one_answers" apxple"。

这是我的XSLT失败的地方,看起来像是操作员或操作员。

您的当前表达式错误..."不是(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中有效。

最新更新