【问题标题】:XML detect the content of multiple children and delete parent based on itXML检测多个children的内容并根据它删除parent
【发布时间】:2017-10-26 08:01:23
【问题描述】:

这是我的 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 中看到的那样。单一的,&lt;item&gt; 包含 2 个 &lt;category&gt;,其中一个与 myCat 匹配,但 &lt;item&gt; 仍然被删除,虽然它不应该被删除。 jsFiddle

附:从给定的示例中,仅应删除第二个 &lt;item&gt;

【问题讨论】:

    标签: javascript jquery xml


    【解决方案1】:

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

    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);
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多