How to remove Children from Xpath Expression Nodes in PHP
2 min readApr 24, 2020
On today’s agenda: using PHP to set up a method for deleting the children from Xpath expression nodes.
To begin our process, we shall open the command line for Composer and paste in this command here:
composer require cloudmersive/cloudmersive_document_convert_api_client
That will install the API client that we will be using. Once that’s done with we shall proceed with calling this function here:
<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\ConvertDataApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$input_file = "/path/to/file"; // \SplFileObject | Input XML file to perform the operation on.$x_path_expression = "x_path_expression_example"; // string | Valid XML XPath query expressiontry {$result = $apiInstance->convertDataXmlEditRemoveAllChildNodesWithXPath($input_file, $x_path_expression);print_r($result);} catch (Exception $e) {echo 'Exception when calling ConvertDataApi->convertDataXmlEditRemoveAllChildNodesWithXPath: ', $e->getMessage(), PHP_EOL;}?>
And there you have it, our method is completely set up. From there it’s just a matter of inputting our XML file and the Xpath expression to be used. It doesn’t get any easier than that!