Php 对xpath选择返回的SimpleXML元素使用TransformToXml
我希望能够使用XSLT处理XPath选择的XML片段。当我传递Php 对xpath选择返回的SimpleXML元素使用TransformToXml,php,xslt,simplexml,Php,Xslt,Simplexml,我希望能够使用XSLT处理XPath选择的XML片段。当我传递$xmlsippet时,它的行为就像我传递了整个$xml一样。我如何告诉处理器处理代码段而不是整个文件 XML 没有得到任何答案,我只是通过构建一个字符串并将其加载为XML来解决这个问题 $cfString = ''; foreach( $cfArray as $item){ $cfString .= $item->asXML(); } $cfXml = <<<XML <?xml version
$xmlsippet
时,它的行为就像我传递了整个$xml
一样。我如何告诉处理器处理代码段而不是整个文件
XML
没有得到任何答案,我只是通过构建一个字符串并将其加载为XML来解决这个问题
$cfString = '';
foreach( $cfArray as $item){
$cfString .= $item->asXML();
}
$cfXml = <<<XML
<?xml version='1.0'?>
<FMPReport>
<File name="{$fileName[0]}">
<CustomFunctionCatalog>
$cfString
</CustomFunctionCatalog>
</File>
</FMPReport>
XML;
$xml = simplexml_load_string($cfXml);
$cfString='';
foreach($cfArray作为$item){
$cfString.=$item->asXML();
}
$cfXml=
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:value-of select="name(child::*[1])"/>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
function displayCustomFunction($customFunctionId) {
LIBXML_NOWARNING;
LIBXML_NOCDATA;
LIBXML_PARSEHUGE;
LIBXML_BIGLINES;
LIBXML_COMPACT;
$filePath = 'xml/abc';
$xslPath = 'xsl/abc';
$xml = simplexml_load_file( $filePath, 'SimpleXMLElement');
// ->xpath returns an array, in this case, an array of one item. array()[0] will be a SimpleXMLElement
$xmlSnippet = $xml->xpath('File/CustomFunctionCatalog/CustomFunction[@id=\''.$customFunctionId.'\']')[0];
$xsl = simplexml_load_file( $xslPath, 'SimpleXMLElement');
$xslt = new XSLTProcessor();
$xslt->importStylesheet($xsl);
$xslt->transformToXml($xml); // returns 'FMPReport'
$xslt->transformToXml($xmlSnippet); // returns 'FMPReport'. Expecting 'Calculation'
}
$cfString = '';
foreach( $cfArray as $item){
$cfString .= $item->asXML();
}
$cfXml = <<<XML
<?xml version='1.0'?>
<FMPReport>
<File name="{$fileName[0]}">
<CustomFunctionCatalog>
$cfString
</CustomFunctionCatalog>
</File>
</FMPReport>
XML;
$xml = simplexml_load_string($cfXml);