0

I'm tring to show some XML for Italian Electronic invoices received, using my custom stylesheet.xsl

All is ok when XML received start with:

<?xml version="1.0" encoding="UTF-8"?>
<p:FatturaElettronica xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" versione="FPR12">

but I've received some XML starting with:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="fatturapa_v1.2.xsl"?>
<p:FatturaElettronica xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" versione="FPR12">

in this case i get browser error when I try to open file because on my webapp i have not the fatturapa_v1.2.xml saved:

Error loading style sheet: XSLT style sheet interpretation failed.

Is there a way to strip out from this XML this line only, using PHP? Thanks

<?xml-stylesheet type="text/xsl" href="fatturapa_v1.2.xsl"?>
1
  • You can try using SimpleXML. Not sure how it deals with stylesheets though. Commented Jan 24, 2022 at 15:14

1 Answer 1

1

Everything in DOM is a node. In this case this is a processing instruction. You can use Xpath to find it and then remove it from its parent node:

$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="fatturapa_v1.2.xsl"?>
<p:FatturaElettronica xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" versione="FPR12"/>
XML;

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('/processing-instruction()[name() = "xml-stylesheet"]') as $pi) {
    // var_dump($pi);
    $pi->parentNode->removeChild($pi);
}

echo $document->saveXML();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.