0

对于后续代码,当我放

$doc = new DomDocument('1.0');

我在 DOMDocument::saveXML() xmlEscapeEntities 中收到警告:字符超出范围

当我把

$doc = new DomDocument('1.0','UTF-8');

我在 $xmldoc = DOMDocument::loadXML($xml);

有代码

function readXsltTemplateAdmin($XsltFileName,$arrData,$params="",$other="",$number="")
    {

        //      kreate XML from array
        /** primer:
        * $xml = <<<EOB
        * <alldata>
        *  <datarow>
        *   <name>sasa</name>
        *   <comment>aab</comment>  
        *  </datatrow>
        *  <datatrow>
        *   <name>dragana</name>
        *   <comment>eeeee</comment>  
        *  </commentrow>
        * </alldata>
        * EOB;
        */

        $doc = new DomDocument('1.0','UTF-8');
        //$doc = new DomDocument('1.0');

        // root 
        $root = $doc->createElement('alldata'.$number);
        $root = $doc->appendChild($root);

        if (is_array($other) || is_array($params))
        {
            if($other!="")
            {   
                foreach($other as $k => $v)     
                {
                      $child = $doc->createElement($k);
                      $child = $root->appendChild($child);
                      $value = $doc->createTextNode($v);
                      $value = $child->appendChild($value);                         
                }
            }

            foreach($params as $ParamKey => $ParamValue)        
            {
                $child = $doc->createElement($ParamKey);
                $child = $root->appendChild($child);
                $value = $doc->createTextNode($ParamValue);
                $value = $child->appendChild($value);                           
            }           

        }   

        if (sizeof($arrData)>0)
        {
            for ($i=0;$i<sizeof($arrData);$i++) {

                $occ = $doc->createElement("datarow");
                $occ = $root->appendChild($occ);
                foreach ($arrData[$i] as $fieldname => $fieldvalue) {

                $child = $doc->createElement($fieldname);
                $child = $occ->appendChild($child);


                $value = $doc->createTextNode($fieldvalue);
                $value = $child->appendChild($value);
              } 
            } 
        }

        $xml = $doc->saveXML();

        $handle = fopen($XsltFileName, "r");

        $xsl = fread($handle, filesize($XsltFileName)) or die();

        fclose($handle);    

        $xmldoc = DOMDocument::loadXML($xml);
        $xsldoc = DOMDocument::loadXML($xsl);

        $proc = new XSLTProcessor() ;
        $proc->registerPHPFunctions();
        $proc->importStyleSheet($xsldoc);

        $output=$proc->transformToXML($xmldoc);         
        return $output; 
    }   
4

1 回答 1

0

loadXML不是静态函数,所以像这样称呼它DOMDocument::loadXML是错误的。你应该使用 dom 实例:$doc->loadXML($xml);

除此之外,您的代码很好并且可以满足它的需求 - 我没有测试 xsl 的东西,因为我不知道应该包含的内容$XsltFileName

于 2013-04-21T14:29:57.750 回答