XML检测多个子节点的内容并根据它删除父节点

时间:2017-10-26 08:01:23

标签: javascript jquery xml

这是我的xml:

<?xml version='1.0'?>
<rss version="2.0">
    <channel>
        <title>Some Title</title>
        <pubDate>1/1/17 00:00:00</pubDate>
        <generator>SomeOne</generator>
        <item>
            <title>
                Some Item Title 1
            </title>
            <link>http://example.org</link>
            <description>
                Lorem ipsum ...
            </description>
            <pubDate>1/1/17 00:00:00</pubDate>
            <category>Cat1</category>
            <category>Cat2</category>
        </item>
        <item>
            <title>
                Some Item Title 2
            </title>
            <link>http://example.org</link>
            <description>
                Lorem ipsum ...
            </description>
            <pubDate>1/1/17 00:00:00</pubDate>
            <category>Cat2</category>
        </item>
        <item>
            <title>
                Some Item Title 3
            </title>
            <link>http://example.org</link>
            <description>
                Lorem ipsum ...
            </description>
            <pubDate>1/1/17 00:00:00</pubDate>
            <category>Cat1</category>
        </item>
        ....
        ....
        ....
    </channel>
</rss>

我正在使用此jQuery代码删除父母:

var myCat = 'Cat1';
rss.find('item').find('category').filter(':not(:contains('+ myCat +'))').parent().remove();

现在的问题是,正如你在我提供的xml中看到的那样。单个,<item>包含2个<category>,其中一个匹配myCat,但<item>仍然被删除,但不应该删除。 jsFiddle

P.S。从给定的示例中,只应删除第二个<item>

1 个答案:

答案 0 :(得分:0)

你应该循环你的物品,看看每个物品是否包含你的猫:

&#13;
&#13;
var str = `<?xml version='1.0'?>
<rss version="2.0">
    <channel>
        <title>Some Title</title>
        <pubDate>1/1/17 00:00:00</pubDate>
        <generator>SomeOne</generator>
        <item>
            <title>
                Some Item Title 1
            </title>
            <link>http://example.org</link>
            <description>
                Lorem ipsum ...
            </description>
            <pubDate>1/1/17 00:00:00</pubDate>
            <category>Cat1</category>
            <category>Cat2</category>
        </item>
        <item>
            <title>
                Some Item Title 2
            </title>
            <link>http://example.org</link>
            <description>
                Lorem ipsum ...
            </description>
            <pubDate>1/1/17 00:00:00</pubDate>
            <category>Cat2</category>
        </item>
        <item>
            <title>
                Some Item Title 3
            </title>
            <link>http://example.org</link>
            <description>
                Lorem ipsum ...
            </description>
            <pubDate>1/1/17 00:00:00</pubDate>
            <category>Cat1</category>
        </item>
    </channel>
</rss>`;

var rss = $.parseXML(str);
rss = $(rss);

var myCat = 'Cat1';
rss.find('item').each(function(){
  if($(this).find('category:contains('+ myCat +')').length == 0){
    $(this).remove();
  }
});

console.log(rss.find('item').length);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;