XML::Chain::Selector - selector for traversing the XML::Chain
my $user = xc('user', xmlns => 'http://testns')->auto_indent({chars=>' 'x4}) ->a('name', '-' => 'Johnny Thinker') ->a('username', '-' => 'jt') ->c('bio') ->c('div', xmlns => 'http://www.w3.org/1999/xhtml') ->a('h1', '-' => 'about') ->a('p', '-' => '...') ->up ->a('greeting', '-' => 'Hey') ->up ->a('active', '-' => '1') ->root; say $user->as_string;
Will print:
<user xmlns="http://testns"> <name>Johnny Thinker</name> <username>jt</username> <bio> <div xmlns="http://www.w3.org/1999/xhtml"> <h1>about</h1> <p>...</p> </div> <greeting>Hey</greeting> </bio> <active>1</active> </user>
Appends new element to current elements and changes context to them. New element is defined in parameters:
$xc->c('i', class => 'icon-download icon-white') # <i class="icon-download icon-white"/>
First parameter is name of the element, then followed by optional element attributes.
Appends text to current elements.
xc('span')->t('some')->t(' ')->t('more text') # <span>some more text</span>
Sets document element as current element.
say xc('p') ->t('this ') ->a(xc('b')->t('is')) ->t(' important!') ->root->as_string; # <p>this <b>is</b> important!</p>
Traverse current elements and replace them by their parents.
say $xc->find('//p/b[@class="less"]')->text_content; say $xc->find('//xhtml:div', xhtml => 'http://www.w3.org/1999/xhtml')->count;
Look-up elements by xpath and set them as current elements. Optional look-up namespace prefixes can be specified. Any global registered namespace prefixes "reg_global_ns" can be used.
Set all current elements child nodes as current elements.
Set first current elements as current elements.
Removes all child nodes from current elements.
my $body = xc('bodyz')->rename('body'); # <body/>
Rename node name(s).
my $img = xc('img')->attr('href' => '#', 'title' => 'imaget'); # <img href="#" title="imaget"/> say $img->attr('title') # imaget say $img->attr('title' => undef) # <img href="#"/>
Get or set elements attributes. With one argument returns attribute value otherwise sets them. Setting attribute to undef will remove it from the element.
undef
# rename using each $body->rename('body'); $body ->a(xc('p.1')->t(1)) ->a(xc('p.2')->t(2)) ->a(xc('div')->t(3)) ->a(xc('p.3')->t(4)) ->each(sub { $_->rename('p') if $_->name =~ m/^p[.]/ }); is($body, '<body><p>1</p><p>2</p><div>3</div><p>4</p></body>','rename using each()');
Loops through all selected elements and calls callback for each of them.
xc('body')->a('p', i => 1)->children->remap( sub { (map {xc('e', i => $_)} 1 .. 3), $_; } )->root; # <body><e i="1"/><e i="2"/><e i="3"/><p i="1"/></body>
Replaces all selected elements by callback returned elements.
my $pdiv = xc('base') ->a(xc('p')->t(1)) ->a(xc('p')->t(2)) ->a(xc('div')->t(3)) ->a(xc('p')->t(4)); my $p = $pdiv->find('//p'); # $pdiv->find('//p[position()=3]')->rm->name eq 'base' # $p->count == 2 # deleted elements are skipped also in old selectors # <base><p>1</p><p>2</p><div>3</div></base>
Deletes current elements and returns their parent.
(experimental feature (good/usefull for debug, needs more testing), works only on element for which as_string is called at this moment)
my $simple = xc('div') ->auto_indent(1) ->a('div', '-' => 'in1') ->a('div', '-' => 'in2') ->t('in2.1') ->a('div', '-' => 'in3') ; say $simple->as_string;
<div> <div>in1</div> <div>in2</div> in2.1 <div>in3</div> </div>
Turn on/off tidy/auto-indentation of document elements. Default indentation characters are tabs.
Argument can be either true/false scalar or a hashref with indentation options. Currently {chars=' 'x4} > will set indentation characters to be four spaces.
{chars=
See "CHAINED DOCUMENT METHODS" in XML::Chain.
Returns string representation of current XML elements. Call root before to get a string representing the whole document.
$xc->as_string $xc->root->as_string
Returns array of current elements as XML::LibXML objects.
Returns text content of all current XML elements.
say $xc->find('//b')->count;
Return the number of current elements.
my $lxml_el = $xc->find('//b')->first->as_xml_libxml;
Checks is there is exactly one element in current elements and return it as XML::Chain::Element object.
$sitemap->reg_global_ns('i' => 'http://www.google.com/schemas/sitemap-image/1.1'); $sitemap->reg_global_ns('s' => 'http://www.sitemaps.org/schemas/sitemap/0.9'); say $sitemap->find('/s:urlset/s:url/i:image')->count # 2
Jozef Kutej
Copyright 2017 Jozef Kutej, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install XML::Chain, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::Chain
CPAN shell
perl -MCPAN -e shell install XML::Chain
For more information on module installation, please visit the detailed CPAN module installation guide.