Lib Xslt Tutorial
libxslt Tutorial
John Fleck
ÀÌ°ÍÀº libxslt Tutorial ÀÇ 0.4 ÆÇÀÌ´Ù.
Copyright (c) 2001 John Fleck
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license can be found here.
¸ñÂ÷
¿ä¾à
libxslt ¶óÀ̺귯¸®¸¦ ÀÌ¿ëÇÑ XSLT º¯È¯À» ¼öÇàÇÏ¿© XML ÆÄÀÏÀ» HTML ·Î º¯È¯ÇÏ´Â °£´ÜÇÑ ÀÀ¿ëÇÁ·Î±×·¥À» ÀÛ¼ºÇÏ´Â °ÁÂ.
1. ¼Ò°³ ¶È®Àå ¸¶Å©¾÷ ¾ð¾î(XML: Extensible Markup Language)Àº ±¸Á¶ÈµÈ µ¥ÀÌÅ͸¦ ÅؽºÆ® ÇüÅ·Π±³È¯Çϱâ À§ÇÑ World Wide Web Consortium Ç¥ÁØÀÌ´Ù. ÀÌ°ÍÀÇ Àαâ´Â ±× º¸Æí¼º¿¡¼ ºñ·ÔÇÑ´Ù. ¾î¶² ÄÄÇ»Å͵çÁö ÅؽºÆ® ÆÄÀÏÀ» ÀÐÀ» ¼ö ÀÖ´Ù. Àû´çÇÑ µµ±¸·Î, ¾î¶² ÄÄÇ»Å͵çÁö ´Ù¸¥ ÄÄÇ»ÅÍÀÇ XML ÆÄÀÏÀ» ÀÐÀ» ¼ö ÀÖ´Ù.
ÀÌ µµ±¸µé °¡¿îµ¥ °¡Àå Áß¿äÇÑ °ÍÀÇ Çϳª·Î È®Àå ¸¶Å©¾÷ ¾ð¾î º¯È¯(XSLT: Extensible Stylesheet Language Transformations)À» µé ¼ö ÀÖ´Ù. XSLT ´Â ¼±¾ðÀûÀÎ ¾ð¾î·Î¼ XML ÆÄÀÏÀ» ½ºÅ¸ÀϽÃÆ®¸¦ ÀÌ¿ëÇØ ÀÓÀÇÀÇ ÅؽºÆ® Ãâ·ÂÀ¸·Î º¯È¯ÇÒ ¼ö ÀÖ°Ô ÇØÁØ´Ù. libxslt ´Â ¹Ù·Î ÀÌ·¯ÇÑ º¯È¯À» ¼öÇà ÇÏ´Â ÇÔ¼ö¸¦ Á¦°øÇÑ´Ù.
libxslt ´Â Daniel Veillard °¡ ±×³ð ÇÁ·ÎÁ§Æ®¸¦ À§ÇØ ÀÛ¼ºÇÑ ÀÚÀ¯ C ¾ð¾î ¶óÀ̺귯¸®·Î XSLT º¯È¯À» ÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇÒ ¼ö ÀÖµµ·Ï Çϱâ À§ÇØ ¸¸µé¾îÁ³´Ù.
ÁÖÀÇ: libxslt ´Â ±×³ð ÇÁ·ÎÁ§Æ®ÀÇ ÈÄ¿ø ÇÏ¿¡ ÀÛ¼ºµÇ¾úÀ¸³ª, ¾î¶² ±×³ð ¶óÀ̺귯¸®¿¡µµ ÀÇÁ¸ÇÏÁö ¾Ê´Â´Ù. ÀÌ °ÁÂÀÇ ¿¹Á¦¿¡µµ »ç¿ëÇÏÁö ¾Ê¾Ò´Ù.
ÀÌ °Á¿¡¼´Â XML ÆÄÀÏÀ» Àоîµé¿©, ½ºÅ¸ÀϽÃÆ®¸¦ Àû¿ëÇÏ°í ±× °á°ú·Î ³ª¿Â Ãâ·ÂÀ» ÀúÀåÇÏ´Â °£´ÜÇÑ ÇÁ·Î±×·¥À» º¸±â¸¦ µé¾î ¼³¸íÇÑ´Ù. ÀÌ°ÍÀº ´ç½ÅÀÌ ¸¸µé°íÀÚ ÇÏ´Â ÇÁ·Î±×·¥Àº ¾Æ´Ò °ÍÀÌ´Ù. xsltproc À̶ó´Â libxslt ¹À½¿¡ Æ÷ÇÔµÈ ÇÁ·Î±×·¥ÀÌ ¶È°°Àº ÀÏÀ» ÇÏ¸ç ´õ Æ°Æ°ÇÏ°í ¿ÏÀüÇÑ ±â´ÉÀ» ±¸ÇöÇÏ°í ÀÖ´Ù. °Á¸¦ À§ÇØ ÀÛ¼ºÇÏ´Â ÇÁ·Î±×·¥Àº xsltproc À» ¹þ°Ü³»¼ ÁÙÀÎ ÆÇÀ¸·Î libxslt ÀÇ ±â´ÉÀ» º¸¿©ÁÖ±â À§ÇØ °í¾ÈµÇ¾ú´Ù.
xsltproc ÀÇ ¿ÏÀüÇÑ ÄÚµå´Â libxslt ¹èÆ÷º»¿¡ Æ÷ÇÔµÈ xsltproc.c ¿¡ ÀÖ´Ù. ±×°ÍÀº ¶ÇÇÑ À¥»ó¿¡¼µµ º¼ ¼ö ÀÖ´Ù.
Âü°í¹®Çå:
2. ±âº» ÇÔ¼ö ¶XML ÆÄÀÏÀ» º¯È¯Çϱâ À§Çؼ´Â, ¼¼ °¡Áö ÇÔ¼ö¸¦ ¼öÇàÇØ¾ß ÇÑ´Ù:
2.1. ±¸¹®ºÐ¼® ÁغñÇϱ⠶ÀÔ·Â ÆÄÀÏÀ̳ª ½ºÅ¸ÀϽÃÆ®¸¦ ºÐ¼®Çϱâ Àü¿¡, ¿£Æ¼Æ¼¸¦ ´Ù·ç±â À§ÇØ ¼³Á¤ÇØ¾ß ÇÏ´Â ÀÏ·ÃÀÇ ÀýÂ÷°¡ ÀÖ´Ù. ÀÌ ÀýÂ÷´Â libxslt °íÀ¯ÀÇ °ÍÀÌ ¾Æ´Ï´Ù. XML ÆÄÀÏÀ» ±¸¹®ºÐ¼®ÇÏ´Â ¾î´ø libxml2 ÇÁ·Î±×·¥À̵çÁö ºñ½ÁÇÑ ÀýÂ÷¸¦ °ÅÃÄ¾ß ÇÒ °ÍÀÌ´Ù.
ù°, ÀÏ»óÀûÀÎ libxml ·çƾÀ» È£ÃâÇØ¾ß ÇÑ´Ù. xmlSubstituteEntitiesDefault ÇÔ¼ö¿¡, Á¤¼ö°ª 1 À» ³Ñ°Ü¼ libxml2 ±¸¹®ºÐ¼®±â¿¡°Ô ÀÔ·Â ÆÄÀÏÀ» ±¸¹®ºÐ¼® ÇÏ´Â µ¿¾È ¿£Æ¼Æ¼ ÁöȯÀ» Ç϶ó°í ¾Ë·ÁÁà¾ß ÇÑ´Ù. (0 À» ³Ñ±â¸é libxml2 ¿¡¼ ¿£Æ¼Æ¼ ġȯÀÌ ÀϾÁö ¾Ê´Â´Ù.)
µÑ°, xmlLoadExtDtdDefaultValue ÀÌ 1ÀÇ °ªÀ» °®µµ·Ï ÇØ¾ß ÇÑ´Ù. ÀÌ°ÍÀº libxml ÀÌ ¿ÜºÎ ¿£Æ¼Æ¼ ºÎºÐÀ» ºÒ·¯¿Àµµ·Ï ¼³Á¤ÇÑ´Ù. ÀÌ°ÍÀ» ÇÏÁö ¾ÊÀ¸¸é ÀÔ·Â ÆÄÀÏÀÌ ¿£Æ¼Æ¼ ÀϺθ¦ ¿ÜºÎ¿¡¼ ºÒ·¯¿Àµµ·Ï µÇ¾î ÀÖ´Ù¸é, ¿¡·¯°¡ ³¯ °ÍÀÌ´Ù.
2.2. ½ºÅ¸ÀϽÃÆ® ±¸¹® ºÐ¼® ¶½ºÅ¸ÀϽÃÆ® ±¸¹® ºÐ¼®Àº ÇϳªÀÇ ÇÔ¼ö È£Ãâ·Î ÀÌ·¯¿ìÁö¸ç, ÀÌ ÇÔ¼ö´Â xmlChar ²ÃÀÇ ÀÎÀÚ¸¦ Çϳª ¹Þ´Â´Ù:
cur = xsltParseStylesheetFile((const xmlChar *)argv[i]);
¿©±â¼, ¸í·ÉÁÙ ÀÎÀÚ·Î ³Ñ°Ü¹ÞÀº ½ºÅ¸ÀϽÃÆ® ÆÄÀÏ À̸§À» to xmlChar ·Î Çüº¯È¯Çß´Ù. ÇÔ¼ö°¡ µ¹·ÁÁÖ´Â °ªÀº xsltStylesheetPtr ²Ã·Î¼ ½ºÅ¸ÀϽÃÆ® ³ª¹«¸¦ ºñ·ÔÇÑ ½ºÅ¸ÀϽÃÆ®¿¡ ´ëÇÑ Á¤º¸¸¦ °¡Áø ¸Þ¸ð¸®»óÀÇ ±¸Á¶Ã¼ÀÌ´Ù. ±×°ÍÀ» Á÷Á¢ Á¶ÀÛÇÒ ¼öµµ ÀÖÁö¸¸ ÀÌ º¸±â¿¡¼´Â ±×·² ÇÊ¿ä°¡ ¾ø´Ù.
2.3. ÀÔ·Â ÆÄÀÏ ±¸¹® ºÐ¼® ¶ÀÔ·Â ÆÄÀÏ ±¸¹® ºÐ¼®Àº ÇϳªÀÇ ÇÔ¼ö È£Ãâ·Î ÀÌ·ç¾îÁø´Ù:
doc = xmlParseFile(argv[i]);
ÇÔ¼ö°¡ µ¹·ÁÁÖ´Â °ªÀº xmlDocPtr ¶ó´Â ¹®¼¿¡ ´ëÇÑ Á¤º¸¸¦ ´ã°í ÀÖ´Â ¸Þ¸ð¸®»óÀÇ ±¸Á¶Ã¼ÀÌ´Ù. ±×°ÍÀ» Á÷Á¢ Á¶ÀÛÇÒ ¼öµµ ÀÖÁö¸¸ ÀÌ º¸±â¿¡¼´Â ±×·² ÇÊ¿ä°¡ ¾ø´Ù.
2.4. ½ºÅ¸ÀϽÃÆ® Àû¿ëÇϱ⠶ÀÌÁ¦ ¸Þ¸ð¸®¿¡´Â ¹®¼¿Í ½ºÅ¸ÀϽÃÆ®¸¦ ³ªÅ¸³»´Â Æ®¸® Á¤º¸°¡ ÀúÀåµÇ¾î ÀÖ°í, xsltApplyStylesheet ÇÔ¼ö¸¦ »ç¿ëÇؼ ¹®¼¿¡ ½ºÅ¸ÀϽÃÆ®¸¦ Àû¿ëÇÑ´Ù:
res = xsltApplyStylesheet(cur, doc, params);
ÀÌ ÇÔ¼ö´Â ¾ÕÀÇ µÎ ÇÔ¼ö °á°ú°ªÀÎ xsltStylesheetPtr ¿Í xmlDocPtr ÀÎÀÚ¸¦ ¹Þ´Â´Ù, ¼¼¹øÂÅ ÀÎÀÚ params ´Â XSLT º¸Á¶ º¯¼ö¸¦ ½ºÅ¸ÀϽÃÆ®¿¡ ³Ñ±â´Â µ¥ ¾µ ¼ö ÀÖ´Â
NULL ·Î ³¡³ª´Â À̸§/°ª ½ÖÀÇ »ó¼ö ¹®ÀÚ¿ ¹è¿ÀÌ´Ù.
2.5. °á°ú ÀúÀåÇϱ⠶libxslt ´Â °á°ú Ãâ·ÂÀ» ÀúÀåÇϱâ À§ÇÑ ÀÏ·ÃÀÇ ÇÔ¼öµéÀ» Æ÷ÇÔÇÑ´Ù. ÀÌ º¸±â¿¡¼´Â, xsltSaveResultToFile ÀÌ ¾²¿´À¸¸ç, °á°ú¸¦
stdout ¿¡ ÀúÀåÇÑ´Ù:
xsltSaveResultToFile(stdout, res, cur);
ÁÖÀÇ: libxml ¿¡µµ xmlSaveFile °ú °°Àº Ãâ·Â ÇÔ¼ö°¡ ÀÖ¾î ÀÌ·² ¶§ ¾µ ¼ö ÀÖ´Ù. ±×·¯³ª libxslt ÀúÀå ÇÔ¼ö Áß Çϳª¸¦ ¾²Áö ¾ÊÀ» °æ¿ì ½ºÅ¸ÀϽÃÆ®¿¡ ÀÖ´Â Ãâ·Â°ú °ü°èµÃ Á¤º¸µé, À̸¦Å×¸é ¾î¶² ÀÎÄÚµùÀ» ¾µÁö ¼±¾ðÇÏ´Â °Í µîÀ» ÀÒ°Ô µÈ´Ù.
2.6. º¸Á¶º¯¼ö ¶XSLT ¿¡¼´Â º¸Á¶º¯¼ö¸¦ ÅëÇØ Ãß°¡ÀûÀÎ Á¤º¸¸¦ ½ºÅ¸ÀϽÃÆ®¿¡ ³Ñ±æ ¼ö ÀÖ´Ù. libxslt ´Â XSLT º¸Á¶º¯¼ö¸¦ xsltApplyStylesheet¿¡ ³Ñ±ä °ªµé Áß¿¡ ÇϳªÃ³·³ ¹Þ´Â´Ù.
°ÁÂÀÇ º¸±â¿Í ÀÌ °Á º¸±âÀÇ ±â¹ÝÀÌ µÇ´Â xsltproc ¿¡¼´Â ³Ñ±â´Â º¸Á¶º¯¼ö°¡ À̸§/°ª ½ÖÀÇ ÇüŸ¦ °®´Â´Ù. ÀÌ ÇÁ·Î±×·¥Àº ±×°ÍÀ» ¸í·ÉÁÙ ÀÎÀڷκÎÅÍ ¼öÁýÇÏ¿© ±×°ÍÀ» ¹è¿ params ¿¡ »ðÀÔÇÑ µÚ ÇÔ¼ö·Î ³Ñ±ä´Ù. ¹è¿ÀÇ ¸¶Áö¸· ¿ø¼Ò´Â NULL °ªÀ» °®´Â´Ù.
ÁÖÀÇ: ³Ñ±â´Â ÀÎÀÚ°¡ ³ëµå°¡ ¾Æ´Ñ ¹®ÀÚ¿ÀÏ ¶§, ¹Ýµå½Ã escaped ¾ß ÇÑ´Ù. ÀÌ °ÁÂÀÇ ÇÁ·Î±×·¥¿¡¼´Â, ´ÙÀ½°ú °°´Ù:
tutorial]$ ./libxslt_tutorial --param rootid "'asect1'" stylesheet.xsl filename.xml
2.7. û¼Ò ¶³¡¸¶Ä£ µÚ libxslt ¿Í libxml ¿¡¼´Â ¸Þ¸ð¸® ÇØÁ¦¸¦ À§ÇÑ ÇÔ¼ö¸¦ Á¦°øÇÑ´Ù.
1: xsltFreeStylesheet(cur);
2: xmlFreeDoc(res); 3: xmlFreeDoc(doc); 4: xsltCleanupGlobals(); 5: xmlCleanupParser();
3. ÄÚµå ¶libxslt_tutorial.c
/*
* libxslt_tutorial.c: demo program for the XSL Transformation 1.0 engine * * based on xsltproc.c, by Daniel.Veillard@imag.fr * by John Fleck * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA. * */ #include <string.h> #include <libxml/xmlmemory.h> #include <libxml/debugXML.h> #include <libxml/HTMLtree.h> #include <libxml/xmlIO.h> #include <libxml/DOCBparser.h> #include <libxml/xinclude.h> #include <libxml/catalog.h> #include <libxslt/xslt.h> #include <libxslt/xsltInternals.h> #include <libxslt/transform.h> #include <libxslt/xsltutils.h> extern int xmlLoadExtDtdDefaultValue; static void usage(const char *name) { printf("Usage: %s [options] stylesheet file [file ...]\n", name); printf(" --param name value : pass a (parameter,value) pair\n"); } int main(int argc, char **argv) { int i; const char *params[16 + 1]; int nbparams = 0; xsltStylesheetPtr cur = NULL; xmlDocPtr doc, res; if (argc <= 1) { usage(argv[0]); return(1); } for (i = 1; i < argc; i++) { if (argv[i][0] != '-') break; if ((!strcmp(argv[i], "-param")) || (!strcmp(argv[i], "--param"))) { i++; params[nbparams++] = argv[i++]; params[nbparams++] = argv[i]; if (nbparams >= 16) { fprintf(stderr, "too many params\n"); return (1); } } else { fprintf(stderr, "Unknown option %s\n", argv[i]); usage(argv[0]); return (1); } } params[nbparams] = NULL; xmlSubstituteEntitiesDefault(1); xmlLoadExtDtdDefaultValue = 1; cur = xsltParseStylesheetFile((const xmlChar *)argv[i]); i++; doc = xmlParseFile(argv[i]); res = xsltApplyStylesheet(cur, doc, params); xsltSaveResultToFile(stdout, res, cur); xsltFreeStylesheet(cur); xmlFreeDoc(res); xmlFreeDoc(doc); xsltCleanupGlobals(); xmlCleanupParser(); return(0); } |
You have had a long-term stimulation relative to business. |