⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpath.pm

📁 1. 记录每个帖子的访问人情况
💻 PM
📖 第 1 页 / 共 2 页
字号:
}sub set_filename {    my $self = shift;    $self->{_filename} = shift;}sub get_parser {    my $self = shift;    $self->{_parser};}sub set_parser {    my $self = shift;    $self->{_parser} = shift;}sub get_xml {    my $self = shift;    $self->{_xml};}sub set_xml {    my $self = shift;    $self->{_xml} = shift;}sub get_ioref {    my $self = shift;    $self->{_ioref};}sub set_ioref {    my $self = shift;    $self->{_ioref} = shift;}sub get_context {    my $self = shift;    $self->{_context};}sub set_context {    my $self = shift;    $self->{_context} = shift;}sub cleanup {    my $self = shift;    if ($XML::XPath::SafeMode) {        my $context = $self->get_context;        return unless $context;        $context->dispose;    }}sub set_namespace {    my $self = shift;    my ($prefix, $expanded) = @_;    $self->{path_parser}->set_namespace($prefix, $expanded);}sub clear_namespaces {    my $self = shift;    $self->{path_parser}->clear_namespaces();}1;__END__=head1 NAMEXML::XPath - a set of modules for parsing and evaluating XPath statements=head1 DESCRIPTIONThis module aims to comply exactly to the XPath specification athttp://www.w3.org/TR/xpath and yet allow extensions to be added in theform of functions. Modules such as XSLT and XPointer may need to dothis as they support functionality beyond XPath.=head1 SYNOPSIS    use XML::XPath;    use XML::XPath::XMLParser;        my $xp = XML::XPath->new(filename => 'test.xhtml');        my $nodeset = $xp->find('/html/body/p'); # find all paragraphs        foreach my $node ($nodeset->get_nodelist) {        print "FOUND\n\n",             XML::XPath::XMLParser::as_string($node),            "\n\n";    }=head1 DETAILSThere's an awful lot to all of this, so bear with it - if you stick itout it should be worth it. Please get a good understanding of XPathby reading the spec before asking me questions. All of the classesand parts herein are named to be synonimous with the names in thespecification, so consult that if you don't understand why I'm doingsomething in the code.=head1 APIThe API of XML::XPath itself is extremely simple to allow you to getgoing almost immediately. The deeper API's are more complex, but youshouldn't have to touch most of that.=head2 new()This constructor follows the often seen named parameter method call.Parameters you can use are: filename, parser, xml, ioref and context.The filename parameter specifies an XML file to parse. The xmlparameter specifies a string to parse, and the ioref parameterspecifies an ioref to parse. The context option allows you to specify a context node. The context node has to be in the format of a node as specified in L<XML::XPath::XMLParser>. The 4 parametersfilename, xml, ioref and context are mutually exclusive - you shouldonly specify one (if you specify anything other than context, thecontext node is the root of your document).The parser option allows you to pass in an already prepared XML::Parser object, to save you having to create more than onein your application (if, for example, you're doing more than just XPath).    my $xp = XML::XPath->new( context => $node );It is very much recommended that you use only 1 XPath object throughout the life of your application. This is because the object (and it's sub-objects)maintain certain bits of state information that will be useful (suchas XPath variables) to later calls to find(). It's also a good idea becauseyou'll use less memory this way.=head2 I<nodeset> = find($path, [$context])The find function takes an XPath expression (a string) and returns either anXML::XPath::NodeSet object containing the nodes it found (or empty ifno nodes matched the path), or one of XML::XPath::Literal (a string),XML::XPath::Number, or XML::XPath::Boolean. It should always return something - and you can use ->isa() to find out what it returned. If youneed to check how many nodes it found you should check $nodeset->size.See L<XML::XPath::NodeSet>. An optional second parameter of a contextnode allows you to use this method repeatedly, for example XSLT needsto do this.=head2 findnodes($path, [$context])Returns a list of nodes found by $path, optionally in context $context. In scalar context returns an XML::XPath::NodeSet object.=head2 findnodes_as_string($path, [$context])Returns the nodes found reproduced as XML. The result is not guaranteedto be valid XML though.=head2 findvalue($path, [$context])Returns either a C<XML::XPath::Literal>, a C<XML::XPath::Boolean> or aC<XML::XPath::Number> object. If the path returns a NodeSet,$nodeset->to_literal is called automatically for you (and thus aC<XML::XPath::Literal> is returned). Note thatfor each of the objects stringification is overloaded, so you can justprint the value found, or manipulate it in the ways you would a normalperl value (e.g. using regular expressions).=head2 exists($path, [$context])Returns true if the given path exists.=head2 matches($node, $path, [$context])Returns true if the node matches the path (optionally in context $context).=head2 getNodeText($path)Returns the text string for a particular XML node.  Returns a string,or undef if the node doesn't exist.=head2 setNodeText($path, $text)Sets the text string for a particular XML node.  The node can be anelement or an attribute.  If the node to be set is an attribute, andthe attribute node does not exist, it will be created automatically.=head2 createNode($path)Creates the node matching the path given.  If part of the path given, orall of the path do not exist, the necessary nodes will be createdautomatically.=head2 set_namespace($prefix, $uri)Sets the namespace prefix mapping to the uri.Normally in XML::XPath the prefixes in XPath node tests take theircontext from the current node. This means that foo:bar will alwaysmatch an element <foo:bar> regardless of the namespace that the prefixfoo is mapped to (which might even change within the document, resultingin unexpected results). In order to make prefixes in XPath node testsactually map to a real URI, you need to enable that via a callto the set_namespace method of your XML::XPath object.=head2 clear_namespaces()Clears all previously set namespace mappings.=head2 $XML::XPath::NamespacesSet this to 0 if you I<don't> want namespace processing to occur. Thiswill make everything a little (tiny) bit faster, but you'll suffer for it,probably.=head1 Node Object ModelSee L<XML::XPath::Node>, L<XML::XPath::Node::Element>, L<XML::XPath::Node::Text>, L<XML::XPath::Node::Comment>,L<XML::XPath::Node::Attribute>, L<XML::XPath::Node::Namespace>,and L<XML::XPath::Node::PI>.=head1 On Garbage CollectionXPath nodes work in a special way that allows circular references, and yet still lets Perl's reference counting garbage collector to clean upthe nodes after use. This should be totally transparent to the user,with one caveat: B<If you free your tree before letting go of a sub-tree,consider that playing with fire and you may get burned>. What does thismean to the average user? Not much. Provided you don't free (or let goout of scope) either the tree you passed to XML::XPath->new, or if youdidn't pass a tree, and passed a filename or IO-ref, then provided youdon't let the XML::XPath object go out of scope before you let resultsof find() and its friends go out of scope, then you'll be fine. Even ifyou B<do> let the tree go out of scope before results, you'll probablystill be fine. The only case where you may get stung is when the lastpart of your path/query is either an ancestor or parent axis. In thatcase the worst that will happen is you'll end up with a circular referencethat won't get cleared until interpreter destruction time. You can getaround that by explicitly calling $node->DESTROY on each of your resultnodes, if you really need to do that.Mail me direct if that's not clear. Note that it's not doom and gloom. It'sby no means perfect, but the worst that will happen is a long running processcould leak memory. Most long running processes will therefore be able toexplicitly be careful not to free the tree (or XML::XPath object) beforefreeing results. AxKit, an application that uses XML::XPath, does this andI didn't have to make any changes to the code - it's already sensibleprogramming.If you I<really> don't want all this to happen, then set the variable$XML::XPath::SafeMode, and call $xp->cleanup() on the XML::XPath objectwhen you're finished, or $tree->dispose() if you have a tree instead.=head1 ExamplePlease see the test files in t/ for examples on how to use XPath.=head1 Support/AuthorThis module is copyright 2000 AxKit.com Ltd. This is freesoftware, and as such comes with NO WARRANTY. No dates are used in thismodule. You may distribute this module under the terms of either theGnu GPL,  or the Artistic License (the same terms as Perl itself).For support, please subscribe to the Perl-XML mailing list at the URL http://listserv.activestate.com/mailman/listinfo/perl-xmlMatt Sergeant, matt@sergeant.org=head1 SEE ALSOL<XML::XPath::Literal>, L<XML::XPath::Boolean>, L<XML::XPath::Number>,L<XML::XPath::XMLParser>, L<XML::XPath::NodeSet>, L<XML::XPath::PerlSAX>,L<XML::XPath::Builder>.=cut

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -