Атрибут доступа на узле с пространством имен

У меня есть xml, который я вставил в сообщение, и я хочу использовать groovy XmlSlurper для доступа к языку узлов с атрибутом xlink:href, который не является нулевым/пустым. Я прочитал xml с помощью XmlSlurper следующим образом:

 def node = xmlSlurper.parseText(xml).declareNamespace(
                xlink:"http://www.w3.org/1999/xlink"
        );

затем я пытаюсь получить доступ ко всем узлам с именем и атрибутом и обработать этот атрибут:

node.depthFirst().findAll {
            it.name() == "language" && it["@xlink.href"]?.text().trim()
        }.each {
            println(it["@xlink.href"]?.text())
        }

@xlink.href кажется всегда пустым, но он есть. При отладке кода я заметил, что его атрибут на языке узла имеет значение:

"{http://www.w3.org/1999/xlink}href" -> "http://prestashop.cartisy.com/api/languages/1"

Каков правильный способ доступа к атрибуту xlink.href на языке узла?

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <product>
        <id><![CDATA[1]]></id>
        <id_manufacturer xlink:href="http://prestashop.test.com/api/manufacturers/1"><![CDATA[1]]></id_manufacturer>
        <id_supplier xlink:href="http://prestashop.test.com/api/suppliers/1"><![CDATA[1]]></id_supplier>
        <id_category_default xlink:href="http://prestashop.test.com/api/categories/5"><![CDATA[5]]></id_category_default>
        <new />
        <cache_default_attribute><![CDATA[1]]></cache_default_attribute>
        <id_default_image xlink:href="http://prestashop.test.com/api/images/products/1/1" not_filterable="true"><![CDATA[1]]></id_default_image>
        <id_default_combination xlink:href="http://prestashop.test.com/api/combinations/1" not_filterable="true"><![CDATA[1]]></id_default_combination>
        <id_tax_rules_group xlink:href="http://prestashop.test.com/api/tax_rule_groups/1"><![CDATA[1]]></id_tax_rules_group>
        <position_in_category not_filterable="true"><![CDATA[0]]></position_in_category>
        <manufacturer_name not_filterable="true"><![CDATA[Fashion Manufacturer]]></manufacturer_name>
        <quantity not_filterable="true"><![CDATA[0]]></quantity>
        <type not_filterable="true"><![CDATA[simple]]></type>
        <id_shop_default><![CDATA[1]]></id_shop_default>
        <reference><![CDATA[demo_1]]></reference>
        <supplier_reference />
        <location />
        <width><![CDATA[0.000000]]></width>
        <height><![CDATA[0.000000]]></height>
        <depth><![CDATA[0.000000]]></depth>
        <weight><![CDATA[0.000000]]></weight>
        <quantity_discount><![CDATA[0]]></quantity_discount>
        <ean13><![CDATA[0]]></ean13>
        <upc />
        <cache_is_pack><![CDATA[0]]></cache_is_pack>
        <cache_has_attachments><![CDATA[0]]></cache_has_attachments>
        <is_virtual><![CDATA[0]]></is_virtual>
        <on_sale><![CDATA[0]]></on_sale>
        <online_only><![CDATA[0]]></online_only>
        <ecotax><![CDATA[0.000000]]></ecotax>
        <minimal_quantity><![CDATA[1]]></minimal_quantity>
        <price><![CDATA[19.980000]]></price>
        <wholesale_price><![CDATA[4.950000]]></wholesale_price>
        <unity />
        <unit_price_ratio><![CDATA[0.000000]]></unit_price_ratio>
        <additional_shipping_cost><![CDATA[0.00]]></additional_shipping_cost>
        <customizable><![CDATA[0]]></customizable>
        <text_fields><![CDATA[0]]></text_fields>
        <uploadable_files><![CDATA[0]]></uploadable_files>
        <active><![CDATA[1]]></active>
        <redirect_type><![CDATA[404]]></redirect_type>
        <id_product_redirected><![CDATA[0]]></id_product_redirected>
        <available_for_order><![CDATA[1]]></available_for_order>
        <available_date><![CDATA[0000-00-00]]></available_date>
        <condition><![CDATA[new]]></condition>
        <show_price><![CDATA[1]]></show_price>
        <indexed><![CDATA[1]]></indexed>
        <visibility><![CDATA[both]]></visibility>
        <advanced_stock_management><![CDATA[0]]></advanced_stock_management>
        <date_add><![CDATA[2014-10-29 21:55:10]]></date_add>
        <date_upd><![CDATA[2014-10-29 21:55:10]]></date_upd>
        <meta_description>
            <language id="1" xlink:href="http://prestashop.test.com/api/languages/1" />
            <language id="2" xlink:href="http://prestashop.test.com/api/languages/2" />
            <language id="3" xlink:href="http://prestashop.test.com/api/languages/3" />
        </meta_description>
        <meta_keywords>
            <language id="1" xlink:href="http://prestashop.test.com/api/languages/1" />
            <language id="2" xlink:href="http://prestashop.test.com/api/languages/2" />
            <language id="3" xlink:href="http://prestashop.test.com/api/languages/3" />
        </meta_keywords>
        <meta_title>
            <language id="1" xlink:href="http://prestashop.test.com/api/languages/1" />
            <language id="2" xlink:href="http://prestashop.test.com/api/languages/2" />
            <language id="3" xlink:href="http://prestashop.test.com/api/languages/3" />
        </meta_title>
        <link_rewrite>
            <language id="1" xlink:href="http://prestashop.test.com/api/languages/1"><![CDATA[faded-short-sleeve-tshirts]]></language>
            <language id="2" xlink:href="http://prestashop.test.com/api/languages/2"><![CDATA[faded-short-sleeve-tshirts]]></language>
            <language id="3" xlink:href="http://prestashop.test.com/api/languages/3"><![CDATA[faded-short-sleeve-tshirts]]></language>
        </link_rewrite>
        <name>
            <language id="1" xlink:href="http://prestashop.test.com/api/languages/1"><![CDATA[Faded Short Sleeve T-shirts]]></language>
            <language id="2" xlink:href="http://prestashop.test.com/api/languages/2"><![CDATA[Faded Short Sleeve T-shirts]]></language>
            <language id="3" xlink:href="http://prestashop.test.com/api/languages/3"><![CDATA[Faded Short Sleeve T-shirts]]></language>
        </name>
        <description>
            <language id="1" xlink:href="http://prestashop.test.com/api/languages/1"><![CDATA[<p>Fashion has been creating well-designed collections since 2010. The brand offers feminine designs delivering stylish separates and statement dresses which has since evolved into a full ready-to-wear collection in which every item is a vital part of a woman's wardrobe. The result? Cool, easy, chic looks with youthful elegance and unmistakable signature style. All the beautiful pieces are made in Italy and manufactured with the greatest attention. Now Fashion extends to a range of accessories including shoes, hats, belts and more!</p>]]></language>
            <language id="2" xlink:href="http://prestashop.test.com/api/languages/2"><![CDATA[<p>Fashion has been creating well-designed collections since 2010. The brand offers feminine designs delivering stylish separates and statement dresses which has since evolved into a full ready-to-wear collection in which every item is a vital part of a woman's wardrobe. The result? Cool, easy, chic looks with youthful elegance and unmistakable signature style. All the beautiful pieces are made in Italy and manufactured with the greatest attention. Now Fashion extends to a range of accessories including shoes, hats, belts and more!</p>]]></language>
            <language id="3" xlink:href="http://prestashop.test.com/api/languages/3"><![CDATA[<p>Fashion has been creating well-designed collections since 2010. The brand offers feminine designs delivering stylish separates and statement dresses which has since evolved into a full ready-to-wear collection in which every item is a vital part of a woman's wardrobe. The result? Cool, easy, chic looks with youthful elegance and unmistakable signature style. All the beautiful pieces are made in Italy and manufactured with the greatest attention. Now Fashion extends to a range of accessories including shoes, hats, belts and more!</p>]]></language>
        </description>
        <description_short>
            <language id="1" xlink:href="http://prestashop.test.com/api/languages/1"><![CDATA[<p>Faded short sleeve t-shirt with high neckline. Soft and stretchy material for a comfortable fit. Accessorize with a straw hat and you're ready for summer!</p>]]></language>
            <language id="2" xlink:href="http://prestashop.test.com/api/languages/2"><![CDATA[<p>Faded short sleeve t-shirt with high neckline. Soft and stretchy material for a comfortable fit. Accessorize with a straw hat and you're ready for summer!</p>]]></language>
            <language id="3" xlink:href="http://prestashop.test.com/api/languages/3"><![CDATA[<p>Faded short sleeve t-shirt with high neckline. Soft and stretchy material for a comfortable fit. Accessorize with a straw hat and you're ready for summer!</p>]]></language>
        </description_short>
        <available_now>
            <language id="1" xlink:href="http://prestashop.test.com/api/languages/1"><![CDATA[In stock]]></language>
            <language id="2" xlink:href="http://prestashop.test.com/api/languages/2"><![CDATA[In stock]]></language>
            <language id="3" xlink:href="http://prestashop.test.com/api/languages/3"><![CDATA[In stock]]></language>
        </available_now>
        <available_later>
            <language id="1" xlink:href="http://prestashop.test.com/api/languages/1" />
            <language id="2" xlink:href="http://prestashop.test.com/api/languages/2" />
            <language id="3" xlink:href="http://prestashop.test.com/api/languages/3" />
        </available_later>
        <associations>
            <categories node_type="categories">
                <categories xlink:href="http://prestashop.test.com/api/categories/2">
                    <id><![CDATA[2]]></id>
                </categories>
                <categories xlink:href="http://prestashop.test.com/api/categories/3">
                    <id><![CDATA[3]]></id>
                </categories>
                <categories xlink:href="http://prestashop.test.com/api/categories/4">
                    <id><![CDATA[4]]></id>
                </categories>
                <categories xlink:href="http://prestashop.test.com/api/categories/5">
                    <id><![CDATA[5]]></id>
                </categories>
            </categories>
            <images node_type="images">
                <images xlink:href="http://prestashop.test.com/api/images/products/1/1">
                    <id><![CDATA[1]]></id>
                </images>
                <images xlink:href="http://prestashop.test.com/api/images/products/1/2">
                    <id><![CDATA[2]]></id>
                </images>
                <images xlink:href="http://prestashop.test.com/api/images/products/1/3">
                    <id><![CDATA[3]]></id>
                </images>
                <images xlink:href="http://prestashop.test.com/api/images/products/1/4">
                    <id><![CDATA[4]]></id>
                </images>
            </images>
            <combinations node_type="combinations">
                <combinations xlink:href="http://prestashop.test.com/api/combinations/1">
                    <id><![CDATA[1]]></id>
                </combinations>
                <combinations xlink:href="http://prestashop.test.com/api/combinations/2">
                    <id><![CDATA[2]]></id>
                </combinations>
                <combinations xlink:href="http://prestashop.test.com/api/combinations/3">
                    <id><![CDATA[3]]></id>
                </combinations>
                <combinations xlink:href="http://prestashop.test.com/api/combinations/4">
                    <id><![CDATA[4]]></id>
                </combinations>
                <combinations xlink:href="http://prestashop.test.com/api/combinations/5">
                    <id><![CDATA[5]]></id>
                </combinations>
                <combinations xlink:href="http://prestashop.test.com/api/combinations/6">
                    <id><![CDATA[6]]></id>
                </combinations>
            </combinations>
            <product_option_values node_type="product_option_values">
                <product_option_values xlink:href="http://prestashop.test.com/api/product_option_values/1">
                    <id><![CDATA[1]]></id>
                </product_option_values>
                <product_option_values xlink:href="http://prestashop.test.com/api/product_option_values/13">
                    <id><![CDATA[13]]></id>
                </product_option_values>
                <product_option_values xlink:href="http://prestashop.test.com/api/product_option_values/14">
                    <id><![CDATA[14]]></id>
                </product_option_values>
                <product_option_values xlink:href="http://prestashop.test.com/api/product_option_values/2">
                    <id><![CDATA[2]]></id>
                </product_option_values>
                <product_option_values xlink:href="http://prestashop.test.com/api/product_option_values/3">
                    <id><![CDATA[3]]></id>
                </product_option_values>
            </product_option_values>
            <product_features node_type="product_features">
                <product_features xlink:href="http://prestashop.test.com/api/product_features/5">
                    <id><![CDATA[5]]></id>
                    <custom><![CDATA[0]]></custom>
                    <id_feature_value xlink:href="http://prestashop.test.com/api/product_feature_values/5"><![CDATA[5]]></id_feature_value>
                </product_features>
                <product_features xlink:href="http://prestashop.test.com/api/product_features/6">
                    <id><![CDATA[6]]></id>
                    <custom><![CDATA[0]]></custom>
                    <id_feature_value xlink:href="http://prestashop.test.com/api/product_feature_values/11"><![CDATA[11]]></id_feature_value>
                </product_features>
                <product_features xlink:href="http://prestashop.test.com/api/product_features/7">
                    <id><![CDATA[7]]></id>
                    <custom><![CDATA[0]]></custom>
                    <id_feature_value xlink:href="http://prestashop.test.com/api/product_feature_values/17"><![CDATA[17]]></id_feature_value>
                </product_features>
            </product_features>
            <tags node_type="tags" />
            <stock_availables node_type="stock_availables">
                <stock_availables xlink:href="http://prestashop.test.com/api/stock_availables/1">
                    <id><![CDATA[1]]></id>
                    <id_product_attribute><![CDATA[0]]></id_product_attribute>
                </stock_availables>
                <stock_availables xlink:href="http://prestashop.test.com/api/stock_availables/8">
                    <id><![CDATA[8]]></id>
                    <id_product_attribute><![CDATA[1]]></id_product_attribute>
                </stock_availables>
                <stock_availables xlink:href="http://prestashop.test.com/api/stock_availables/9">
                    <id><![CDATA[9]]></id>
                    <id_product_attribute><![CDATA[2]]></id_product_attribute>
                </stock_availables>
                <stock_availables xlink:href="http://prestashop.test.com/api/stock_availables/10">
                    <id><![CDATA[10]]></id>
                    <id_product_attribute><![CDATA[3]]></id_product_attribute>
                </stock_availables>
                <stock_availables xlink:href="http://prestashop.test.com/api/stock_availables/11">
                    <id><![CDATA[11]]></id>
                    <id_product_attribute><![CDATA[4]]></id_product_attribute>
                </stock_availables>
                <stock_availables xlink:href="http://prestashop.test.com/api/stock_availables/12">
                    <id><![CDATA[12]]></id>
                    <id_product_attribute><![CDATA[5]]></id_product_attribute>
                </stock_availables>
                <stock_availables xlink:href="http://prestashop.test.com/api/stock_availables/13">
                    <id><![CDATA[13]]></id>
                    <id_product_attribute><![CDATA[6]]></id_product_attribute>
                </stock_availables>
            </stock_availables>
            <accessories node_type="products" />
            <product_bundle node_type="products" />
        </associations>
    </product>
</prestashop>

person ddelizia    schedule 10.02.2015    source источник


Ответы (1)


Это @ns:attr и не @ns.attr:

def node = new groovy.util.XmlSlurper().parseText('''\
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<meta_description>
<language id="1" xlink:href="http://prestashop.test.com/api/languages/1" />
<language id="2" xlink:href="http://prestashop.test.com/api/languages/2" />
<language id="3" xlink:href="http://prestashop.test.com/api/languages/3" />
</meta_description>
</prestashop>
''').declareNamespace(
    xlink:"http://www.w3.org/1999/xlink")

node.depthFirst().findAll {
    it.name() == "language" && it["@xlink:href"]?.text()?.trim()
    //                                  ~~~
}.each {
    println(it["@xlink:href"]?.text())
    //               ~~~
}
//=> http://prestashop.test.com/api/languages/1
//=> http://prestashop.test.com/api/languages/2
//=> http://prestashop.test.com/api/languages/3
person cfrick    schedule 10.02.2015