[c++] libxslt와 소스 코드 예제

libxslt는 XML과 관련된 데이터를 가공하고 변환하기 위한 라이브러리입니다. 이 라이브러리를 사용하여 XML 문서를 다른 형식으로 변환하고 원하는 형태로 가공할 수 있습니다.

libxslt 라이브러리 설치

libxslt 라이브러리를 설치하려면 아래와 같이 명령을 실행합니다.

sudo apt-get install libxslt-dev

libxslt 라이브러리를 사용한 예제 코드

아래는 libxslt 라이브러리를 사용하여 XML 문서를 변환하는 간단한 C++ 예제 코드입니다.

#include <libxslt/xslt.h>
#include <libxslt/transform.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

int main() {
    xmlDocPtr doc, res;
    xsltStylesheetPtr cur;
    const char* params[1] = { NULL };

    xmlInitParser();
    cur = xsltParseStylesheetFile((const xmlChar*)"style.xsl");
    doc = xmlParseFile("input.xml");
    res = xsltApplyStylesheet(cur, doc, params);
    xsltSaveResultToFile(stdout, res, cur);

    xsltFreeStylesheet(cur);
    xmlFreeDoc(res);
    xmlFreeDoc(doc);
    xsltCleanupGlobals();
    xmlCleanupParser();

    return 0;
}

이 코드는 입력으로 주어진 input.xmlstyle.xsl에 정의된 스타일시트를 사용하여 변환하고 결과를 표준 출력에 기록합니다.

libxslt에 대한 자세한 내용은 공식 문서를 참고하시기 바랍니다.

참고 자료