我希望能够使用XSLT处理XPath已选择的XML代码段。当我通过$xmlSnippet
时,它的行为就好像我已经通过了整个$xml
一样。如何告诉处理器在代码段而不是整个文件上工作?
XML
<?xml version="1.0" encoding="UTF-16"?>
<FMPReport link="Summary.xml" creationTime="12:10:13 pm" creationDate="30/10/2015" type="Report" version="14.0.3">
<File name="assets.fmp12" path="server.com">
<CustomFunctionCatalog>
<CustomFunction id="1" functionArity="1" visible="True" parameters="pageID" name="!filemakerstandards.org">
<Calculation><![CDATA["http://filemakerstandards.org/pages/viewpage.action?pageId=" & pageID]]></Calculation>
</CustomFunction>
<CustomFunction id="2" functionArity="2" visible="True" parameters="name;value" name="#">
<Calculation><![CDATA['example 2']]></Calculation>
</CustomFunction>
</CustomFunctionCatalog>
</File>
</FMPReport>
XSL
<?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>
PHP
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'
}
答案 0 :(得分:1)
没有得到任何答案我只是通过构建字符串并将其加载为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);
,然后使用XSLT处理。