2011-09-28 4 views
-1

어떻게 작동하는지에 대한 이해가 부족한 것을 용서하십시오! 도움을 줄 준비가되지 않았다면 게시를 귀찮게하지 마십시오. 고맙습니다!DOMXpath의 XSL tansform 결과 -> PHP에서 평가

나는이 :

$XML = new DOMDocument(); 
$XML->load('demo.xml'); 

$xpath = new DOMXpath($XML); 
$elements = $xpath->evaluate($_GET["xpath"]); 

$XSL = new DOMDocument(); 
$XSL->load('xml2json.xsl', LIBXML_NOCDATA); 

$xslt = new XSLTProcessor(); 
$xslt->importStylesheet($XSL); 

echo $xslt->transformToXML($elements); 

나는 다음과 같은 오류가 발생합니다 :이 작품을 만들기 위해

(!) Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: Invalid Document in C:\wamp\www\content.php on line 18 

은 어떻게있는 DOMDocument에 DOMNodeList을 변환합니까?

내가 어떻게 작동 시키는가? 당신은

$elements = $xpath->evaluate($_GET["xpath"]); 
… 
echo $xslt->transformToXML($elements); 

return values for DOMXPath::evaluate()을하고

function getContent(&$NodeContent="",$nod) 
{ $NodList=$nod->childNodes; 
    for($j=0 ; $j < $NodList->length; $j++) 
    {  $nod2=$NodList->item($j);//Node j 
     $nodemane=$nod2->nodeName; 
     $nodevalue=$nod2->nodeValue; 
     if($nod2->nodeType == XML_TEXT_NODE) 
      $NodeContent .= $nodevalue; 
     else 
     {  $NodeContent .= "<$nodemane"; 
      $attAre=$nod2->attributes; 
      foreach ($attAre as $value) 
       $NodeContent .=" {$value->nodeName}='{$value->nodeValue}'" ; 
      $NodeContent .=">";      
      getContent($NodeContent,$nod2);      
      $NodeContent .= "</$nodemane>"; 
     } 
    } 

} 


$XML = new DOMDocument(); 
$XML->load('demo.xml'); 

$xpath = new DOMXpath($XML); 
$elements = $xpath->query($_GET["xpath"]); 

$XSL = new DOMDocument(); 
$XSL->load('xml2json.xsl', LIBXML_NOCDATA); 

$xslt = new XSLTProcessor(); 
$xslt->importStylesheet($XSL); 

$newdoc = new DOMDocument; 
$newdoc -> preserveWhiteSpace = false; 
$newdoc -> formatOutput = true; 

$elm = $elements->item(0); 
getContent($docstring,$elm); 
$docstring = '<root>'.$docstring.'</root>'; 
$docstring = str_replace(array("\r\n", "\r", "\n", "\t"), '', $docstring); 
$newdoc -> LoadXML($docstring); 
echo $xslt->transformToXML($newdoc); 
+1

"RTFE"에 대한 닫기 플래그가 있어야합니다. – salathe

답변

1

Returns a typed result if possible or a DOMNodeList containing all nodes matching the given XPath expression. If the expression is malformed or the contextnode is invalid, DOMXPath::evaluate() returns FALSE .

string XSLTProcessor::transformToXML (DOMDocument $doc) 

, 당신은전달되지 않은 method signature for transformToXML() 상태입니다"잘못된 문서"오류가 발생합니다.

+1

안녕하세요, 그렇습니다.하지만 어떻게이 예제의 문서로 변환합니까? –

+0

새 DOMDocument를 만듭니다. 결과 노드 가져 오기 및 추가. – Gordon

+0

감사합니다. 문제가 생겼습니다. 해결책이 없습니다. 행운을 빌어 DOMNodeList에서 DOMDocument를 생성하는 검색 기능이 있습니다. 코드 샘플을 제공해 주시겠습니까? –