Reputation: 1995
recently we received a new xsl file which is using XSLT namespace :
xmlns:date="http://exslt.org/dates-and-times"
and somewhere in the xsl it call following function:
<xsl:value-of select="date:format-date(concat('1900-', @month, '-1'),'MMM')"/>
the problem is the server is behind a proxy, so when i use following code it can't find the namespace and it through back error.
this is how I call xsl file:
$xsl_doc = new DOMDocument();
$xsl_doc->load("stylesheet/report.xsl"); //load the stylesheet
$xml_doc = new DOMDocument();
$xml_doc->loadXML($xml); //load the xml
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl_doc);
$newdom = $proc->transformToDoc($xml_doc);
moreover when i enter this address in browswerhttp://exslt.org/dates-and-times
it return error 404
this is the error that I am getting at first place :
Warning: XSLTProcessor::transformToDoc() [xsltprocessor.transformtodoc]: runtime error: file file: stylesheet/section_b.xsl line 64 element value-of in \Application\_preview.php on line 25
Warning: XSLTProcessor::transformToDoc() [xsltprocessor.transformtodoc]: XPath evaluation returned no result. in \Application\_preview.php on line 25
as you can see it throw error at line 64 which is the place format-date
function is used
in the XSLT website is mentioned about importing the function into the xslt, so I download date.zip, extract and add following code to the xsl file, the first 3 line of the xsl become like following :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:response="http://ws.mywebsite.com.my/myosnet/response"
xmlns:date="http://exslt.org/dates-and-times"
version="1.0">
<xsl:import href="ext_module/date/date.xsl" />
then i tried to load again, but still it through back error
Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: compilation error: file file: stylesheet/ext_module/date/functions/format-date/date.format-date.xsl line 5 element import in \Application\_preview.php on line 24
Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: xsltParseStylesheetTop: ignoring misplaced import element in \Application\_preview.php on line 24
Warning: XSLTProcessor::transformToDoc() [xsltprocessor.transformtodoc]: No stylesheet associated to this object in \Application\_preview.php on line 25
Fatal error: Call to a member function saveHTML() on a non-object in \Application\_preview.php on line 26
I even tried to bring the function definition in the date.format.xsl but i failed. my question is how to add these custom functions. or how to make it to use proxy.
Upvotes: 0
Views: 1304
Reputation: 1995
I solved the problem by adding the function date:format-date
to the xslt by following the bellow step:
against the exslt instruction for the php translation just the .function
file is enough and shouldn't import the main file.
so first I downloaded the related files from exslt.org , in my case i downloaded date.zip, in the zip file there are many files which just the .function
file is needed. added following code to the xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:response="http://ws.mywebsite.com.my/myosnet/response"
xmlns:date="http://exslt.org/dates-and-times"
version="1.0">
<xsl:import href="ext_module/date/date.format-date.function.xsl" />
in the date.format-date.function.xsl
i noticed, it import another dependency so I downloaded string dependency and linked them together by copying str.padding.function.xsl
next to the date.format-date.function.xsl
.
up here the php error fixed, but the data not showing properly, so i read the format-date
function and noticed the function check the length to pinpoint the year-month-day and if lets say day not be in the 2character format then it will not work. so I changed @month with introducing a new variable and modified the length of the @month
<xsl:variable name="hnmnth">
<xsl:number value="@month" format="01" />
</xsl:variable>
and changed the call to the function :
<xsl:value-of select="date:format-date(concat('1900-', $hnmnth, '-01'),'MMM')"/>
Upvotes: 0
Reputation: 116992
the problem is the server is behind a proxy, so when i use following code it can't find the namespace
I don't think that is the correct analysis. A namespace URI is just a meaningless string - the XSLT processor is not trying to "find" it.
The much more likely reason is that your processor does not support the EXSLT date:format-date() extension function. In fact, I don't know of any XSLT 1.0 processor that does, so it's kind of strange that someone would write a stylesheet using it.
You can test this by running the following stylesheet (on any valid XML input):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:text>Vendor: </xsl:text>
<xsl:value-of select="system-property('xsl:vendor')"/>
<xsl:text>
Version: </xsl:text>
<xsl:value-of select="system-property('xsl:version')"/>
<xsl:text>
Testing function: </xsl:text>
<xsl:choose>
<xsl:when test="function-available('date:format-date()')">
<xsl:value-of select="date:format-date('1900-05-01', 'MMM')" />
</xsl:when>
<xsl:otherwise>
<xsl:text>format-date() is not available</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
AFAIK, PHP uses the libxslt processor, so you should be seeing the following result:
<?xml version="1.0" encoding="utf-8"?>
Vendor: libxslt
Version: 1.0
Testing function: format-date() is not available
The other problem is that the function call is incorrect:
concat('1900-', @month, '-1')
does not produce a valid date.
Now, since it seems that the only reason to call the function here is to format the month as MMM, the simplest solution would be to replace the entire function call:
<xsl:value-of select="date:format-date(concat('1900-', @month, '-1'),'MMM')"/>
with:
<xsl:value-of select="substring('JanFebMarAprMayJunJulAugSepOctNovDec', 3*(@month - 1) + 1, 3)" />
Upvotes: 2