What is the purpose of using normalize-space in an XPath query on an attribute like id?

When using normalize-space in an XPath query on an attribute like id, the purpose is to remove any leading or trailing white spaces and collapse multiple consecutive white spaces into a single space. This ensures that the comparison of the attribute value is accurate and consistent, especially when dealing with user input or data that may have inconsistencies in spacing.

// Example of using normalize-space in an XPath query on an id attribute
$xml = '<root>
            <element id="  123  "></element>
            <element id="456"></element>
        </root>';

$doc = new DOMDocument();
$doc->loadXML($xml);

$xpath = new DOMXPath($doc);

// Using normalize-space to remove leading/trailing spaces and collapse multiple spaces
$query = '//element[normalize-space(@id) = "123"]';
$elements = $xpath->query($query);

foreach ($elements as $element) {
    echo $element->getAttribute('id') . "\n";
}