E4X选择节点,其中的后裔可以是A或B或A和与放大器;乙放大器、后裔、节点、E4X

2023-09-08 14:07:36 作者:我把心寄错了地址

所以我有这样的XML结构:

So I have this XML structure:

<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。

Using the following E4X query I get only item "aaa" and item "bbb".

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

不过,我可以种明白为什么我没有看到产品CCC,因为它既是ID =1和安培;&安培; 2

However, I can kind of see why I'm not seeing item "ccc" because it is BOTH id="1" && "2"

所以,真的不知道什么是正确的查询应该在这里,即使后代是正确的方法。

So not really sure what the correct query should be here, and even if descendants is the correct technique.

我不希望最后做长的附加ID =1和安培;&安培;的id =2查询或者是因为我有无限的组合这些值(2&安培;&安培;3,1&安培;&安培;2&安培;&安培;3)等。

I don't want to end up doing long additional id="1" && id="2" queries either because I have unlimited combinations of these values ("2" && "3", "1" && "2" && "3") etc..

任何想法将是最有帮助的。

Any thoughts would be most helpful..

感谢

所以帕特里克解决了这个本EX pression:

So Patrick solved this with this expression:

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

不过,考虑这一步,你将如何动态地创建@id值,因为这将是一个不断变化的查询,这取决于用户的选择。

However, taking this one step further, how would dynamically create the @id values, because this will be a changing query depending on user selections.

像这样的东西(但,但是这不工作):

Something like this (but this, but this doesn't work):

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

还有什么想法帕特里克..任何人?

Any more thoughts Patrick.. anyone?

感谢

推荐答案

有关的或者搜索,您可以简单地做:

For the Or search you can simply do :

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

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

Using a custom filter function you can do your And search that is more complicated than an 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"] ) ) );