I'm getting a few errors from XSLTProcessor:
XSLTProcessor::transformToDoc() [<a href='function.XSLTProcessor-transformToDoc'>function.XSLTProcessor-transformToDoc</a>]: Invalid or inclomplete context
XSLTProcessor::transformToDoc() [<a href='function.XSLTProcessor-transformToDoc'>function.XSLTProcessor-transformToDoc</a>]:
XSLTProcessor::transformToDoc() [<a href='function.XSLTProcessor-transformToDoc'>function.XSLTProcessor-transformToDoc</a>]: xsltValueOf: text copy failed in
Which is parsing this XSLT Line:
<xsl:apply-templates select="page/sections/section" mode="subset"/>
The section is:
<xsl:template match="page/sections/section" mode="subset">
<a href="#{shorttitle}">
<xsl:value-of select="title"/>
</a>
<xsl:if test="position() != last()"> | </xsl:if>
</xsl:template>
The XML that the section is parsing is:
<shorttitle>About</shortti开发者_如何转开发tle>
<title>#~ About</title>
The PHP XSLT Code is:
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load( $xsltFile, LIBXML_NOCDATA);
$xslt->importStylesheet( $XSL );
print $xslt->transformToXML( $XML );
My suspicion about the the errors is due to content. I'm not getting these errors with Firefox's XSLT rendering, nor am I getting an invalid XML document on the backend.I'm not getting errors on the load, its just on the transformToXML function.
Works for me (php5.3).
I modified slightly the code to make it run but I can't see any pb with the XSLT. I did not encounter the message you describe.
php
<?php
$xsltFile = "a.xslt";
$xmlFile = "a.xml";
$xslt = new XSLTProcessor();
$xsl = new DOMDocument();
$xml = new DOMDocument();
$xsl->load( $xsltFile, LIBXML_NOCDATA);
$xml->load( $xmlFile, LIBXML_NOCDATA );
$xslt->importStylesheet( $xsl );
print $xslt->transformToXML( $xml );
?>
xml
<page>
<sections>
<section>
<shorttitle>http://about.com/about.php</shorttitle>
<title>#~ About</title>
</section>
</sections>
</page>
xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:apply-templates select="page/sections/section" mode="subset"/>
</xsl:template>
<xsl:template match="page/sections/section" mode="subset">
<a href="#{shorttitle}">
<xsl:value-of select="title"/>
</a>
<xsl:if test="position() != last()"> | </xsl:if>
</xsl:template>
</xsl:stylesheet>
If you are running in to errors with XSLTProcessor, it is often worth checking the version of the libxml and libxslt versions that your system is using (see: http://www.php.net/manual/en/xsl.requirements.php). The easiest way to check these is to look at the output of
<?php phpinfo(); ?>
You are looking for the "libxml Version" and the "libxslt compiled against libxml Version". You may be using syntax or structure that is not supported by the version you use.
If this is not the source of your problem, we will likely need to see more of the code, xml and xslt so that we have the proper context. As Alain's answer indicates, by completing your syntax somewhat arbitrarily to be executable, there are no immediate problems apparent.
精彩评论