Maxim has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I am trying to remove elements using XML::LibXML. But there is an attribute which blocks me. I don't how to do it.
use XML::LibXML; use warnings; use strict; my $file = (<DATA>); my $value = 'name'; my $d = XML::LibXML->new->parse_file($file); my $attr = XML::LibXML::Attr->new($d [,$value]); for my $dead ($attr->findnodes(q{/category/subcategory[name = "Prestig +e"]})) { $dead->unbindNode; } __DATA__ <category name="Coffee"> <heading>150 years of know how at the service of a same strategy: Ta +ste</heading> <photo>images/bag_legal.jpg</photo> <subcategory name="Prestige"> <photo>images/prestige.jpg</photo> <product>Coffee <brand>Legal</brand> <channel>D</channel> <channel>A</channel> <conditionning unit="gr">250</conditionning> <description>Legal Prestige</description> <packaging_qty>12</packaging_qty> <product_id>800.55.01</product_id> <recipe>/recipes/coffee1.html</recipe> </product> </subcategory> <subcategory name="Grand Arabica"> <comments> <paragraph>Experience the subtle voluptuousness of extraordinary + Arabica coffee in your cup. Grand Arabica: incomparable pleasure in +coffee.</paragraph> </comments> <photo>images/arabica.jpg</photo> <product>Coffee <brand>Legal</brand> <channel>A</channel> <channel>D</channel> <channel>S</channel> <conditionning unit="gr">250</conditionning> <description>Grand Arabica</description> <packaging_qty>12</packaging_qty> <product_id>802.55.01</product_id> </product> </subcategory> </category>

I want to delete the subcategory "Prestige".
Thanks.
Maxim

Replies are listed 'Best First'.
Re: How to remove elements with XML::LibXML
by Corion (Patriarch) on Oct 10, 2004 at 09:27 UTC

    Are you sure that your XPath expression is matching? Is there any error message you are getting? I believe your XPath expression should be more like

    /category/subcategory[@name = "Prestige"]

    to find the correct subcategory. Try the ->as_string method to debug your queries.

      Hi corion,
      You were right about the XPath expression. It works well now, I just try now. Thanks so much.
      But the error got it are:

      Multidimensional syntax $d ,$value not supported at C:\Program Files\Apache G oup\Apache2\cgi-bin\legas\delsub.pl line 12.
      Global symbol "@d" requires explicit package name at C:\Program Files\Apache Gr up\Apache2\cgi-bin\legas\delsub.pl line 12.
      syntax error at C:\Program Files\Apache Group\Apache2\cgi-bin\legas\delsub.pl l ne 12, near "[,"
      Execution of C:\Program Files\Apache Group\Apache2\cgi-bin\legas\delsub.pl abor ed due to compilation errors.

      Anyway, Thank you so much. I reall appreciate.
      Maxim

        Please, Maxim, read the documentation, and read about Perl syntax. You this syntax error, because you copied stuff out of the documentation without reading it and realizing that the $d, [$value] part is not meant to be copied verbatim. The square brackets around $value are used to describe that it is an optional parameter.

        I recommend that you read a book about Perl and also read the documentation for the functions you are using before piecing together a script by copying from the documentation.