加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – E4X选择节点,其中后代可以是A OR B或A \u0026\u0

发布时间:2020-12-16 22:44:19 所属栏目:百科 来源:网络整理
导读:所以我有这个 XML结构: ItemsItem name="aaa" ProductRanges ProductRange id="1" / /ProductRanges/ItemItem name="bbb" ProductRanges ProductRange id="2" / /ProductRanges/ItemItem name="ccc" ProductRanges ProductRange id="1" / ProductRange id="
所以我有这个 XML结构:

<Items>
<Item name="aaa">
    <ProductRanges>
        <ProductRange id="1" />
    </ProductRanges>
</Item>
<Item name="bbb">
    <ProductRanges>
        <ProductRange id="2" />
    </ProductRanges>
</Item>
<Item name="ccc">
    <ProductRanges>
        <ProductRange id="1" />
        <ProductRange id="2" />
    </ProductRanges>
</Item>
</Items>

使用以下E4X查询,我只获得项目“aaa”和项目“bbb”.

trace(   Items.Item.(descendants("ProductRange").@id == "1" || descendants("ProductRange").@id == "2")   );

但是,我可以理解为什么我没有看到项目“ccc”,因为它是两个id =“1”&& “2”

所以不确定这里应该是什么正确的查询,即使后代是正确的技术.

我不想结束长时间的额外id =“1”&& id =“2”查询要么是因为我有这些值的无限组合(“2”和“&”3“,”1“和&&”2“&&”3“)等.

任何想法都会有所帮助..

谢谢

所以Patrick用这个表达式解决了这个问题

xml.Item.(descendants('ProductRange').(@id=="1" || @id=="2").length()>0);

但是,更进一步,如何动态创建@id值,因为这将是一个不断变化的查询,具体取决于用户选择.

像这样的东西(但这,但这不起作用):

var attributeValues:String = "@id=="1" || @id=="2" || @id=="3" || @id=="4"";
xml.Item.(descendants('ProductRange').(attributeValues).length()>0);

更多的想法帕特里克..任何人?

谢谢

解决方法

对于或搜索,您可以简单地执行:

xml.Item.(descendants('ProductRange').(@id=="1" || @id=="2").length()>0);

使用自定义过滤器功能,你可以做你的搜索比Or更复杂:

var xml:XML=<Items>
<Item name="aaa">
    <ProductRanges>
        <ProductRange id="1" />
    </ProductRanges>
</Item>
<Item name="bbb">
    <ProductRanges>
        <ProductRange id="2" />
    </ProductRanges>
</Item>
<Item name="ccc">
    <ProductRanges>
        <ProductRange id="1" />
        <ProductRange id="3" />
        <ProductRange id="2" />
    </ProductRanges>
</Item>
</Items>;

function andSearch(node:XMLList,searchFor:Array) {
    var mask:int = 0;
    var match:int = (1 << searchFor.length ) - 1;
    var fn:Function = function(id:String) {
        var i:int = searchFor.indexOf(id);
        if (i >= 0) {
            mask = mask | (1<<i);
        }
        return mask==match;
    }
    node.(ProductRange.(fn(@id)));
    return mask==match;
}

trace( xml.Item.( andSearch( ProductRanges,["1","2"] ) ) );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读