Table of Contents

xpath in =importXML() for extracting meta descriptions

Extracting Meta Descriptions from XML using XPath and importXML() Extracting specific data points from raw HTML or XML documents is a common task in web...

2026-08-10

Extracting Meta Descriptions from XML using XPath and importXML()

Extracting specific data points from raw HTML or XML documents is a common task in web scraping and data processing. When you move into the realm of using PHP's DOMDocument functions, specifically importXML(), combined with XPath, you gain powerful control over navigating the document structure. However, translating that navigation logic into a format easily consumable by external tools like Google Sheets often presents unique challenges related to syntax and quoting.

You are running into a common frustration: knowing the correct XPath syntax is one thing, but ensuring that the resulting string format works correctly when imported into a spreadsheet tool is another entirely. Let's break down how to reliably extract meta descriptions using XPath and address those tricky quotation issues.

Understanding the Target Structure

To successfully extract meta descriptions, we first need to understand the structure of the HTML document as an XML tree. Meta tags are nested within the <head> section of the document. A standard description tag looks like this:

<head>
      <meta name="description" content="This is the page's meta description." />
      <title>Page Title</title>
  </head>
  

To pull out the value from the content attribute of the specific <meta> tag, we need an XPath expression that targets this path.

Crafting the Correct XPath Expression

The core goal is to select the attribute value (@content) of any element (meta) that has a specific attribute name (@name) set to 'description'. Using the descendant selector (//) makes the query robust regardless of where the <head> tag sits in the document.

A highly effective and general XPath for this task is:

//meta[@name='description']/@content
  

When you use importXML(URL, xpath_expression), PHP parses the XML document from the URL and returns a DOM object based on the result of the expression. The @content part specifically selects the value of the content attribute for all matching nodes.

Addressing the Google Sheets Quoting Issue

The reason you are experiencing difficulties when pasting these results into Google Sheets is usually not an issue with the XPath itself, but how the resulting string data interacts with Google Sheets' import mechanism. When exporting data from PHP to a spreadsheet format (like CSV or simply pasting text), the delimiter and internal quoting rules matter greatly.

If you are using a method that outputs raw text directly into the sheet cells, standard double quotes (") for wrapping the extracted content often work best when importing structured data. The variations you tested—using single quotes (') versus double quotes (") to denote string literals in XPath—are syntax rules for the XPath engine itself, not necessarily the output format required by the spreadsheet application.

For maximum compatibility when dealing with external tools:

  1. Extract Pure Text: Ensure your PHP code extracts only the text content and does not include surrounding XML tags or extra whitespace.
  2. Use Standard Double Quotes for Output: When preparing the data for export, ensure you are using standard delimited formats (like CSV) where the field contents are properly quoted if they contain spaces.

Practical PHP Implementation Example

Here is how you might structure the extraction in a practical scenario, demonstrating the use of importXML():

<?php
  
  $url = 'https://www.example.com';
  
  try {
      // 1. Import the XML document
      $xml = simplexml_load_file($url);
  
      if ($xml !== false) {
          // 2. Use XPath to find all meta description content values
          $xpath = new DOMXPath($xml);
          $descriptions = $xpath->query("//meta[@name='description']/@content");
  
          echo "<h2>Extracted Meta Descriptions:</h2>";
  
          if ($descriptions->length > 0) {
              foreach ($descriptions as $node) {
                  // Get the value of the attribute
                  echo "<p>Description: " . htmlspecialchars($node->nodeValue) . "</p>";
              }
          } else {
              echo "<p>No meta descriptions found.</p>";
          }
      } else {
          echo "Error loading XML from $url.";
      }
  
  } catch (Exception $e) {
      echo "An error occurred: " . $e->getMessage();
  }
  
  ?>
  

As you can see, using DOMXPath on the imported XML object provides a programmatic way to query the structure. While the final step of feeding this data into Google Sheets requires careful formatting, mastering the XPath selection first ensures that the source data you are pulling is clean and accurate. For complex data manipulation tasks involving web content processing, leveraging robust frameworks like those found in the Laravel ecosystem can significantly streamline these parsing operations.

Stefan

Stefan

SEO engineer and Laravel developer. Building tools to help Laravel applications rank higher in search results.

Share this article

Back to Blog