please, can you help me with a problem in XML transform by XSLT in PHP?
I have this code:
$xml = new SimpleXMLElement(file_get_contents(DIR_WWW . '/xslt/test.xml'));
$xsl = new DOMDocument();
$xsl->load(DIR_WWW . '/xslt/test.xslt');
$processor = new XSLTProcessor();
$processor->importStylesheet($xsl);
$filepath = DIR_WWW . '/xslt/output.xml';
file_put_contents($filepath, $processor->transformToXml($xml));
$response = new FileResponse($filepath);
$this->presenter->sendResponse($response);
But I get always this error messages:
XSLTProcessor::importStylesheet(): compilation error: file /srv/www/xslt/test.xslt line 3 element stylesheet
this is my xml file:
<?xml version="1.0" encoding="windows-1250"?>
<dat:dataPack xmlns:dat="http://www.stormware.cz/schema/version_2/data.xsd" id="01" ico="12345678" application="Externi Aplikace" version="2.0" note="Import">
<dat:dataPackItem id="1" version="2.0">
<inv:invoice xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd" version="2.0">
<inv:invoiceHeader>
<inv:invoiceType>receivedInvoice</inv:invoiceType>
<inv:number>
<typ:ids xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd">22F003</typ:ids>
</inv:number>
<inv:symVar>350</inv:symVar>
<inv:originalDocument>350</inv:originalDocument>
<inv:date>2022-05-01</inv:date>
<inv:dateAccounting>2022-05-01</inv:dateAccounting>
<inv:dateTax>2022-05-01</inv:dateTax>
</inv:invoiceHeader>
<inv:invoiceSummary>
<inv:foreignCurrency>
<typ:currency xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd">
<typ:ids xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd">EUR</typ:ids>
</typ:currency>
<typ:priceSum xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd">10,6</typ:priceSum>
</inv:foreignCurrency>
</inv:invoiceSummary>
</inv:invoice>
</dat:dataPackItem>
</dat:dataPack>
This is my xslt file:
<?xml version="1.0" encoding="Windows-1250"?>
<!--xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rsp="http://www.stormware.cz/schema/version_2/response.xsd" xmlns:rdc="http://www.stormware.cz/schema/version_2/documentresponse.xsd" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd" xmlns:lst="http://www.stormware.cz/schema/version_2/list.xsd" xmlns:acu="http://www.stormware.cz/schema/version_2/accountingunit.xsd" xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd" xmlns:vch="http://www.stormware.cz/schema/version_2/voucher.xsd" xmlns:int="http://www.stormware.cz/schema/version_2/intDoc.xsd" xmlns:stk="http://www.stormware.cz/schema/version_2/stock.xsd" xmlns:ord="http://www.stormware.cz/schema/version_2/order.xsd" xmlns:ofr="http://www.stormware.cz/schema/version_2/offer.xsd" xmlns:enq="http://www.stormware.cz/schema/version_2/enquiry.xsd" xmlns:vyd="http://www.stormware.cz/schema/version_2/vydejka.xsd" xmlns:pri="http://www.stormware.cz/schema/version_2/prijemka.xsd" xmlns:pre="http://www.stormware.cz/schema/version_2/prevodka.xsd" xmlns:vyr="http://www.stormware.cz/schema/version_2/vyroba.xsd" xmlns:pro="http://www.stormware.cz/schema/version_2/prodejka.xsd" xmlns:adb="http://www.stormware.cz/schema/version_2/addressbook.xsd" xmlns:dat="http://www.stormware.cz/schema/version_2/data.xsd" version="3.0">
<xsl:output method="xml" version="1.0" encoding="windows-1250" indent="yes"/>
<xsl:namespace-alias stylesheet-prefix="rsp" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="rdc" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="typ" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="lst" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="acu" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="inv" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="vch" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="int" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="stk" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="ord" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="ofr" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="enq" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="vyd" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="pri" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="pre" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="adb" result-prefix="xsl"/>
<xsl:namespace-alias stylesheet-prefix="pro" result-prefix="xsl"/>
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyzáéíóúůýěščřžť'"/>
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚŮÝĚŠČŘŽŤ'"/>
<xsl:decimal-format name="cesky" decimal-separator="."/>
<!-- Zkonvertuje Dokument z EDI prefabrikatu do S3 -->
<xsl:template match="dat:dataPack">
<xsl:element name="MoneyData">
<xsl:element name="SeznamFaktPrij">
<xsl:apply-templates select="dat:dataPackItem"/>
</xsl:element>
</xsl:element>
</xsl:template>
<!-- Seznam faktur -->
<xsl:template match="dat:dataPackItem">
<xsl:element name="FaktPrij">
<xsl:apply-templates select="inv:invoice"/>
</xsl:element>
</xsl:template>
<!-- Faktura -->
<xsl:template match="inv:invoice">
<xsl:apply-templates select="inv:invoiceHeader"/>
<xsl:apply-templates select="inv:invoiceSummary"/>
</xsl:template>
<!-- Zjištění, zda se jedná o cizí měnu ve faktuře a sumarizace faktury -->
<xsl:template match="inv:invoiceSummary">
<xsl:choose>
<xsl:when test="inv:foreignCurrency">
<xsl:element name="Valuty">
<xsl:element name="Mena">
<xsl:element name="Kod">
<xsl:value-of select="inv:foreignCurrency/typ:currency/typ:ids"/>
</xsl:element>
<xsl:element name="Mnozstvi">
<xsl:value-of select="inv:foreignCurrency/typ:amount"/>
</xsl:element>
<xsl:element name="Kurs">
<xsl:value-of select="inv:foreignCurrency/typ:rate"/>
</xsl:element>
<xsl:element name="SouhrnDPH">
<xsl:if test="inv:foreignCurrency/typ:priceSum">
<xsl:element name="Zaklad0"><xsl:value-of select="inv:foreignCurrency/typ:priceSum"/></xsl:element>
</xsl:if>
<xsl:if test="inv:foreignCurrency/typ:priceLow">
<xsl:element name="Zaklad5"><xsl:value-of select="inv:foreignCurrency/typ:priceLow"/></xsl:element>
</xsl:if>
<xsl:if test="inv:foreignCurrency/typ:priceLowVAT">
<xsl:element name="DPH5"><xsl:value-of select="inv:foreignCurrency/typ:priceLowVAT"/></xsl:element>
</xsl:if>
<xsl:if test="inv:foreignCurrency/typ:priceHigh">
<xsl:element name="Zaklad22"><xsl:value-of select="inv:foreignCurrency/typ:priceHigh"/></xsl:element>
</xsl:if>
<xsl:if test="inv:foreignCurrency/typ:priceHighVAT">
<xsl:element name="DPH22"><xsl:value-of select="inv:foreignCurrency/typ:priceHighVAT"/></xsl:element>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="SouhrnDPH">
<xsl:choose>
<xsl:when test="inv:homeCurrency/typ:priceNone">
<xsl:choose>
<xsl:when test="inv:homeCurrency/typ:round/typ:priceRound">
<xsl:element name="Zaklad0"><xsl:value-of select="(inv:homeCurrency/typ:priceNone)+(inv:homeCurrency/typ:round/typ:priceRound)"/></xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="Zaklad0"><xsl:value-of select="inv:homeCurrency/typ:priceNone"/></xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:if test="inv:homeCurrency/typ:round/typ:priceRound">
<xsl:element name="Zaklad0"><xsl:value-of select="inv:homeCurrency/typ:round/typ:priceRound"/></xsl:element>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="inv:homeCurrency/typ:priceLow">
<xsl:element name="Zaklad5"><xsl:value-of select="inv:homeCurrency/typ:priceLow"/></xsl:element>
</xsl:if>
<xsl:if test="inv:homeCurrency/typ:priceLowVAT">
<xsl:element name="DPH5"><xsl:value-of select="inv:homeCurrency/typ:priceLowVAT"/></xsl:element>
</xsl:if>
<xsl:if test="inv:homeCurrency/typ:priceHigh">
<xsl:element name="Zaklad22"><xsl:value-of select="inv:homeCurrency/typ:priceHigh"/></xsl:element>
</xsl:if>
<xsl:if test="inv:homeCurrency/typ:priceHighVAT">
<xsl:element name="DPH22"><xsl:value-of select="inv:homeCurrency/typ:priceHighVAT"/></xsl:element>
</xsl:if>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- informace o faktuře -->
<xsl:template match="inv:invoiceHeader">
<xsl:element name="Doklad">
<xsl:value-of select="inv:number/typ:numberRequested"/>
</xsl:element>
<xsl:element name="PrijatDokl">
<xsl:value-of select="inv:number/typ:numberRequested"/>
</xsl:element>
<xsl:element name="EvCisDokl">
<xsl:value-of select="inv:number/typ:numberRequested"/>
</xsl:element>
<!-- Zjištění dobropisu -->
<xsl:choose>
<xsl:when test="inv:invoiceType='issuedCreditNotice'">
<xsl:element name="Dobropis">1</xsl:element>
<xsl:element name="DobrDUZP">
<xsl:value-of select="inv:dateTax"/>
</xsl:element>
<xsl:element name="Doruceno">
<xsl:value-of select="inv:dateTax"/>
</xsl:element>
</xsl:when>
<xsl:when test="inv:invoiceType='issuedCorrectiveTax'">
<xsl:element name="Dobropis">1</xsl:element>
<xsl:element name="DobrDUZP">
<xsl:value-of select="inv:dateTax"/>
</xsl:element>
<xsl:element name="Doruceno">
<xsl:value-of select="inv:dateTax"/>
</xsl:element>
</xsl:when>
<xsl:when test="inv:invoiceType='issuedInvoice'">
<xsl:element name="Dobropis">0</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="Dobropis">0</xsl:element>
</xsl:otherwise>
</xsl:choose>
<xsl:element name="Popis">
<xsl:value-of select="inv:text"/>
</xsl:element>
<xsl:element name="TextPredFa">
<xsl:value-of select="inv:text"/>
</xsl:element>
<!-- rozlišení malých a velkých písmen -->
<xsl:element name="Cinnost">
<xsl:value-of select="translate(inv:activity/typ:ids, $smallcase, $uppercase)"/>
</xsl:element>
<xsl:element name="Zakazka">
<xsl:value-of select="translate(inv:contract/typ:ids, $smallcase, $uppercase)"/>
</xsl:element>
<xsl:element name="Stredisko">
<xsl:value-of select="translate(inv:centre/typ:ids, $smallcase, $uppercase)"/>
</xsl:element>
<xsl:element name="Vystaveno">
<xsl:value-of select="inv:date"/>
</xsl:element>
<xsl:element name="DatUcPr">
<xsl:value-of select="inv:dateTax"/>
</xsl:element>
<xsl:element name="PlnenoDPH">
<xsl:value-of select="inv:dateTax"/>
</xsl:element>
<xsl:element name="Splatno">
<xsl:value-of select="inv:dateDue"/>
</xsl:element>
<xsl:element name="DatSkPoh">
<xsl:value-of select="inv:dateTax"/>
</xsl:element>
<xsl:element name="VarSymbol">
<xsl:value-of select="inv:symVar"/>
</xsl:element>
<xsl:element name="ParSymbol">
<xsl:value-of select="inv:symVar"/>
</xsl:element>
<xsl:element name="KonstSym">
<xsl:value-of select="inv:symConst"/>
</xsl:element>
<xsl:element name="Uhrada">
<xsl:value-of select="inv:paymentType/typ:ids"/>
</xsl:element>
<!-- <xsl:element name="TextPredFa"> <xsl:value-of select="inv:dateTax"/> </xsl:element> -->
<xsl:element name="CObjednavk">
<xsl:value-of select="inv:numberOrder"/>
</xsl:element>
<!-- Informace o dodavateli -->
<xsl:element name="DodOdb">
<xsl:element name="Tel">
<xsl:element name="Cislo">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:phone"/>
</xsl:element>
</xsl:element>
<xsl:element name="EMail">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:email"/>
</xsl:element>
<xsl:element name="ICO"> <xsl:value-of select="normalize-space(replace(inv:partnerIdentity/typ:address/typ:ico,'[a-zA-Z]',''))"/> </xsl:element>
<xsl:element name="DIC">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:dic"/>
</xsl:element>
<!-- sloučení Názvu firmy a Jména kontaktní osoby -->
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:company) > 0">
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:name) > 0">
<xsl:element name="FaktNazev">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:company"/> * <xsl:value-of select="inv:partnerIdentity/typ:address/typ:name"/>
</xsl:element>
<xsl:element name="ObchNazev">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:company"/> * <xsl:value-of select="inv:partnerIdentity/typ:address/typ:name"/>
</xsl:element>
</xsl:if>
</xsl:if>
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:company) > 0">
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:name) = 0">
<xsl:element name="FaktNazev">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:company"/>
</xsl:element>
<xsl:element name="ObchNazev">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:company"/>
</xsl:element>
</xsl:if>
</xsl:if>
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:company) = 0">
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:name) > 0">
<xsl:element name="FaktNazev">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:name"/>
</xsl:element>
<xsl:element name="ObchNazev">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:name"/>
</xsl:element>
</xsl:if>
</xsl:if>
<xsl:element name="ObchAdresa">
<xsl:element name="Ulice">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:street"/>
</xsl:element>
<xsl:element name="Misto">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:city"/>
</xsl:element>
<xsl:element name="PSC">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:zip"/>
</xsl:element>
</xsl:element>
<xsl:element name="FaktAdresa">
<xsl:element name="Ulice">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:street"/>
</xsl:element>
<xsl:element name="Misto">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:city"/>
</xsl:element>
<xsl:element name="PSC">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:zip"/>
</xsl:element>
</xsl:element>
</xsl:element>
<!-- Informace o konečném příjemci -->
<xsl:element name="KonecPrij">
<!-- sloučení Názvu firmy a Jména kontaktní osoby -->
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:company) > 0">
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:name) > 0">
<xsl:element name="Nazev">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:company"/> * <xsl:value-of select="inv:partnerIdentity/typ:address/typ:name"/>
</xsl:element>
</xsl:if>
</xsl:if>
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:company) > 0">
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:name) = 0">
<xsl:element name="Nazev">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:company"/>
</xsl:element>
</xsl:if>
</xsl:if>
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:company) = 0">
<xsl:if test="string-length(inv:partnerIdentity/typ:address/typ:name) > 0">
<xsl:element name="Nazev">
<xsl:value-of select="inv:partnerIdentity/typ:address/typ:name"/>
</xsl:element>
</xsl:if>
</xsl:if>
<xsl:element name="Adresa">
<xsl:element name="Misto">
<xsl:value-of select="inv:partnerIdentity/typ:shipToAddress/typ:city"/>
</xsl:element>
<xsl:element name="Ulice">
<xsl:value-of select="inv:partnerIdentity/typ:shipToAddress/typ:street"/>
</xsl:element>
<xsl:element name="PSC">
<xsl:value-of select="inv:partnerIdentity/typ:shipToAddress/typ:zip"/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
What can I doing with this message?
@MartinHonnen
Ok, thank you.
I installed docker image by using this code of dockerfile:
FROM wodby/php:8.1-dev
USER root
RUN apk add --no-cache curl unzip
&& curl -o libsaxon.zip https://www.saxonica.com/download/libsaxon-HEC-linux-v12.1.zip
&& unzip libsaxon.zip -d /usr/local/lib
&& rm -f libsaxon.zip
&& echo "extension=saxon.so" > /usr/local/etc/php/conf.d/saxon.ini
USER wodby
I use nette framework. Can you help me, how can I use saxonProcessor in my framework? I have this config.neon
parameters:
saxonSoPath: '/usr/local/etc/php/conf.d/saxon.ini'
# lib .so
saxon:
type: SaxonSaxonProcessor
factory: SaxonSaxonProcessor::__construct
arguments: [%saxonSoPath%]
But this code it’s not working.
If I create new instance in php file, I get this error:
Class or interface ‘SaxonSaxonProcessor’ not found.
2
Answers
Thank you Michael. I try it. Now I try install saxon for PHP, but I've some problems with them. I'll see if I can get it going.
Before going to extremes, try changing line 212 of your stylesheet from:
to:
CAVEAT: Untested, may produce unintended result (the purpose of the original code is not clear).
Note that your stylesheet is written very poorly and could use a ground-up rewrite.