Как удалить определенные атрибуты из всех потомков в XML? - PullRequest
1 голос
/ 27 февраля 2012

У меня есть XML как

var test:XML = new XML( <record id="5" name="AccountTransactions"
    <field id="34" type="Nuber"/>
    </record>);

Я хочу удалить все атрибуты, кроме идентификатора, и ввести все узлы XML.по этому коду я не могу этого сделать.Можете ли вы предложить лучшее решение, кроме циклов.

var atts:XMLListCollection = new XMLListCollection(test.descendants().attributes().((localName() != "id") && (localName() != "type")));
atts.removeAll(); trace(test)

он по-прежнему показывает все атрибуты: /

Ответы [ 2 ]

1 голос
/ 27 февраля 2012
    var test:XML = new XML( '<record id="5" name="AccountTransactions"><field id="34" type="Nuber" score="ded"/><field id="35" type="Nuber" score="sc"/></record>');
    var attributes:XMLList = test.field.@*;
    var length:int = attributes.length();
    for (var i:int = 0; i < length; i++) {
        (attributes[i].localName() != "id" && attributes[i].localName() != "type") ? [delete attributes[i], length--] : void;
    }
    trace(test);
1 голос
/ 27 февраля 2012
var xml:XML = new XML(
    <record id="5" name="AccountTransactions">
        <field id="34" type="Number">
            <test id="0"/>
        </field>
    </record>);

//make array of attribute keys, excluding "id" and "type"
var attributesArray:Array = new Array();
for each (var attribute:Object in xml.attributes())
{
    var attributeName:String = attribute.name();
    if (attributeName != "id" && attributeName != "type")
    {
        attributesArray.push(attributeName);
    }
}

//loop through filtered attributes and remove them from the xml
for each (var attributeKey:String in attributesArray)
{
    delete xml.@[attributeKey];
    delete xml.descendants().@[attributeKey];
}
...