{
    "mode": "perldoc",
    "parameter": "XML::XPath",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/XML%3A%3AXPath/json",
    "generated": "2026-05-30T06:06:19Z",
    "synopsis": "use XML::XPath;\nuse XML::XPath::XMLParser;\nmy $xp = XML::XPath->new(filename => 'test.xhtml');\nmy $nodeset = $xp->find('/html/body/p'); # find all paragraphs\nforeach my $node ($nodeset->getnodelist) {\nprint \"FOUND\\n\\n\",\nXML::XPath::XMLParser::asstring($node),\n\"\\n\\n\";\n}",
    "sections": {
        "NAME": {
            "content": "XML::XPath - Parse and evaluate XPath statements.\n",
            "subsections": []
        },
        "VERSION": {
            "content": "Version 1.44\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module aims to comply exactly to the XPath specification at\nhttp://www.w3.org/TR/xpath and yet allow extensions to be added in the\nform of functions.Modules such as XSLT and XPointer may need to do this\nas they support functionality beyond XPath.\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use XML::XPath;\nuse XML::XPath::XMLParser;\n\nmy $xp = XML::XPath->new(filename => 'test.xhtml');\n\nmy $nodeset = $xp->find('/html/body/p'); # find all paragraphs\n\nforeach my $node ($nodeset->getnodelist) {\nprint \"FOUND\\n\\n\",\nXML::XPath::XMLParser::asstring($node),\n\"\\n\\n\";\n}\n",
            "subsections": []
        },
        "DETAILS": {
            "content": "There is an awful lot to all of this, so bear with it - if you stick it\nout it should be worth it. Please get a good understanding of XPath by\nreading the spec before asking me questions. All of the classes and\nparts herein are named to be synonymous with the names in the\nspecification, so consult that if you don't understand why I'm doing\nsomething in the code.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "The API of XML::XPath itself is extremely simple to allow you to get\ngoing almost immediately. The deeper API's are more complex, but you\nshouldn't have to touch most of that.\n\nnew()\nThis constructor follows the often seen named parameter method call.\nParameters you can use are: filename, parser, xml, ioref and context.\nThe filename parameter specifies an XML file to parse. The xml parameter\nspecifies a string to parse, and the ioref parameter specifies an ioref\nto parse. The context option allows you to specify a context node. The\ncontext node has to be in the format of a node as specified in\nXML::XPath::XMLParser. The 4 parameters filename, xml, ioref and context\nare mutually exclusive - you should only specify one (if you specify\nanything other than context, the context node is the root of your\ndocument). The parser option allows you to pass in an already prepared\nXML::Parser object, to save you having to create more than one in your\napplication (if, for example, you are doing more than just XPath).\n\nmy $xp = XML::XPath->new( context => $node );\n\nIt is very much recommended that you use only 1 XPath object throughout\nthe life of your application. This is because the object (and it's\nsub-objects) maintain certain bits of state information that will be\nuseful (such as XPath variables) to later calls to find(). It's also a\ngood idea because you'll use less memory this way.\n\nfind($path, [$context])\nThe find function takes an XPath expression (a string) and returns\neither an XML::XPath::NodeSet object containing the nodes it found (or\nempty if no nodes matched the path), or one of XML::XPath::Literal (a\nstring), XML::XPath::Number or XML::XPath::Boolean. It should always\nreturn something - and you can use ->isa() to find out what it returned.\nIf you need to check how many nodes it found you should check\n$nodeset->size. See XML::XPath::NodeSet. An optional second parameter of\na context node allows you to use this method repeatedly, for example\nXSLT needs to do this.\n\nfindnodes($path, [$context])\nReturns a list of nodes found by $path, optionally in context $context.\nIn scalar context returns an XML::XPath::NodeSet object.\n\nmatches($node, $path, [$context])\nReturns true if the node matches the path (optionally in context\n$context).\n\nfindnodesasstring($path, [$context])\nReturns the nodes found reproduced as XML.The result isn't guaranteed to\nbe valid XML though.\n\nfindvalue($path, [$context])\nReturns either a \"XML::XPath::Literal\", a \"XML::XPath::Boolean\" or a\n\"XML::XPath::Number\" object.If the path returns a\nNodeSet,$nodeset->toliteral is called automatically for you (and thus a\n\"XML::XPath::Literal\" is returned).Note that for each of the objects\nstringification is overloaded, so you can just print the value found, or\nmanipulate it in the ways you would a normal perl value (e.g. using\nregular expressions).\n\nexists($path, [$context])\nReturns true if the given path exists.\n\ngetNodeText($path)\nReturns the XML::XPath::Literal for a particular XML node. Returns a\nstring if exists or '' (empty string) if the node doesn't exist.\n\nsetNodeText($path, $text)\nSets the text string for a particular XML node. The node can be an\nelement or an attribute. If the node to be set is an attribute, and the\nattribute node does not exist, it will be created automatically.\n\ncreateNode($path)\nCreates the node matching the $path given. If part of the path given or\nall of the path do not exist, the necessary nodes will be created\nautomatically.\n\nsetnamespace($prefix, $uri)\nSets the namespace prefix mapping to the uri.\n\nNormally in \"XML::XPath\" the prefixes in XPath node test take their\ncontext from the current node. This means that foo:bar will always match\nan element <foo:bar> regardless of the namespace that the prefix foo is\nmapped to (which might even change within the document, resulting in\nunexpected results). In order to make prefixes in XPath node tests\nactually map to a real URI, you need to enable that via a call to the\nsetnamespace method of your \"XML::XPath\" object.\n\nclearnamespaces()\nClears all previously set namespace mappings.\n\n$XML::XPath::Namespaces\nSet this to 0 if you *don't* want namespace processing to occur. This\nwill make everything a little (tiny) bit faster, but you'll suffer for\nit, probably.\n",
            "subsections": []
        },
        "Node Object Model": {
            "content": "See XML::XPath::Node, XML::XPath::Node::Element, XML::XPath::Node::Text,\nXML::XPath::Node::Comment, XML::XPath::Node::Attribute,\nXML::XPath::Node::Namespace, and XML::XPath::Node::PI.\n",
            "subsections": []
        },
        "On Garbage Collection": {
            "content": "XPath nodes work in a special way that allows circular references, and\nyet still lets Perl's reference counting garbage collector to clean up\nthe nodes after use. This should be totally transparent to the user,\nwith one caveat: If you free your tree before letting go of a\nsub-tree,consider that playing with fire and you may get burned. What\ndoes this mean to the average user? Not much. Provided you don't free\n(or let go out of scope) either the tree you passed to XML::XPath->new,\nor if you didn't pass a tree, and passed a filename or IO-ref, then\nprovided you don't let the XML::XPath object go out of scope before you\nlet results of find() and its friends go out of scope, then you'll be\nfine. Even if you do let the tree go out of scope before results, you'll\nprobably still be fine. The only case where you may get stung is when\nthe last part of your path/query is either an ancestor or parent axis.\nIn that case the worst that will happen is you'll end up with a circular\nreference that won't get cleared until interpreter destruction time.You\ncan get around that by explicitly calling $node->DESTROY on each of your\nresult nodes, if you really need to do that.\n\nMail me direct if that's not clear. Note that it's not doom and gloom.\nIt's by no means perfect,but the worst that will happen is a long\nrunning process could leak memory. Most long running processes will\ntherefore be able to explicitly be careful not to free the tree (or\nXML::XPath object) before freeing results.AxKit, an application that\nuses XML::XPath, does this and I didn't have to make any changes to the\ncode - it's already sensible programming.\n\nIf you *really* don't want all this to happen, then set the variable\n$XML::XPath::SafeMode, and call $xp->cleanup() on the XML::XPath object\nwhen you're finished, or $tree->dispose() if you have a tree instead.\n",
            "subsections": []
        },
        "Example": {
            "content": "Please see the test files in t/ for examples on how to use XPath.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Original author Matt Sergeant, \"<matt at sergeant.org>\"\n\nCurrently maintained by Mohammad S Anwar, \"<mohammad.anwar at\nyahoo.com>\"\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "XML::XPath::Literal, XML::XPath::Boolean, XML::XPath::Number,\nXML::XPath::XMLParser, XML::XPath::NodeSet, XML::XPath::PerlSAX,\nXML::XPath::Builder.\n",
            "subsections": []
        },
        "LICENSE AND COPYRIGHT": {
            "content": "This module is copyright 2000 AxKit.com Ltd. This is free software, and\nas such comes with NO WARRANTY. No dates are used in this module. You\nmay distribute this module under the terms of either the Gnu GPL, or the\nArtistic License (the same terms as Perl itself).\n\nFor support, please subscribe to the Perl-XML\n<http://listserv.activestate.com/mailman/listinfo/perl-xml> mailing list\nat the URL\n",
            "subsections": []
        }
    },
    "summary": "XML::XPath - Parse and evaluate XPath statements.",
    "flags": [],
    "examples": [],
    "see_also": []
}