2014-05-16 2 views
0

레코드 집합을 xml로 내 보낸 다음 xslt 변환을 통해 xliff로 내보내는 중입니다. 내보내기가 잘 작동하지만 내보내기 파일에서 일부 문자를 변환하지 못했습니다. 여기에 단계별 세부 사항이 나와 있습니다.php XSLTProcessor를 사용하는 XML/XSLT 출력 인코딩 문제

1 단계. 문자열 다음 Autocomplete On' see the wrong character ==> í

MySQL의 DB/테이블 필드 인코딩은 상기 텍스트 저장 UTF8 예컨대

`unicode longtext COLLATE utf8_unicode_ci` 

설정된다. 사용하여 XSLT 변환 :

(만 붙여

단계 2. HTML 코드는

<?xml version="1.0" standalone="yes"?> 
    <html version="1.2"><body><table><tr><td id="Autocomplete_On"> 
     Autocomplete On' see the wrong character ==&gt; &#xC3;&#xAD;</td> 
</tr></table></body></html> 

4 단계 XML로 수출 목적의 예

<html version="1.2"> 
    <table> 
     <tr> 
      <td id="Autocomplete_On">Autocomplete On' see the wrong character ==&#62; í</td> 
     </tr> 
    </table> 
    </html> 

3 단계 변환 생성됩니다 브라우저에서 보았을 때 출력의 원하는 부분을 보았습니다. 실제 문자는 Ã입니다.

01 23,516,
<body> 
     <group id="id796986axmarkhtml-0"> 
     <group id="id533787bxmarkbody-1"> 
      <group id="id533788bxmarktable-2"> 
      <group id="id533790bxmarktr-3"> 
       <trans-unit id="td-4"> 
       <source>Autocomplete On' see the wrong character ==&gt; í</source> 
       <target>Autocomplete On' see the wrong character ==&gt; í</target> 
       </trans-unit> 
      </group> 
      </group> 
     </group> 
     </group> 
    </body> 

실제 코드 :

private function xml2xliff($htmlStr,$source,$target){ 
     $xml=new \DOMDocument(); 
     //hacky way to tidy html 
     @$xml->loadHTML($htmlStr);//step 3 
     $xsl = new \DOMDocument; 
     $xsl->load(__DIR__.'/xliff/xsl/xml2xliff.xsl'); 
     $proc = new \XSLTProcessor(); 
     $proc->ImportStyleSheet($xsl); 
     $proc->setParameter('', 'source', $this->getIsoName($source)); 
     $proc->setParameter('', 'target', $this->getIsoName($target)); 
     return $proc->transformToXML($xml); //step 4 
    } 

$ htmlStr 2 단계에서 생성 된 HTML 코드 조각이다,

그래서 문제는 문자열이 두 번 변환되는 것입니다. 고려 실제 문자는 í

단계 3. 4. í

또 다른 예로 변환, 즉 &#xC3;&#xAD;

단계 A를 변환 여전히

1 단계 í

2 단계입니다 :

입력. Autocomplete On They’re gone now

xml 출력. Autocomplete On Theyâre gone now

답변

0

DOMDocument :: loadHtml()은 HTML을 ANSI로로드하지만 UTF-8입니다. 그래서 특수 캐릭터가 쪼개져 파괴되었습니다. 당신은 XML 처리 명령으로 UTF-8을 사용로를 속일 수 있습니다

$html = <<<HTML 
<html> 
    <table> 
    <tr> 
     <td id="Autocomplete_On">Autocomplete On' see the wrong character ==&#62; í</td> 
    </tr> 
    </table> 
</html> 
HTML; 

$dom = new DOMDocument('1.0', 'UTF-8'); 

$dom->loadHTML('<?xml encoding="UTF-8"?>'.$html); 
var_dump(
    $dom->saveXml() 
); 

출력 : 문제를 해결

string(331) "<?xml version="1.0" standalone="yes"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
<?xml encoding="UTF-8"??> 
<html version="1.2"><body><table><tr><td id="Autocomplete_On">Autocomplete On' see the wrong character ==&gt; &#xED;</td>&#xD; 
    </tr></table></body></html> 
" 
+0

덕분에, 내가 다른 인코딩 예를 들어, 아포스트로피 문자로 무슨 일이 일어날 지 궁금 예를 들어'Autocomplete On On they they 're now now '에서. – sakhunzai

+0

왜 그것의 추가? '이중 인용 부호 – sakhunzai