{
    "mode": "perldoc",
    "parameter": "XML::LibXML::XPathContext",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/XML%3A%3ALibXML%3A%3AXPathContext/json",
    "generated": "2026-06-10T23:08:42Z",
    "synopsis": "my $xpc = XML::LibXML::XPathContext->new();\nmy $xpc = XML::LibXML::XPathContext->new($node);\n$xpc->registerNs($prefix, $namespaceuri)\n$xpc->unregisterNs($prefix)\n$uri = $xpc->lookupNs($prefix)\n$xpc->registerVarLookupFunc($callback, $data)\n$data = $xpc->getVarLookupData();\n$callback = $xpc->getVarLookupFunc();\n$xpc->unregisterVarLookupFunc($name);\n$xpc->registerFunctionNS($name, $uri, $callback)\n$xpc->unregisterFunctionNS($name, $uri)\n$xpc->registerFunction($name, $callback)\n$xpc->unregisterFunction($name)\n@nodes = $xpc->findnodes($xpath)\n@nodes = $xpc->findnodes($xpath, $contextnode )\n$nodelist = $xpc->findnodes($xpath, $contextnode )\n$object = $xpc->find($xpath )\n$object = $xpc->find($xpath, $contextnode )\n$value = $xpc->findvalue($xpath )\n$value = $xpc->findvalue($xpath, $contextnode )\n$bool = $xpc->exists( $xpathexpression, $contextnode );\n$xpc->setContextNode($node)\nmy $node = $xpc->getContextNode;\n$xpc->setContextPosition($position)\nmy $position = $xpc->getContextPosition;\n$xpc->setContextSize($size)\nmy $size = $xpc->getContextSize;\n$xpc->setContextNode($node)",
    "sections": {
        "NAME": {
            "content": "XML::LibXML::XPathContext - XPath Evaluation\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "my $xpc = XML::LibXML::XPathContext->new();\nmy $xpc = XML::LibXML::XPathContext->new($node);\n$xpc->registerNs($prefix, $namespaceuri)\n$xpc->unregisterNs($prefix)\n$uri = $xpc->lookupNs($prefix)\n$xpc->registerVarLookupFunc($callback, $data)\n$data = $xpc->getVarLookupData();\n$callback = $xpc->getVarLookupFunc();\n$xpc->unregisterVarLookupFunc($name);\n$xpc->registerFunctionNS($name, $uri, $callback)\n$xpc->unregisterFunctionNS($name, $uri)\n$xpc->registerFunction($name, $callback)\n$xpc->unregisterFunction($name)\n@nodes = $xpc->findnodes($xpath)\n@nodes = $xpc->findnodes($xpath, $contextnode )\n$nodelist = $xpc->findnodes($xpath, $contextnode )\n$object = $xpc->find($xpath )\n$object = $xpc->find($xpath, $contextnode )\n$value = $xpc->findvalue($xpath )\n$value = $xpc->findvalue($xpath, $contextnode )\n$bool = $xpc->exists( $xpathexpression, $contextnode );\n$xpc->setContextNode($node)\nmy $node = $xpc->getContextNode;\n$xpc->setContextPosition($position)\nmy $position = $xpc->getContextPosition;\n$xpc->setContextSize($size)\nmy $size = $xpc->getContextSize;\n$xpc->setContextNode($node)\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The XML::LibXML::XPathContext class provides an almost complete interface to libxml2's XPath\nimplementation. With XML::LibXML::XPathContext, it is possible to evaluate XPath expressions in\nthe context of arbitrary node, context size, and context position, with a user-defined\nnamespace-prefix mapping, custom XPath functions written in Perl, and even a custom XPath\nvariable resolver.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "",
            "subsections": [
                {
                    "name": "Namespaces",
                    "content": "This example demonstrates \"registerNs()\" method. It finds all paragraph nodes in an XHTML\ndocument.\n\nmy $xc = XML::LibXML::XPathContext->new($xhtmldoc);\n$xc->registerNs('xhtml', 'http://www.w3.org/1999/xhtml');\nmy @nodes = $xc->findnodes('//xhtml:p');\n"
                },
                {
                    "name": "Custom XPath functions",
                    "content": "This example demonstrates \"registerFunction()\" method by defining a function filtering nodes\nbased on a Perl regular expression:\n\nsub grepnodes {\nmy ($nodelist,$regexp) =  @;\nmy $result = XML::LibXML::NodeList->new;\nfor my $node ($nodelist->getnodelist()) {\n$result->push($node) if $node->textContent =~ $regexp;\n}\nreturn $result;\n};\n\nmy $xc = XML::LibXML::XPathContext->new($node);\n$xc->registerFunction('grepnodes', \\&grepnodes);\nmy @nodes = $xc->findnodes('//section[grepnodes(para,\"\\bsearch(ing|es)?\\b\")]');\n"
                },
                {
                    "name": "Variables",
                    "content": "This example demonstrates \"registerVarLookup()\" method. We use XPath variables to recycle\nresults of previous evaluations:\n\nsub varlookup {\nmy ($varname,$ns,$data)=@;\nreturn $data->{$varname};\n}\n\nmy $areas = XML::LibXML->new->parsefile('areas.xml');\nmy $empl = XML::LibXML->new->parsefile('employees.xml');\n\nmy $xc = XML::LibXML::XPathContext->new($empl);\n\nmy %variables = (\nA => $xc->find('/employees/employee[@salary>10000]'),\nB => $areas->find('/areas/area[district='Brooklyn']/street'),\n);\n\n# get names of employees from $A working in an area listed in $B\n$xc->registerVarLookupFunc(\\&varlookup, \\%variables);\nmy @nodes = $xc->findnodes('$A[workarea/street = $B]/name');\n"
                }
            ]
        },
        "METHODS": {
            "content": "new\nmy $xpc = XML::LibXML::XPathContext->new();\n\nCreates a new XML::LibXML::XPathContext object without a context node.\n\nmy $xpc = XML::LibXML::XPathContext->new($node);\n\nCreates a new XML::LibXML::XPathContext object with the context node set to $node.\n\nregisterNs\n$xpc->registerNs($prefix, $namespaceuri)\n\nRegisters namespace $prefix to $namespaceuri.\n\nunregisterNs\n$xpc->unregisterNs($prefix)\n\nUnregisters namespace $prefix.\n\nlookupNs\n$uri = $xpc->lookupNs($prefix)\n\nReturns namespace URI registered with $prefix. If $prefix is not registered to any namespace\nURI returns \"undef\".\n\nregisterVarLookupFunc\n$xpc->registerVarLookupFunc($callback, $data)\n\nRegisters variable lookup function $prefix. The registered function is executed by the XPath\nengine each time an XPath variable is evaluated. It takes three arguments: $data, variable\nname, and variable ns-URI and must return one value: a number or string or any\n\"XML::LibXML::\" object that can be a result of findnodes: Boolean, Literal, Number, Node\n(e.g. Document, Element, etc.), or NodeList. For convenience, simple (non-blessed) array\nreferences containing only XML::LibXML::Node objects can be used instead of an\nXML::LibXML::NodeList.\n\ngetVarLookupData\n$data = $xpc->getVarLookupData();\n\nReturns the data that have been associated with a variable lookup function during a previous\ncall to \"registerVarLookupFunc\".\n\ngetVarLookupFunc\n$callback = $xpc->getVarLookupFunc();\n\nReturns the variable lookup function previously registered with \"registerVarLookupFunc\".\n\nunregisterVarLookupFunc\n$xpc->unregisterVarLookupFunc($name);\n\nUnregisters variable lookup function and the associated lookup data.\n\nregisterFunctionNS\n$xpc->registerFunctionNS($name, $uri, $callback)\n\nRegisters an extension function $name in $uri namespace. $callback must be a CODE reference.\nThe arguments of the callback function are either simple scalars or \"XML::LibXML::*\" objects\ndepending on the XPath argument types. The function is responsible for checking the argument\nnumber and types. Result of the callback code must be a single value of the following types:\na simple scalar (number, string) or an arbitrary \"XML::LibXML::*\" object that can be a\nresult of findnodes: Boolean, Literal, Number, Node (e.g. Document, Element, etc.), or\nNodeList. For convenience, simple (non-blessed) array references containing only\nXML::LibXML::Node objects can be used instead of a XML::LibXML::NodeList.\n\nunregisterFunctionNS\n$xpc->unregisterFunctionNS($name, $uri)\n\nUnregisters extension function $name in $uri namespace. Has the same effect as passing\n\"undef\" as $callback to registerFunctionNS.\n\nregisterFunction\n$xpc->registerFunction($name, $callback)\n\nSame as \"registerFunctionNS\" but without a namespace.\n\nunregisterFunction\n$xpc->unregisterFunction($name)\n\nSame as \"unregisterFunctionNS\" but without a namespace.\n\nfindnodes\n@nodes = $xpc->findnodes($xpath)\n\n@nodes = $xpc->findnodes($xpath, $contextnode )\n\n$nodelist = $xpc->findnodes($xpath, $contextnode )\n\nPerforms the xpath statement on the current node and returns the result as an array. In\nscalar context, returns an XML::LibXML::NodeList object. Optionally, a node may be passed as\na second argument to set the context node for the query.\n\nThe xpath expression can be passed either as a string, or as a XML::LibXML::XPathExpression\nobject.\n\nfind\n$object = $xpc->find($xpath )\n\n$object = $xpc->find($xpath, $contextnode )\n\nPerforms the xpath expression using the current node as the context of the expression, and\nreturns the result depending on what type of result the XPath expression had. For example,\nthe XPath \"1 * 3 + 52\" results in an XML::LibXML::Number object being returned. Other\nexpressions might return a XML::LibXML::Boolean object, or a XML::LibXML::Literal object (a\nstring). Each of those objects uses Perl's overload feature to ``do the right thing'' in\ndifferent contexts. Optionally, a node may be passed as a second argument to set the context\nnode for the query.\n\nThe xpath expression can be passed either as a string, or as a XML::LibXML::XPathExpression\nobject.\n\nfindvalue\n$value = $xpc->findvalue($xpath )\n\n$value = $xpc->findvalue($xpath, $contextnode )\n\nIs exactly equivalent to:\n\n$xpc->find( $xpath, $contextnode )->toliteral;\n\nThat is, it returns the literal value of the results. This enables you to ensure that you\nget a string back from your search, allowing certain shortcuts. This could be used as the\nequivalent of <xsl:value-of select=``somexpath''/>. Optionally, a node may be passed in the\nsecond argument to set the context node for the query.\n\nThe xpath expression can be passed either as a string, or as a XML::LibXML::XPathExpression\nobject.\n\nexists\n$bool = $xpc->exists( $xpathexpression, $contextnode );\n\nThis method behaves like *findnodes*, except that it only returns a boolean value (1 if the\nexpression matches a node, 0 otherwise) and may be faster than *findnodes*, because the\nXPath evaluation may stop early on the first match (this is true for libxml2 >= 2.6.27).\n\nFor XPath expressions that do not return node-set, the method returns true if the returned\nvalue is a non-zero number or a non-empty string.\n\nsetContextNode\n$xpc->setContextNode($node)\n\nSet the current context node.\n\ngetContextNode\nmy $node = $xpc->getContextNode;\n\nGet the current context node.\n\nsetContextPosition\n$xpc->setContextPosition($position)\n\nSet the current context position. By default, this value is -1 (and evaluating XPath\nfunction \"position()\" in the initial context raises an XPath error), but can be set to any\nvalue up to context size. This usually only serves to cheat the XPath engine to return given\nposition when \"position()\" XPath function is called. Setting this value to -1 restores the\ndefault behavior.\n\ngetContextPosition\nmy $position = $xpc->getContextPosition;\n\nGet the current context position.\n\nsetContextSize\n$xpc->setContextSize($size)\n\nSet the current context size. By default, this value is -1 (and evaluating XPath function\n\"last()\" in the initial context raises an XPath error), but can be set to any non-negative\nvalue. This usually only serves to cheat the XPath engine to return the given value when\n\"last()\" XPath function is called. If context size is set to 0, position is automatically\nalso set to 0. If context size is positive, position is automatically set to 1. Setting\ncontext size to -1 restores the default behavior.\n\ngetContextSize\nmy $size = $xpc->getContextSize;\n\nGet the current context size.\n\nsetContextNode\n$xpc->setContextNode($node)\n\nSet the current context node.\n",
            "subsections": []
        },
        "BUGS AND CAVEATS": {
            "content": "XML::LibXML::XPathContext objects *are* reentrant, meaning that you can call methods of an\nXML::LibXML::XPathContext even from XPath extension functions registered with the same object or\nfrom a variable lookup function. On the other hand, you should rather avoid registering new\nextension functions, namespaces and a variable lookup function from within extension functions\nand a variable lookup function, unless you want to experience untested behavior.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Matt Sergeant, Christian Glahn, Petr Pajas\n",
            "subsections": []
        },
        "HISTORICAL REMARK": {
            "content": "Prior to XML::LibXML 1.61 this module was distributed separately for maintenance reasons.\n",
            "subsections": []
        },
        "VERSION": {
            "content": "2.0134\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "2001-2007, AxKit.com Ltd.\n\n2002-2006, Christian Glahn.\n\n2006-2009, Petr Pajas.\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        }
    },
    "summary": "XML::LibXML::XPathContext - XPath Evaluation",
    "flags": [],
    "examples": [
        "This example demonstrates \"registerNs()\" method. It finds all paragraph nodes in an XHTML",
        "document.",
        "my $xc = XML::LibXML::XPathContext->new($xhtmldoc);",
        "$xc->registerNs('xhtml', 'http://www.w3.org/1999/xhtml');",
        "my @nodes = $xc->findnodes('//xhtml:p');",
        "This example demonstrates \"registerFunction()\" method by defining a function filtering nodes",
        "based on a Perl regular expression:",
        "sub grepnodes {",
        "my ($nodelist,$regexp) =  @;",
        "my $result = XML::LibXML::NodeList->new;",
        "for my $node ($nodelist->getnodelist()) {",
        "$result->push($node) if $node->textContent =~ $regexp;",
        "return $result;",
        "};",
        "my $xc = XML::LibXML::XPathContext->new($node);",
        "$xc->registerFunction('grepnodes', \\&grepnodes);",
        "my @nodes = $xc->findnodes('//section[grepnodes(para,\"\\bsearch(ing|es)?\\b\")]');",
        "This example demonstrates \"registerVarLookup()\" method. We use XPath variables to recycle",
        "results of previous evaluations:",
        "sub varlookup {",
        "my ($varname,$ns,$data)=@;",
        "return $data->{$varname};",
        "my $areas = XML::LibXML->new->parsefile('areas.xml');",
        "my $empl = XML::LibXML->new->parsefile('employees.xml');",
        "my $xc = XML::LibXML::XPathContext->new($empl);",
        "my %variables = (",
        "A => $xc->find('/employees/employee[@salary>10000]'),",
        "B => $areas->find('/areas/area[district='Brooklyn']/street'),",
        ");",
        "# get names of employees from $A working in an area listed in $B",
        "$xc->registerVarLookupFunc(\\&varlookup, \\%variables);",
        "my @nodes = $xc->findnodes('$A[workarea/street = $B]/name');"
    ],
    "see_also": []
}