How can DOMDocument and XSLTProcessor be utilized in PHP to transform XML data with XSL stylesheets stored as strings?

To transform XML data with XSL stylesheets stored as strings in PHP, you can use the DOMDocument class to load the XML data and the XSLTProcessor class to apply the XSL stylesheet. You can load the XSL stylesheet as a string, create a new XSLTProcessor object, import the XSL stylesheet, and then apply the transformation to the XML data.

$xmlData = '<data><item>1</item><item>2</item><item>3</item></data>';
$xslStylesheet = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/data">
  <output>
    <xsl:for-each select="item">
      <item><xsl:value-of select="."/></item>
    </xsl:for-each>
  </output>
</xsl:template>
</xsl:stylesheet>';

$dom = new DOMDocument();
$dom->loadXML($xmlData);

$xsl = new DOMDocument();
$xsl->loadXML($xslStylesheet);

$processor = new XSLTProcessor();
$processor->importStylesheet($xsl);

echo $processor->transformToXML($dom);