XSL(변환)
XSL(변환)
XML 문서를 다른 XML이나 HTML로 변환해야 할 때 XSLT를 쓰죠. XSL 확장은 libxslt 라이브러리를 사용해서 XSLT 변환을 수행하는 XSL 표준을 구현해요. 스타일시트와 XML 문서를 결합해 원하는 형태의 출력을 만들어 내는 게 핵심이에요.
출처: XSL
본문
XSL 확장의 중심은 XSLTProcessor 클래스예요. 흐름은 간단해요. 스타일시트를 불러오고(importStylesheet), 변환을 실행하면 돼요.
$xslt = new XSLTProcessor();
$xslt->importStylesheet($xsl); // XSL 스타일시트 불러오기
$result = $xslt->transformToXml($xml); // XML 변환
XSLTProcessor 클래스
XSLTProcessor::__construct— 새 XSLTProcessor 객체를 생성해요XSLTProcessor::importStylesheet— 스타일시트를 불러와요XSLTProcessor::transformToXml— XML로 변환해요XSLTProcessor::transformToDoc— 문서 객체로 변환해요XSLTProcessor::transformToUri— URI(파일)로 변환해요XSLTProcessor::setParameter/getParameter/removeParameter— 스타일시트 매개변수의 값을 설정/조회/제거해요XSLTProcessor::registerPHPFunctions— XSLT 함수로 PHP 함수를 쓸 수 있게 해요XSLTProcessor::registerPHPFunctionNS— 네임스페이스가 지정된 XSLT 함수로 PHP 함수를 등록해요XSLTProcessor::hasExsltSupport— PHP가 EXSLT를 지원하는지 확인해요XSLTProcessor::setProfiling— 프로파일링 출력 파일을 설정해요XSLTProcessor::setSecurityPrefs/getSecurityPrefs— 보안 설정을 지정하고 조회해요
주의할 점
스타일시트 안에서 document() 함수를 쓸 때는 절대 경로가 필요해요. Windows 사용자라면 특히 주의해야 하는데, 백슬래시 대신 슬래시로 된 절대 경로를 써야 해요. 예를 들어 document('test.xml')이나 document('c:\temp\test.xml')은 동작하지 않고, document('c:/temp/test.xml')처럼 앞으로 슬래시를 사용해야 해요.
또한 스타일시트에 PHP 함수를 엮어 쓰려면 registerPHPFunctions()로 함수를 등록하고, 스타일시트에 xmlns:php="http://php.net/xsl" 네임스페이스 선언을 추가해야 해요.
더 알아보기
설치 요구 사항과 미리 정의된 상수는 Installing/Configuring과 Predefined Constants 문서를 확인해 보세요. collection.xml과 collection.xsl 예제 파일, libxml 오류 처리 함수를 이용한 오류 처리 방법은 Examples 문서에 있어요.