transform.c 183 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525
  1. /*
  2. * transform.c: Implementation of the XSL Transformation 1.0 engine
  3. * transform part, i.e. applying a Stylesheet to a document
  4. *
  5. * References:
  6. * http://www.w3.org/TR/1999/REC-xslt-19991116
  7. *
  8. * Michael Kay "XSLT Programmer's Reference" pp 637-643
  9. * Writing Multiple Output Files
  10. *
  11. * XSLT-1.1 Working Draft
  12. * http://www.w3.org/TR/xslt11#multiple-output
  13. *
  14. * See Copyright for the status of this software.
  15. *
  16. * daniel@veillard.com
  17. */
  18. #define IN_LIBXSLT
  19. #include "libxslt.h"
  20. #include <limits.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <stddef.h>
  24. #include <libxml/xmlmemory.h>
  25. #include <libxml/parser.h>
  26. #include <libxml/tree.h>
  27. #include <libxml/valid.h>
  28. #include <libxml/hash.h>
  29. #include <libxml/encoding.h>
  30. #include <libxml/xmlerror.h>
  31. #include <libxml/xpath.h>
  32. #include <libxml/parserInternals.h>
  33. #include <libxml/xpathInternals.h>
  34. #include <libxml/HTMLtree.h>
  35. #include <libxml/debugXML.h>
  36. #include <libxml/uri.h>
  37. #include "xslt.h"
  38. #include "xsltInternals.h"
  39. #include "xsltutils.h"
  40. #include "pattern.h"
  41. #include "transform.h"
  42. #include "variables.h"
  43. #include "numbersInternals.h"
  44. #include "namespaces.h"
  45. #include "attributes.h"
  46. #include "templates.h"
  47. #include "imports.h"
  48. #include "keys.h"
  49. #include "documents.h"
  50. #include "extensions.h"
  51. #include "extra.h"
  52. #include "preproc.h"
  53. #include "security.h"
  54. #ifdef WITH_XSLT_DEBUG
  55. #define WITH_XSLT_DEBUG_EXTRA
  56. #define WITH_XSLT_DEBUG_PROCESS
  57. #define WITH_XSLT_DEBUG_VARIABLE
  58. #endif
  59. #define XSLT_GENERATE_HTML_DOCTYPE
  60. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  61. static int xsltGetHTMLIDs(const xmlChar *version, const xmlChar **publicID,
  62. const xmlChar **systemID);
  63. #endif
  64. int xsltMaxDepth = 3000;
  65. int xsltMaxVars = 15000;
  66. /*
  67. * Useful macros
  68. */
  69. #ifndef FALSE
  70. # define FALSE (0 == 1)
  71. # define TRUE (!FALSE)
  72. #endif
  73. #define IS_BLANK_NODE(n) \
  74. (((n)->type == XML_TEXT_NODE) && (xsltIsBlank((n)->content)))
  75. /*
  76. * Forward declarations
  77. */
  78. static xmlNsPtr
  79. xsltCopyNamespaceListInternal(xmlNodePtr node, xmlNsPtr cur);
  80. static xmlNodePtr
  81. xsltCopyTree(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
  82. xmlNodePtr node, xmlNodePtr insert, int isLRE,
  83. int topElemVisited);
  84. static void
  85. xsltApplySequenceConstructor(xsltTransformContextPtr ctxt,
  86. xmlNodePtr contextNode, xmlNodePtr list,
  87. xsltTemplatePtr templ);
  88. static void
  89. xsltApplyXSLTTemplate(xsltTransformContextPtr ctxt,
  90. xmlNodePtr contextNode,
  91. xmlNodePtr list,
  92. xsltTemplatePtr templ,
  93. xsltStackElemPtr withParams);
  94. /**
  95. * templPush:
  96. * @ctxt: the transformation context
  97. * @value: the template to push on the stack
  98. *
  99. * Push a template on the stack
  100. *
  101. * Returns the new index in the stack or 0 in case of error
  102. */
  103. static int
  104. templPush(xsltTransformContextPtr ctxt, xsltTemplatePtr value)
  105. {
  106. if (ctxt->templMax == 0) {
  107. ctxt->templMax = 4;
  108. ctxt->templTab =
  109. (xsltTemplatePtr *) xmlMalloc(ctxt->templMax *
  110. sizeof(ctxt->templTab[0]));
  111. if (ctxt->templTab == NULL) {
  112. xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
  113. return (0);
  114. }
  115. }
  116. else if (ctxt->templNr >= ctxt->templMax) {
  117. ctxt->templMax *= 2;
  118. ctxt->templTab =
  119. (xsltTemplatePtr *) xmlRealloc(ctxt->templTab,
  120. ctxt->templMax *
  121. sizeof(ctxt->templTab[0]));
  122. if (ctxt->templTab == NULL) {
  123. xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
  124. return (0);
  125. }
  126. }
  127. ctxt->templTab[ctxt->templNr] = value;
  128. ctxt->templ = value;
  129. return (ctxt->templNr++);
  130. }
  131. /**
  132. * templPop:
  133. * @ctxt: the transformation context
  134. *
  135. * Pop a template value from the stack
  136. *
  137. * Returns the stored template value
  138. */
  139. static xsltTemplatePtr
  140. templPop(xsltTransformContextPtr ctxt)
  141. {
  142. xsltTemplatePtr ret;
  143. if (ctxt->templNr <= 0)
  144. return (0);
  145. ctxt->templNr--;
  146. if (ctxt->templNr > 0)
  147. ctxt->templ = ctxt->templTab[ctxt->templNr - 1];
  148. else
  149. ctxt->templ = (xsltTemplatePtr) 0;
  150. ret = ctxt->templTab[ctxt->templNr];
  151. ctxt->templTab[ctxt->templNr] = 0;
  152. return (ret);
  153. }
  154. /**
  155. * xsltLocalVariablePop:
  156. * @ctxt: the transformation context
  157. * @limitNr: number of variables which should remain
  158. * @level: the depth in the xsl:template's tree
  159. *
  160. * Pops all variable values at the given @depth from the stack.
  161. *
  162. * Returns the stored variable value
  163. * **NOTE:**
  164. * This is an internal routine and should not be called by users!
  165. */
  166. void
  167. xsltLocalVariablePop(xsltTransformContextPtr ctxt, int limitNr, int level)
  168. {
  169. xsltStackElemPtr variable;
  170. if (ctxt->varsNr <= 0)
  171. return;
  172. do {
  173. if (ctxt->varsNr <= limitNr)
  174. break;
  175. variable = ctxt->varsTab[ctxt->varsNr - 1];
  176. if (variable->level <= level)
  177. break;
  178. if (variable->level >= 0)
  179. xsltFreeStackElemList(variable);
  180. ctxt->varsNr--;
  181. } while (ctxt->varsNr != 0);
  182. if (ctxt->varsNr > 0)
  183. ctxt->vars = ctxt->varsTab[ctxt->varsNr - 1];
  184. else
  185. ctxt->vars = NULL;
  186. }
  187. /**
  188. * xsltTemplateParamsCleanup:
  189. *
  190. * Removes xsl:param and xsl:with-param items from the
  191. * variable-stack. Only xsl:with-param items are not freed.
  192. */
  193. static void
  194. xsltTemplateParamsCleanup(xsltTransformContextPtr ctxt)
  195. {
  196. xsltStackElemPtr param;
  197. for (; ctxt->varsNr > ctxt->varsBase; ctxt->varsNr--) {
  198. param = ctxt->varsTab[ctxt->varsNr -1];
  199. /*
  200. * Free xsl:param items.
  201. * xsl:with-param items will have a level of -1 or -2.
  202. */
  203. if (param->level >= 0) {
  204. xsltFreeStackElemList(param);
  205. }
  206. }
  207. if (ctxt->varsNr > 0)
  208. ctxt->vars = ctxt->varsTab[ctxt->varsNr - 1];
  209. else
  210. ctxt->vars = NULL;
  211. }
  212. #ifdef WITH_PROFILER
  213. /**
  214. * profPush:
  215. * @ctxt: the transformation context
  216. * @value: the profiling value to push on the stack
  217. *
  218. * Push a profiling value on the stack
  219. *
  220. * Returns the new index in the stack or 0 in case of error
  221. */
  222. static int
  223. profPush(xsltTransformContextPtr ctxt, long value)
  224. {
  225. if (ctxt->profMax == 0) {
  226. ctxt->profMax = 4;
  227. ctxt->profTab =
  228. (long *) xmlMalloc(ctxt->profMax * sizeof(ctxt->profTab[0]));
  229. if (ctxt->profTab == NULL) {
  230. xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
  231. return (0);
  232. }
  233. }
  234. else if (ctxt->profNr >= ctxt->profMax) {
  235. ctxt->profMax *= 2;
  236. ctxt->profTab =
  237. (long *) xmlRealloc(ctxt->profTab,
  238. ctxt->profMax * sizeof(ctxt->profTab[0]));
  239. if (ctxt->profTab == NULL) {
  240. xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
  241. return (0);
  242. }
  243. }
  244. ctxt->profTab[ctxt->profNr] = value;
  245. ctxt->prof = value;
  246. return (ctxt->profNr++);
  247. }
  248. /**
  249. * profPop:
  250. * @ctxt: the transformation context
  251. *
  252. * Pop a profiling value from the stack
  253. *
  254. * Returns the stored profiling value
  255. */
  256. static long
  257. profPop(xsltTransformContextPtr ctxt)
  258. {
  259. long ret;
  260. if (ctxt->profNr <= 0)
  261. return (0);
  262. ctxt->profNr--;
  263. if (ctxt->profNr > 0)
  264. ctxt->prof = ctxt->profTab[ctxt->profNr - 1];
  265. else
  266. ctxt->prof = (long) 0;
  267. ret = ctxt->profTab[ctxt->profNr];
  268. ctxt->profTab[ctxt->profNr] = 0;
  269. return (ret);
  270. }
  271. static void
  272. profCallgraphAdd(xsltTemplatePtr templ, xsltTemplatePtr parent)
  273. {
  274. int i;
  275. if (templ->templMax == 0) {
  276. templ->templMax = 4;
  277. templ->templCalledTab =
  278. (xsltTemplatePtr *) xmlMalloc(templ->templMax *
  279. sizeof(templ->templCalledTab[0]));
  280. templ->templCountTab =
  281. (int *) xmlMalloc(templ->templMax *
  282. sizeof(templ->templCountTab[0]));
  283. if (templ->templCalledTab == NULL || templ->templCountTab == NULL) {
  284. xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
  285. return;
  286. }
  287. }
  288. else if (templ->templNr >= templ->templMax) {
  289. templ->templMax *= 2;
  290. templ->templCalledTab =
  291. (xsltTemplatePtr *) xmlRealloc(templ->templCalledTab,
  292. templ->templMax *
  293. sizeof(templ->templCalledTab[0]));
  294. templ->templCountTab =
  295. (int *) xmlRealloc(templ->templCountTab,
  296. templ->templMax *
  297. sizeof(templ->templCountTab[0]));
  298. if (templ->templCalledTab == NULL || templ->templCountTab == NULL) {
  299. xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
  300. return;
  301. }
  302. }
  303. for (i = 0; i < templ->templNr; i++) {
  304. if (templ->templCalledTab[i] == parent) {
  305. templ->templCountTab[i]++;
  306. break;
  307. }
  308. }
  309. if (i == templ->templNr) {
  310. /* not found, add new one */
  311. templ->templCalledTab[templ->templNr] = parent;
  312. templ->templCountTab[templ->templNr] = 1;
  313. templ->templNr++;
  314. }
  315. }
  316. #endif /* WITH_PROFILER */
  317. /**
  318. * xsltPreCompEval:
  319. * @ctxt: transform context
  320. * @node: context node
  321. * @comp: precompiled expression
  322. *
  323. * Evaluate a precompiled XPath expression.
  324. */
  325. static xmlXPathObjectPtr
  326. xsltPreCompEval(xsltTransformContextPtr ctxt, xmlNodePtr node,
  327. xsltStylePreCompPtr comp) {
  328. xmlXPathObjectPtr res;
  329. xmlXPathContextPtr xpctxt;
  330. xmlNodePtr oldXPContextNode;
  331. xmlNsPtr *oldXPNamespaces;
  332. int oldXPProximityPosition, oldXPContextSize, oldXPNsNr;
  333. xpctxt = ctxt->xpathCtxt;
  334. oldXPContextNode = xpctxt->node;
  335. oldXPProximityPosition = xpctxt->proximityPosition;
  336. oldXPContextSize = xpctxt->contextSize;
  337. oldXPNsNr = xpctxt->nsNr;
  338. oldXPNamespaces = xpctxt->namespaces;
  339. xpctxt->node = node;
  340. #ifdef XSLT_REFACTORED
  341. if (comp->inScopeNs != NULL) {
  342. xpctxt->namespaces = comp->inScopeNs->list;
  343. xpctxt->nsNr = comp->inScopeNs->xpathNumber;
  344. } else {
  345. xpctxt->namespaces = NULL;
  346. xpctxt->nsNr = 0;
  347. }
  348. #else
  349. xpctxt->namespaces = comp->nsList;
  350. xpctxt->nsNr = comp->nsNr;
  351. #endif
  352. res = xmlXPathCompiledEval(comp->comp, xpctxt);
  353. xpctxt->node = oldXPContextNode;
  354. xpctxt->proximityPosition = oldXPProximityPosition;
  355. xpctxt->contextSize = oldXPContextSize;
  356. xpctxt->nsNr = oldXPNsNr;
  357. xpctxt->namespaces = oldXPNamespaces;
  358. return(res);
  359. }
  360. /**
  361. * xsltPreCompEvalToBoolean:
  362. * @ctxt: transform context
  363. * @node: context node
  364. * @comp: precompiled expression
  365. *
  366. * Evaluate a precompiled XPath expression as boolean.
  367. */
  368. static int
  369. xsltPreCompEvalToBoolean(xsltTransformContextPtr ctxt, xmlNodePtr node,
  370. xsltStylePreCompPtr comp) {
  371. int res;
  372. xmlXPathContextPtr xpctxt;
  373. xmlNodePtr oldXPContextNode;
  374. xmlNsPtr *oldXPNamespaces;
  375. int oldXPProximityPosition, oldXPContextSize, oldXPNsNr;
  376. xpctxt = ctxt->xpathCtxt;
  377. oldXPContextNode = xpctxt->node;
  378. oldXPProximityPosition = xpctxt->proximityPosition;
  379. oldXPContextSize = xpctxt->contextSize;
  380. oldXPNsNr = xpctxt->nsNr;
  381. oldXPNamespaces = xpctxt->namespaces;
  382. xpctxt->node = node;
  383. #ifdef XSLT_REFACTORED
  384. if (comp->inScopeNs != NULL) {
  385. xpctxt->namespaces = comp->inScopeNs->list;
  386. xpctxt->nsNr = comp->inScopeNs->xpathNumber;
  387. } else {
  388. xpctxt->namespaces = NULL;
  389. xpctxt->nsNr = 0;
  390. }
  391. #else
  392. xpctxt->namespaces = comp->nsList;
  393. xpctxt->nsNr = comp->nsNr;
  394. #endif
  395. res = xmlXPathCompiledEvalToBoolean(comp->comp, xpctxt);
  396. xpctxt->node = oldXPContextNode;
  397. xpctxt->proximityPosition = oldXPProximityPosition;
  398. xpctxt->contextSize = oldXPContextSize;
  399. xpctxt->nsNr = oldXPNsNr;
  400. xpctxt->namespaces = oldXPNamespaces;
  401. return(res);
  402. }
  403. /************************************************************************
  404. * *
  405. * XInclude default settings *
  406. * *
  407. ************************************************************************/
  408. static int xsltDoXIncludeDefault = 0;
  409. /**
  410. * xsltSetXIncludeDefault:
  411. * @xinclude: whether to do XInclude processing
  412. *
  413. * Set whether XInclude should be processed on document being loaded by default
  414. */
  415. void
  416. xsltSetXIncludeDefault(int xinclude) {
  417. xsltDoXIncludeDefault = (xinclude != 0);
  418. }
  419. /**
  420. * xsltGetXIncludeDefault:
  421. *
  422. * Provides the default state for XInclude processing
  423. *
  424. * Returns 0 if there is no processing 1 otherwise
  425. */
  426. int
  427. xsltGetXIncludeDefault(void) {
  428. return(xsltDoXIncludeDefault);
  429. }
  430. static unsigned long xsltDefaultTrace = (unsigned long) XSLT_TRACE_ALL;
  431. /**
  432. * xsltDebugSetDefaultTrace:
  433. * @val: tracing level mask
  434. *
  435. * Set the default debug tracing level mask
  436. */
  437. void xsltDebugSetDefaultTrace(xsltDebugTraceCodes val) {
  438. xsltDefaultTrace = val;
  439. }
  440. /**
  441. * xsltDebugGetDefaultTrace:
  442. *
  443. * Get the current default debug tracing level mask
  444. *
  445. * Returns the current default debug tracing level mask
  446. */
  447. xsltDebugTraceCodes xsltDebugGetDefaultTrace() {
  448. return xsltDefaultTrace;
  449. }
  450. /************************************************************************
  451. * *
  452. * Handling of Transformation Contexts *
  453. * *
  454. ************************************************************************/
  455. static xsltTransformCachePtr
  456. xsltTransformCacheCreate(void)
  457. {
  458. xsltTransformCachePtr ret;
  459. ret = (xsltTransformCachePtr) xmlMalloc(sizeof(xsltTransformCache));
  460. if (ret == NULL) {
  461. xsltTransformError(NULL, NULL, NULL,
  462. "xsltTransformCacheCreate : malloc failed\n");
  463. return(NULL);
  464. }
  465. memset(ret, 0, sizeof(xsltTransformCache));
  466. return(ret);
  467. }
  468. static void
  469. xsltTransformCacheFree(xsltTransformCachePtr cache)
  470. {
  471. if (cache == NULL)
  472. return;
  473. /*
  474. * Free tree fragments.
  475. */
  476. if (cache->RVT) {
  477. xmlDocPtr tmp, cur = cache->RVT;
  478. while (cur) {
  479. tmp = cur;
  480. cur = (xmlDocPtr) cur->next;
  481. if (tmp->_private != NULL) {
  482. /*
  483. * Tree the document info.
  484. */
  485. xsltFreeDocumentKeys((xsltDocumentPtr) tmp->_private);
  486. xmlFree(tmp->_private);
  487. }
  488. xmlFreeDoc(tmp);
  489. }
  490. }
  491. /*
  492. * Free vars/params.
  493. */
  494. if (cache->stackItems) {
  495. xsltStackElemPtr tmp, cur = cache->stackItems;
  496. while (cur) {
  497. tmp = cur;
  498. cur = cur->next;
  499. /*
  500. * REVISIT TODO: Should be call a destruction-function
  501. * instead?
  502. */
  503. xmlFree(tmp);
  504. }
  505. }
  506. xmlFree(cache);
  507. }
  508. /**
  509. * xsltNewTransformContext:
  510. * @style: a parsed XSLT stylesheet
  511. * @doc: the input document
  512. *
  513. * Create a new XSLT TransformContext
  514. *
  515. * Returns the newly allocated xsltTransformContextPtr or NULL in case of error
  516. */
  517. xsltTransformContextPtr
  518. xsltNewTransformContext(xsltStylesheetPtr style, xmlDocPtr doc) {
  519. xsltTransformContextPtr cur;
  520. xsltDocumentPtr docu;
  521. int i;
  522. xsltInitGlobals();
  523. cur = (xsltTransformContextPtr) xmlMalloc(sizeof(xsltTransformContext));
  524. if (cur == NULL) {
  525. xsltTransformError(NULL, NULL, (xmlNodePtr)doc,
  526. "xsltNewTransformContext : malloc failed\n");
  527. return(NULL);
  528. }
  529. memset(cur, 0, sizeof(xsltTransformContext));
  530. cur->cache = xsltTransformCacheCreate();
  531. if (cur->cache == NULL)
  532. goto internal_err;
  533. /*
  534. * setup of the dictionary must be done early as some of the
  535. * processing later like key handling may need it.
  536. */
  537. cur->dict = xmlDictCreateSub(style->dict);
  538. cur->internalized = ((style->internalized) && (cur->dict != NULL));
  539. #ifdef WITH_XSLT_DEBUG
  540. xsltGenericDebug(xsltGenericDebugContext,
  541. "Creating sub-dictionary from stylesheet for transformation\n");
  542. #endif
  543. /*
  544. * initialize the template stack
  545. */
  546. cur->templTab = (xsltTemplatePtr *)
  547. xmlMalloc(10 * sizeof(xsltTemplatePtr));
  548. if (cur->templTab == NULL) {
  549. xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
  550. "xsltNewTransformContext: out of memory\n");
  551. goto internal_err;
  552. }
  553. cur->templNr = 0;
  554. cur->templMax = 5;
  555. cur->templ = NULL;
  556. cur->maxTemplateDepth = xsltMaxDepth;
  557. /*
  558. * initialize the variables stack
  559. */
  560. cur->varsTab = (xsltStackElemPtr *)
  561. xmlMalloc(10 * sizeof(xsltStackElemPtr));
  562. if (cur->varsTab == NULL) {
  563. xmlGenericError(xmlGenericErrorContext,
  564. "xsltNewTransformContext: out of memory\n");
  565. goto internal_err;
  566. }
  567. cur->varsNr = 0;
  568. cur->varsMax = 10;
  569. cur->vars = NULL;
  570. cur->varsBase = 0;
  571. cur->maxTemplateVars = xsltMaxVars;
  572. /*
  573. * the profiling stack is not initialized by default
  574. */
  575. cur->profTab = NULL;
  576. cur->profNr = 0;
  577. cur->profMax = 0;
  578. cur->prof = 0;
  579. cur->style = style;
  580. xmlXPathInit();
  581. cur->xpathCtxt = xmlXPathNewContext(doc);
  582. if (cur->xpathCtxt == NULL) {
  583. xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
  584. "xsltNewTransformContext : xmlXPathNewContext failed\n");
  585. goto internal_err;
  586. }
  587. /*
  588. * Create an XPath cache.
  589. */
  590. if (xmlXPathContextSetCache(cur->xpathCtxt, 1, -1, 0) == -1)
  591. goto internal_err;
  592. /*
  593. * Initialize the extras array
  594. */
  595. if (style->extrasNr != 0) {
  596. cur->extrasMax = style->extrasNr + 20;
  597. cur->extras = (xsltRuntimeExtraPtr)
  598. xmlMalloc(cur->extrasMax * sizeof(xsltRuntimeExtra));
  599. if (cur->extras == NULL) {
  600. xmlGenericError(xmlGenericErrorContext,
  601. "xsltNewTransformContext: out of memory\n");
  602. goto internal_err;
  603. }
  604. cur->extrasNr = style->extrasNr;
  605. for (i = 0;i < cur->extrasMax;i++) {
  606. cur->extras[i].info = NULL;
  607. cur->extras[i].deallocate = NULL;
  608. cur->extras[i].val.ptr = NULL;
  609. }
  610. } else {
  611. cur->extras = NULL;
  612. cur->extrasNr = 0;
  613. cur->extrasMax = 0;
  614. }
  615. XSLT_REGISTER_VARIABLE_LOOKUP(cur);
  616. XSLT_REGISTER_FUNCTION_LOOKUP(cur);
  617. cur->xpathCtxt->nsHash = style->nsHash;
  618. /*
  619. * Initialize the registered external modules
  620. */
  621. xsltInitCtxtExts(cur);
  622. /*
  623. * Setup document element ordering for later efficiencies
  624. * (bug 133289)
  625. */
  626. if (xslDebugStatus == XSLT_DEBUG_NONE)
  627. xmlXPathOrderDocElems(doc);
  628. /*
  629. * Must set parserOptions before calling xsltNewDocument
  630. * (bug 164530)
  631. */
  632. cur->parserOptions = XSLT_PARSE_OPTIONS;
  633. docu = xsltNewDocument(cur, doc);
  634. if (docu == NULL) {
  635. xsltTransformError(cur, NULL, (xmlNodePtr)doc,
  636. "xsltNewTransformContext : xsltNewDocument failed\n");
  637. goto internal_err;
  638. }
  639. docu->main = 1;
  640. cur->document = docu;
  641. cur->inst = NULL;
  642. cur->outputFile = NULL;
  643. cur->sec = xsltGetDefaultSecurityPrefs();
  644. cur->debugStatus = xslDebugStatus;
  645. cur->traceCode = (unsigned long*) &xsltDefaultTrace;
  646. cur->xinclude = xsltGetXIncludeDefault();
  647. cur->keyInitLevel = 0;
  648. return(cur);
  649. internal_err:
  650. if (cur != NULL)
  651. xsltFreeTransformContext(cur);
  652. return(NULL);
  653. }
  654. /**
  655. * xsltFreeTransformContext:
  656. * @ctxt: an XSLT parser context
  657. *
  658. * Free up the memory allocated by @ctxt
  659. */
  660. void
  661. xsltFreeTransformContext(xsltTransformContextPtr ctxt) {
  662. if (ctxt == NULL)
  663. return;
  664. /*
  665. * Shutdown the extension modules associated to the stylesheet
  666. * used if needed.
  667. */
  668. xsltShutdownCtxtExts(ctxt);
  669. if (ctxt->xpathCtxt != NULL) {
  670. ctxt->xpathCtxt->nsHash = NULL;
  671. xmlXPathFreeContext(ctxt->xpathCtxt);
  672. }
  673. if (ctxt->templTab != NULL)
  674. xmlFree(ctxt->templTab);
  675. if (ctxt->varsTab != NULL)
  676. xmlFree(ctxt->varsTab);
  677. if (ctxt->profTab != NULL)
  678. xmlFree(ctxt->profTab);
  679. if ((ctxt->extrasNr > 0) && (ctxt->extras != NULL)) {
  680. int i;
  681. for (i = 0;i < ctxt->extrasNr;i++) {
  682. if ((ctxt->extras[i].deallocate != NULL) &&
  683. (ctxt->extras[i].info != NULL))
  684. ctxt->extras[i].deallocate(ctxt->extras[i].info);
  685. }
  686. xmlFree(ctxt->extras);
  687. }
  688. xsltFreeGlobalVariables(ctxt);
  689. xsltFreeDocuments(ctxt);
  690. xsltFreeCtxtExts(ctxt);
  691. xsltFreeRVTs(ctxt);
  692. xsltTransformCacheFree(ctxt->cache);
  693. xmlDictFree(ctxt->dict);
  694. #ifdef WITH_XSLT_DEBUG
  695. xsltGenericDebug(xsltGenericDebugContext,
  696. "freeing transformation dictionary\n");
  697. #endif
  698. memset(ctxt, -1, sizeof(xsltTransformContext));
  699. xmlFree(ctxt);
  700. }
  701. /************************************************************************
  702. * *
  703. * Copy of Nodes in an XSLT fashion *
  704. * *
  705. ************************************************************************/
  706. /**
  707. * xsltAddChild:
  708. * @parent: the parent node
  709. * @cur: the child node
  710. *
  711. * Wrapper version of xmlAddChild with a more consistent behaviour on
  712. * error. One expect the use to be child = xsltAddChild(parent, child);
  713. * and the routine will take care of not leaking on errors or node merge
  714. *
  715. * Returns the child is successfully attached or NULL if merged or freed
  716. */
  717. static xmlNodePtr
  718. xsltAddChild(xmlNodePtr parent, xmlNodePtr cur) {
  719. xmlNodePtr ret;
  720. if (cur == NULL)
  721. return(NULL);
  722. if (parent == NULL) {
  723. xmlFreeNode(cur);
  724. return(NULL);
  725. }
  726. ret = xmlAddChild(parent, cur);
  727. return(ret);
  728. }
  729. /**
  730. * xsltAddTextString:
  731. * @ctxt: a XSLT process context
  732. * @target: the text node where the text will be attached
  733. * @string: the text string
  734. * @len: the string length in byte
  735. *
  736. * Extend the current text node with the new string, it handles coalescing
  737. *
  738. * Returns: the text node
  739. */
  740. static xmlNodePtr
  741. xsltAddTextString(xsltTransformContextPtr ctxt, xmlNodePtr target,
  742. const xmlChar *string, int len) {
  743. /*
  744. * optimization
  745. */
  746. if ((len <= 0) || (string == NULL) || (target == NULL))
  747. return(target);
  748. if (ctxt->lasttext == target->content) {
  749. int minSize;
  750. /* Check for integer overflow accounting for NUL terminator. */
  751. if (len >= INT_MAX - ctxt->lasttuse) {
  752. xsltTransformError(ctxt, NULL, target,
  753. "xsltCopyText: text allocation failed\n");
  754. return(NULL);
  755. }
  756. minSize = ctxt->lasttuse + len + 1;
  757. if (ctxt->lasttsize < minSize) {
  758. xmlChar *newbuf;
  759. int size;
  760. int extra;
  761. /* Double buffer size but increase by at least 100 bytes. */
  762. extra = minSize < 100 ? 100 : minSize;
  763. /* Check for integer overflow. */
  764. if (extra > INT_MAX - ctxt->lasttsize) {
  765. size = INT_MAX;
  766. }
  767. else {
  768. size = ctxt->lasttsize + extra;
  769. }
  770. newbuf = (xmlChar *) xmlRealloc(target->content,size);
  771. if (newbuf == NULL) {
  772. xsltTransformError(ctxt, NULL, target,
  773. "xsltCopyText: text allocation failed\n");
  774. return(NULL);
  775. }
  776. ctxt->lasttsize = size;
  777. ctxt->lasttext = newbuf;
  778. target->content = newbuf;
  779. }
  780. memcpy(&(target->content[ctxt->lasttuse]), string, len);
  781. ctxt->lasttuse += len;
  782. target->content[ctxt->lasttuse] = 0;
  783. } else {
  784. xmlNodeAddContent(target, string);
  785. ctxt->lasttext = target->content;
  786. len = xmlStrlen(target->content);
  787. ctxt->lasttsize = len;
  788. ctxt->lasttuse = len;
  789. }
  790. return(target);
  791. }
  792. /**
  793. * xsltCopyTextString:
  794. * @ctxt: a XSLT process context
  795. * @target: the element where the text will be attached
  796. * @string: the text string
  797. * @noescape: should disable-escaping be activated for this text node.
  798. *
  799. * Adds @string to a newly created or an existent text node child of
  800. * @target.
  801. *
  802. * Returns: the text node, where the text content of @cur is copied to.
  803. * NULL in case of API or internal errors.
  804. */
  805. xmlNodePtr
  806. xsltCopyTextString(xsltTransformContextPtr ctxt, xmlNodePtr target,
  807. const xmlChar *string, int noescape)
  808. {
  809. xmlNodePtr copy;
  810. int len;
  811. if (string == NULL)
  812. return(NULL);
  813. #ifdef WITH_XSLT_DEBUG_PROCESS
  814. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
  815. "xsltCopyTextString: copy text %s\n",
  816. string));
  817. #endif
  818. /*
  819. * Play safe and reset the merging mechanism for every new
  820. * target node.
  821. */
  822. if ((target == NULL) || (target->children == NULL)) {
  823. ctxt->lasttext = NULL;
  824. }
  825. /* handle coalescing of text nodes here */
  826. len = xmlStrlen(string);
  827. if ((ctxt->type == XSLT_OUTPUT_XML) &&
  828. (ctxt->style->cdataSection != NULL) &&
  829. (target != NULL) &&
  830. (target->type == XML_ELEMENT_NODE) &&
  831. (((target->ns == NULL) &&
  832. (xmlHashLookup2(ctxt->style->cdataSection,
  833. target->name, NULL) != NULL)) ||
  834. ((target->ns != NULL) &&
  835. (xmlHashLookup2(ctxt->style->cdataSection,
  836. target->name, target->ns->href) != NULL))))
  837. {
  838. /*
  839. * Process "cdata-section-elements".
  840. */
  841. if ((target->last != NULL) &&
  842. (target->last->type == XML_CDATA_SECTION_NODE))
  843. {
  844. return(xsltAddTextString(ctxt, target->last, string, len));
  845. }
  846. copy = xmlNewCDataBlock(ctxt->output, string, len);
  847. } else if (noescape) {
  848. /*
  849. * Process "disable-output-escaping".
  850. */
  851. if ((target != NULL) && (target->last != NULL) &&
  852. (target->last->type == XML_TEXT_NODE) &&
  853. (target->last->name == xmlStringTextNoenc))
  854. {
  855. return(xsltAddTextString(ctxt, target->last, string, len));
  856. }
  857. copy = xmlNewTextLen(string, len);
  858. if (copy != NULL)
  859. copy->name = xmlStringTextNoenc;
  860. } else {
  861. /*
  862. * Default processing.
  863. */
  864. if ((target != NULL) && (target->last != NULL) &&
  865. (target->last->type == XML_TEXT_NODE) &&
  866. (target->last->name == xmlStringText)) {
  867. return(xsltAddTextString(ctxt, target->last, string, len));
  868. }
  869. copy = xmlNewTextLen(string, len);
  870. }
  871. if (copy != NULL && target != NULL)
  872. copy = xsltAddChild(target, copy);
  873. if (copy != NULL) {
  874. ctxt->lasttext = copy->content;
  875. ctxt->lasttsize = len;
  876. ctxt->lasttuse = len;
  877. } else {
  878. xsltTransformError(ctxt, NULL, target,
  879. "xsltCopyTextString: text copy failed\n");
  880. ctxt->lasttext = NULL;
  881. }
  882. return(copy);
  883. }
  884. /**
  885. * xsltCopyText:
  886. * @ctxt: a XSLT process context
  887. * @target: the element where the text will be attached
  888. * @cur: the text or CDATA node
  889. * @interned: the string is in the target doc dictionary
  890. *
  891. * Copy the text content of @cur and append it to @target's children.
  892. *
  893. * Returns: the text node, where the text content of @cur is copied to.
  894. * NULL in case of API or internal errors.
  895. */
  896. static xmlNodePtr
  897. xsltCopyText(xsltTransformContextPtr ctxt, xmlNodePtr target,
  898. xmlNodePtr cur, int interned)
  899. {
  900. xmlNodePtr copy;
  901. if ((cur->type != XML_TEXT_NODE) &&
  902. (cur->type != XML_CDATA_SECTION_NODE))
  903. return(NULL);
  904. if (cur->content == NULL)
  905. return(NULL);
  906. #ifdef WITH_XSLT_DEBUG_PROCESS
  907. if (cur->type == XML_CDATA_SECTION_NODE) {
  908. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
  909. "xsltCopyText: copy CDATA text %s\n",
  910. cur->content));
  911. } else if (cur->name == xmlStringTextNoenc) {
  912. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
  913. "xsltCopyText: copy unescaped text %s\n",
  914. cur->content));
  915. } else {
  916. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_TEXT,xsltGenericDebug(xsltGenericDebugContext,
  917. "xsltCopyText: copy text %s\n",
  918. cur->content));
  919. }
  920. #endif
  921. /*
  922. * Play save and reset the merging mechanism for every new
  923. * target node.
  924. */
  925. if ((target == NULL) || (target->children == NULL)) {
  926. ctxt->lasttext = NULL;
  927. }
  928. if ((ctxt->style->cdataSection != NULL) &&
  929. (ctxt->type == XSLT_OUTPUT_XML) &&
  930. (target != NULL) &&
  931. (target->type == XML_ELEMENT_NODE) &&
  932. (((target->ns == NULL) &&
  933. (xmlHashLookup2(ctxt->style->cdataSection,
  934. target->name, NULL) != NULL)) ||
  935. ((target->ns != NULL) &&
  936. (xmlHashLookup2(ctxt->style->cdataSection,
  937. target->name, target->ns->href) != NULL))))
  938. {
  939. /*
  940. * Process "cdata-section-elements".
  941. */
  942. /*
  943. * OPTIMIZE TODO: xsltCopyText() is also used for attribute content.
  944. */
  945. /*
  946. * TODO: Since this doesn't merge adjacent CDATA-section nodes,
  947. * we'll get: <![CDATA[x]]><!CDATA[y]]>.
  948. * TODO: Reported in #321505.
  949. */
  950. if ((target->last != NULL) &&
  951. (target->last->type == XML_CDATA_SECTION_NODE))
  952. {
  953. /*
  954. * Append to existing CDATA-section node.
  955. */
  956. copy = xsltAddTextString(ctxt, target->last, cur->content,
  957. xmlStrlen(cur->content));
  958. goto exit;
  959. } else {
  960. unsigned int len;
  961. len = xmlStrlen(cur->content);
  962. copy = xmlNewCDataBlock(ctxt->output, cur->content, len);
  963. if (copy == NULL)
  964. goto exit;
  965. ctxt->lasttext = copy->content;
  966. ctxt->lasttsize = len;
  967. ctxt->lasttuse = len;
  968. }
  969. } else if ((target != NULL) &&
  970. (target->last != NULL) &&
  971. /* both escaped or both non-escaped text-nodes */
  972. (((target->last->type == XML_TEXT_NODE) &&
  973. (target->last->name == cur->name)) ||
  974. /* non-escaped text nodes and CDATA-section nodes */
  975. (((target->last->type == XML_CDATA_SECTION_NODE) &&
  976. (cur->name == xmlStringTextNoenc)))))
  977. {
  978. /*
  979. * we are appending to an existing text node
  980. */
  981. copy = xsltAddTextString(ctxt, target->last, cur->content,
  982. xmlStrlen(cur->content));
  983. goto exit;
  984. } else if ((interned) && (target != NULL) &&
  985. (target->doc != NULL) &&
  986. (target->doc->dict == ctxt->dict))
  987. {
  988. /*
  989. * TODO: DO we want to use this also for "text" output?
  990. */
  991. copy = xmlNewTextLen(NULL, 0);
  992. if (copy == NULL)
  993. goto exit;
  994. if (cur->name == xmlStringTextNoenc)
  995. copy->name = xmlStringTextNoenc;
  996. /*
  997. * Must confirm that content is in dict (bug 302821)
  998. * TODO: This check should be not needed for text coming
  999. * from the stylesheets
  1000. */
  1001. if (xmlDictOwns(ctxt->dict, cur->content))
  1002. copy->content = cur->content;
  1003. else {
  1004. if ((copy->content = xmlStrdup(cur->content)) == NULL)
  1005. return NULL;
  1006. }
  1007. ctxt->lasttext = NULL;
  1008. } else {
  1009. /*
  1010. * normal processing. keep counters to extend the text node
  1011. * in xsltAddTextString if needed.
  1012. */
  1013. unsigned int len;
  1014. len = xmlStrlen(cur->content);
  1015. copy = xmlNewTextLen(cur->content, len);
  1016. if (copy == NULL)
  1017. goto exit;
  1018. if (cur->name == xmlStringTextNoenc)
  1019. copy->name = xmlStringTextNoenc;
  1020. ctxt->lasttext = copy->content;
  1021. ctxt->lasttsize = len;
  1022. ctxt->lasttuse = len;
  1023. }
  1024. if (copy != NULL) {
  1025. if (target != NULL) {
  1026. copy->doc = target->doc;
  1027. /*
  1028. * MAYBE TODO: Maybe we should reset the ctxt->lasttext here
  1029. * to ensure that the optimized text-merging mechanism
  1030. * won't interfere with normal node-merging in any case.
  1031. */
  1032. copy = xsltAddChild(target, copy);
  1033. }
  1034. } else {
  1035. xsltTransformError(ctxt, NULL, target,
  1036. "xsltCopyText: text copy failed\n");
  1037. }
  1038. exit:
  1039. if ((copy == NULL) || (copy->content == NULL)) {
  1040. xsltTransformError(ctxt, NULL, target,
  1041. "Internal error in xsltCopyText(): "
  1042. "Failed to copy the string.\n");
  1043. ctxt->state = XSLT_STATE_STOPPED;
  1044. }
  1045. return(copy);
  1046. }
  1047. /**
  1048. * xsltShallowCopyAttr:
  1049. * @ctxt: a XSLT process context
  1050. * @invocNode: responsible node in the stylesheet; used for error reports
  1051. * @target: the element where the attribute will be grafted
  1052. * @attr: the attribute to be copied
  1053. *
  1054. * Do a copy of an attribute.
  1055. * Called by:
  1056. * - xsltCopyTree()
  1057. * - xsltCopyOf()
  1058. * - xsltCopy()
  1059. *
  1060. * Returns: a new xmlAttrPtr, or NULL in case of error.
  1061. */
  1062. static xmlAttrPtr
  1063. xsltShallowCopyAttr(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
  1064. xmlNodePtr target, xmlAttrPtr attr)
  1065. {
  1066. xmlAttrPtr copy;
  1067. xmlChar *value;
  1068. if (attr == NULL)
  1069. return(NULL);
  1070. if (target->type != XML_ELEMENT_NODE) {
  1071. xsltTransformError(ctxt, NULL, invocNode,
  1072. "Cannot add an attribute node to a non-element node.\n");
  1073. return(NULL);
  1074. }
  1075. if (target->children != NULL) {
  1076. xsltTransformError(ctxt, NULL, invocNode,
  1077. "Attribute nodes must be added before "
  1078. "any child nodes to an element.\n");
  1079. return(NULL);
  1080. }
  1081. value = xmlNodeListGetString(attr->doc, attr->children, 1);
  1082. if (attr->ns != NULL) {
  1083. xmlNsPtr ns;
  1084. ns = xsltGetSpecialNamespace(ctxt, invocNode,
  1085. attr->ns->href, attr->ns->prefix, target);
  1086. if (ns == NULL) {
  1087. xsltTransformError(ctxt, NULL, invocNode,
  1088. "Namespace fixup error: Failed to acquire an in-scope "
  1089. "namespace binding of the copied attribute '{%s}%s'.\n",
  1090. attr->ns->href, attr->name);
  1091. /*
  1092. * TODO: Should we just stop here?
  1093. */
  1094. }
  1095. /*
  1096. * Note that xmlSetNsProp() will take care of duplicates
  1097. * and assigns the new namespace even to a duplicate.
  1098. */
  1099. copy = xmlSetNsProp(target, ns, attr->name, value);
  1100. } else {
  1101. copy = xmlSetNsProp(target, NULL, attr->name, value);
  1102. }
  1103. if (value != NULL)
  1104. xmlFree(value);
  1105. if (copy == NULL)
  1106. return(NULL);
  1107. #if 0
  1108. /*
  1109. * NOTE: This was optimized according to bug #342695.
  1110. * TODO: Can this further be optimized, if source and target
  1111. * share the same dict and attr->children is just 1 text node
  1112. * which is in the dict? How probable is such a case?
  1113. */
  1114. /*
  1115. * TODO: Do we need to create an empty text node if the value
  1116. * is the empty string?
  1117. */
  1118. value = xmlNodeListGetString(attr->doc, attr->children, 1);
  1119. if (value != NULL) {
  1120. txtNode = xmlNewDocText(target->doc, NULL);
  1121. if (txtNode == NULL)
  1122. return(NULL);
  1123. if ((target->doc != NULL) &&
  1124. (target->doc->dict != NULL))
  1125. {
  1126. txtNode->content =
  1127. (xmlChar *) xmlDictLookup(target->doc->dict,
  1128. BAD_CAST value, -1);
  1129. xmlFree(value);
  1130. } else
  1131. txtNode->content = value;
  1132. copy->children = txtNode;
  1133. }
  1134. #endif
  1135. return(copy);
  1136. }
  1137. /**
  1138. * xsltCopyAttrListNoOverwrite:
  1139. * @ctxt: a XSLT process context
  1140. * @invocNode: responsible node in the stylesheet; used for error reports
  1141. * @target: the element where the new attributes will be grafted
  1142. * @attr: the first attribute in the list to be copied
  1143. *
  1144. * Copies a list of attribute nodes, starting with @attr, over to the
  1145. * @target element node.
  1146. *
  1147. * Called by:
  1148. * - xsltCopyTree()
  1149. *
  1150. * Returns 0 on success and -1 on errors and internal errors.
  1151. */
  1152. static int
  1153. xsltCopyAttrListNoOverwrite(xsltTransformContextPtr ctxt,
  1154. xmlNodePtr invocNode,
  1155. xmlNodePtr target, xmlAttrPtr attr)
  1156. {
  1157. xmlAttrPtr copy;
  1158. xmlNsPtr origNs = NULL, copyNs = NULL;
  1159. xmlChar *value;
  1160. /*
  1161. * Don't use xmlCopyProp() here, since it will try to
  1162. * reconciliate namespaces.
  1163. */
  1164. while (attr != NULL) {
  1165. /*
  1166. * Find a namespace node in the tree of @target.
  1167. * Avoid searching for the same ns.
  1168. */
  1169. if (attr->ns != origNs) {
  1170. origNs = attr->ns;
  1171. if (attr->ns != NULL) {
  1172. copyNs = xsltGetSpecialNamespace(ctxt, invocNode,
  1173. attr->ns->href, attr->ns->prefix, target);
  1174. if (copyNs == NULL)
  1175. return(-1);
  1176. } else
  1177. copyNs = NULL;
  1178. }
  1179. /*
  1180. * If attribute has a value, we need to copy it (watching out
  1181. * for possible entities)
  1182. */
  1183. if ((attr->children) && (attr->children->type == XML_TEXT_NODE) &&
  1184. (attr->children->next == NULL)) {
  1185. copy = xmlNewNsProp(target, copyNs, attr->name,
  1186. attr->children->content);
  1187. } else if (attr->children != NULL) {
  1188. value = xmlNodeListGetString(attr->doc, attr->children, 1);
  1189. copy = xmlNewNsProp(target, copyNs, attr->name, BAD_CAST value);
  1190. xmlFree(value);
  1191. } else {
  1192. copy = xmlNewNsProp(target, copyNs, attr->name, NULL);
  1193. }
  1194. if (copy == NULL)
  1195. return(-1);
  1196. attr = attr->next;
  1197. }
  1198. return(0);
  1199. }
  1200. /**
  1201. * xsltShallowCopyElem:
  1202. * @ctxt: the XSLT process context
  1203. * @node: the element node in the source tree
  1204. * or the Literal Result Element
  1205. * @insert: the parent in the result tree
  1206. * @isLRE: if @node is a Literal Result Element
  1207. *
  1208. * Make a copy of the element node @node
  1209. * and insert it as last child of @insert.
  1210. *
  1211. * URGENT TODO: The problem with this one (for the non-refactored code)
  1212. * is that it is used for both, Literal Result Elements *and*
  1213. * copying input nodes.
  1214. *
  1215. * BIG NOTE: This is only called for XML_ELEMENT_NODEs.
  1216. *
  1217. * Called from:
  1218. * xsltApplySequenceConstructor()
  1219. * (for Literal Result Elements - which is a problem)
  1220. * xsltCopy() (for shallow-copying elements via xsl:copy)
  1221. *
  1222. * Returns a pointer to the new node, or NULL in case of error
  1223. */
  1224. static xmlNodePtr
  1225. xsltShallowCopyElem(xsltTransformContextPtr ctxt, xmlNodePtr node,
  1226. xmlNodePtr insert, int isLRE)
  1227. {
  1228. xmlNodePtr copy;
  1229. if ((node->type == XML_DTD_NODE) || (insert == NULL))
  1230. return(NULL);
  1231. if ((node->type == XML_TEXT_NODE) ||
  1232. (node->type == XML_CDATA_SECTION_NODE))
  1233. return(xsltCopyText(ctxt, insert, node, 0));
  1234. copy = xmlDocCopyNode(node, insert->doc, 0);
  1235. if (copy != NULL) {
  1236. copy->doc = ctxt->output;
  1237. copy = xsltAddChild(insert, copy);
  1238. if (copy == NULL) {
  1239. xsltTransformError(ctxt, NULL, node,
  1240. "xsltShallowCopyElem: copy failed\n");
  1241. return (copy);
  1242. }
  1243. if (node->type == XML_ELEMENT_NODE) {
  1244. /*
  1245. * Add namespaces as they are needed
  1246. */
  1247. if (node->nsDef != NULL) {
  1248. /*
  1249. * TODO: Remove the LRE case in the refactored code
  1250. * gets enabled.
  1251. */
  1252. if (isLRE)
  1253. xsltCopyNamespaceList(ctxt, copy, node->nsDef);
  1254. else
  1255. xsltCopyNamespaceListInternal(copy, node->nsDef);
  1256. }
  1257. /*
  1258. * URGENT TODO: The problem with this is that it does not
  1259. * copy over all namespace nodes in scope.
  1260. * The damn thing about this is, that we would need to
  1261. * use the xmlGetNsList(), for every single node; this is
  1262. * also done in xsltCopyTree(), but only for the top node.
  1263. */
  1264. if (node->ns != NULL) {
  1265. if (isLRE) {
  1266. /*
  1267. * REVISIT TODO: Since the non-refactored code still does
  1268. * ns-aliasing, we need to call xsltGetNamespace() here.
  1269. * Remove this when ready.
  1270. */
  1271. copy->ns = xsltGetNamespace(ctxt, node, node->ns, copy);
  1272. } else {
  1273. copy->ns = xsltGetSpecialNamespace(ctxt,
  1274. node, node->ns->href, node->ns->prefix, copy);
  1275. }
  1276. } else if ((insert->type == XML_ELEMENT_NODE) &&
  1277. (insert->ns != NULL))
  1278. {
  1279. /*
  1280. * "Undeclare" the default namespace.
  1281. */
  1282. xsltGetSpecialNamespace(ctxt, node, NULL, NULL, copy);
  1283. }
  1284. }
  1285. } else {
  1286. xsltTransformError(ctxt, NULL, node,
  1287. "xsltShallowCopyElem: copy %s failed\n", node->name);
  1288. }
  1289. return(copy);
  1290. }
  1291. /**
  1292. * xsltCopyTreeList:
  1293. * @ctxt: a XSLT process context
  1294. * @invocNode: responsible node in the stylesheet; used for error reports
  1295. * @list: the list of element nodes in the source tree.
  1296. * @insert: the parent in the result tree.
  1297. * @isLRE: is this a literal result element list
  1298. * @topElemVisited: indicates if a top-most element was already processed
  1299. *
  1300. * Make a copy of the full list of tree @list
  1301. * and insert it as last children of @insert
  1302. *
  1303. * NOTE: Not to be used for Literal Result Elements.
  1304. *
  1305. * Used by:
  1306. * - xsltCopyOf()
  1307. *
  1308. * Returns a pointer to the new list, or NULL in case of error
  1309. */
  1310. static xmlNodePtr
  1311. xsltCopyTreeList(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
  1312. xmlNodePtr list,
  1313. xmlNodePtr insert, int isLRE, int topElemVisited)
  1314. {
  1315. xmlNodePtr copy, ret = NULL;
  1316. while (list != NULL) {
  1317. copy = xsltCopyTree(ctxt, invocNode,
  1318. list, insert, isLRE, topElemVisited);
  1319. if (copy != NULL) {
  1320. if (ret == NULL) {
  1321. ret = copy;
  1322. }
  1323. }
  1324. list = list->next;
  1325. }
  1326. return(ret);
  1327. }
  1328. /**
  1329. * xsltCopyNamespaceListInternal:
  1330. * @node: the target node
  1331. * @cur: the first namespace
  1332. *
  1333. * Do a copy of a namespace list. If @node is non-NULL the
  1334. * new namespaces are added automatically.
  1335. * Called by:
  1336. * xsltCopyTree()
  1337. *
  1338. * QUESTION: What is the exact difference between this function
  1339. * and xsltCopyNamespaceList() in "namespaces.c"?
  1340. * ANSWER: xsltCopyNamespaceList() tries to apply ns-aliases.
  1341. *
  1342. * Returns: a new xmlNsPtr, or NULL in case of error.
  1343. */
  1344. static xmlNsPtr
  1345. xsltCopyNamespaceListInternal(xmlNodePtr elem, xmlNsPtr ns) {
  1346. xmlNsPtr ret = NULL;
  1347. xmlNsPtr p = NULL, q, luNs;
  1348. if (ns == NULL)
  1349. return(NULL);
  1350. /*
  1351. * One can add namespaces only on element nodes
  1352. */
  1353. if ((elem != NULL) && (elem->type != XML_ELEMENT_NODE))
  1354. elem = NULL;
  1355. do {
  1356. if (ns->type != XML_NAMESPACE_DECL)
  1357. break;
  1358. /*
  1359. * Avoid duplicating namespace declarations on the tree.
  1360. */
  1361. if (elem != NULL) {
  1362. if ((elem->ns != NULL) &&
  1363. xmlStrEqual(elem->ns->prefix, ns->prefix) &&
  1364. xmlStrEqual(elem->ns->href, ns->href))
  1365. {
  1366. ns = ns->next;
  1367. continue;
  1368. }
  1369. luNs = xmlSearchNs(elem->doc, elem, ns->prefix);
  1370. if ((luNs != NULL) && (xmlStrEqual(luNs->href, ns->href)))
  1371. {
  1372. ns = ns->next;
  1373. continue;
  1374. }
  1375. }
  1376. q = xmlNewNs(elem, ns->href, ns->prefix);
  1377. if (p == NULL) {
  1378. ret = p = q;
  1379. } else if (q != NULL) {
  1380. p->next = q;
  1381. p = q;
  1382. }
  1383. ns = ns->next;
  1384. } while (ns != NULL);
  1385. return(ret);
  1386. }
  1387. /**
  1388. * xsltShallowCopyNsNode:
  1389. * @ctxt: the XSLT transformation context
  1390. * @invocNode: responsible node in the stylesheet; used for error reports
  1391. * @insert: the target element node in the result tree
  1392. * @ns: the namespace node
  1393. *
  1394. * This is used for copying ns-nodes with xsl:copy-of and xsl:copy.
  1395. *
  1396. * Returns a new/existing ns-node, or NULL.
  1397. */
  1398. static xmlNsPtr
  1399. xsltShallowCopyNsNode(xsltTransformContextPtr ctxt,
  1400. xmlNodePtr invocNode,
  1401. xmlNodePtr insert,
  1402. xmlNsPtr ns)
  1403. {
  1404. /*
  1405. * TODO: Contrary to header comments, this is declared as int.
  1406. * be modified to return a node pointer, or NULL if any error
  1407. */
  1408. xmlNsPtr tmpns;
  1409. if ((insert == NULL) || (insert->type != XML_ELEMENT_NODE))
  1410. return(NULL);
  1411. if (insert->children != NULL) {
  1412. xsltTransformError(ctxt, NULL, invocNode,
  1413. "Namespace nodes must be added before "
  1414. "any child nodes are added to an element.\n");
  1415. return(NULL);
  1416. }
  1417. /*
  1418. * BIG NOTE: Xalan-J simply overwrites any ns-decls with
  1419. * an equal prefix. We definitively won't do that.
  1420. *
  1421. * MSXML 4.0 and the .NET ignores ns-decls for which an
  1422. * equal prefix is already in use.
  1423. *
  1424. * Saxon raises an error like:
  1425. * "net.sf.saxon.xpath.DynamicError: Cannot create two namespace
  1426. * nodes with the same name".
  1427. *
  1428. * NOTE: We'll currently follow MSXML here.
  1429. * REVISIT TODO: Check if it's better to follow Saxon here.
  1430. */
  1431. if (ns->prefix == NULL) {
  1432. /*
  1433. * If we are adding ns-nodes to an element using e.g.
  1434. * <xsl:copy-of select="/foo/namespace::*">, then we need
  1435. * to ensure that we don't incorrectly declare a default
  1436. * namespace on an element in no namespace, which otherwise
  1437. * would move the element incorrectly into a namespace, if
  1438. * the node tree is serialized.
  1439. */
  1440. if (insert->ns == NULL)
  1441. goto occupied;
  1442. } else if ((ns->prefix[0] == 'x') &&
  1443. xmlStrEqual(ns->prefix, BAD_CAST "xml"))
  1444. {
  1445. /*
  1446. * The XML namespace is built in.
  1447. */
  1448. return(NULL);
  1449. }
  1450. if (insert->nsDef != NULL) {
  1451. tmpns = insert->nsDef;
  1452. do {
  1453. if ((tmpns->prefix == NULL) == (ns->prefix == NULL)) {
  1454. if ((tmpns->prefix == ns->prefix) ||
  1455. xmlStrEqual(tmpns->prefix, ns->prefix))
  1456. {
  1457. /*
  1458. * Same prefix.
  1459. */
  1460. if (xmlStrEqual(tmpns->href, ns->href))
  1461. return(NULL);
  1462. goto occupied;
  1463. }
  1464. }
  1465. tmpns = tmpns->next;
  1466. } while (tmpns != NULL);
  1467. }
  1468. tmpns = xmlSearchNs(insert->doc, insert, ns->prefix);
  1469. if ((tmpns != NULL) && xmlStrEqual(tmpns->href, ns->href))
  1470. return(NULL);
  1471. /*
  1472. * Declare a new namespace.
  1473. * TODO: The problem (wrt efficiency) with this xmlNewNs() is
  1474. * that it will again search the already declared namespaces
  1475. * for a duplicate :-/
  1476. */
  1477. return(xmlNewNs(insert, ns->href, ns->prefix));
  1478. occupied:
  1479. /*
  1480. * TODO: We could as well raise an error here (like Saxon does),
  1481. * or at least generate a warning.
  1482. */
  1483. return(NULL);
  1484. }
  1485. /**
  1486. * xsltCopyTree:
  1487. * @ctxt: the XSLT transformation context
  1488. * @invocNode: responsible node in the stylesheet; used for error reports
  1489. * @node: the element node in the source tree
  1490. * @insert: the parent in the result tree
  1491. * @isLRE: indicates if @node is a Literal Result Element
  1492. * @topElemVisited: indicates if a top-most element was already processed
  1493. *
  1494. * Make a copy of the full tree under the element node @node
  1495. * and insert it as last child of @insert
  1496. *
  1497. * NOTE: Not to be used for Literal Result Elements.
  1498. *
  1499. * Used by:
  1500. * - xsltCopyOf()
  1501. *
  1502. * Returns a pointer to the new tree, or NULL in case of error
  1503. */
  1504. static xmlNodePtr
  1505. xsltCopyTree(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
  1506. xmlNodePtr node, xmlNodePtr insert, int isLRE,
  1507. int topElemVisited)
  1508. {
  1509. xmlNodePtr copy;
  1510. if (node == NULL)
  1511. return(NULL);
  1512. switch (node->type) {
  1513. case XML_ELEMENT_NODE:
  1514. case XML_ENTITY_REF_NODE:
  1515. case XML_ENTITY_NODE:
  1516. case XML_PI_NODE:
  1517. case XML_COMMENT_NODE:
  1518. case XML_DOCUMENT_NODE:
  1519. case XML_HTML_DOCUMENT_NODE:
  1520. #ifdef LIBXML_DOCB_ENABLED
  1521. case XML_DOCB_DOCUMENT_NODE:
  1522. #endif
  1523. break;
  1524. case XML_TEXT_NODE: {
  1525. int noenc = (node->name == xmlStringTextNoenc);
  1526. return(xsltCopyTextString(ctxt, insert, node->content, noenc));
  1527. }
  1528. case XML_CDATA_SECTION_NODE:
  1529. return(xsltCopyTextString(ctxt, insert, node->content, 0));
  1530. case XML_ATTRIBUTE_NODE:
  1531. return((xmlNodePtr)
  1532. xsltShallowCopyAttr(ctxt, invocNode, insert, (xmlAttrPtr) node));
  1533. case XML_NAMESPACE_DECL:
  1534. return((xmlNodePtr) xsltShallowCopyNsNode(ctxt, invocNode,
  1535. insert, (xmlNsPtr) node));
  1536. case XML_DOCUMENT_TYPE_NODE:
  1537. case XML_DOCUMENT_FRAG_NODE:
  1538. case XML_NOTATION_NODE:
  1539. case XML_DTD_NODE:
  1540. case XML_ELEMENT_DECL:
  1541. case XML_ATTRIBUTE_DECL:
  1542. case XML_ENTITY_DECL:
  1543. case XML_XINCLUDE_START:
  1544. case XML_XINCLUDE_END:
  1545. return(NULL);
  1546. }
  1547. if (XSLT_IS_RES_TREE_FRAG(node)) {
  1548. if (node->children != NULL)
  1549. copy = xsltCopyTreeList(ctxt, invocNode,
  1550. node->children, insert, 0, 0);
  1551. else
  1552. copy = NULL;
  1553. return(copy);
  1554. }
  1555. copy = xmlDocCopyNode(node, insert->doc, 0);
  1556. if (copy != NULL) {
  1557. copy->doc = ctxt->output;
  1558. copy = xsltAddChild(insert, copy);
  1559. if (copy == NULL) {
  1560. xsltTransformError(ctxt, NULL, invocNode,
  1561. "xsltCopyTree: Copying of '%s' failed.\n", node->name);
  1562. return (copy);
  1563. }
  1564. /*
  1565. * The node may have been coalesced into another text node.
  1566. */
  1567. if (insert->last != copy)
  1568. return(insert->last);
  1569. copy->next = NULL;
  1570. if (node->type == XML_ELEMENT_NODE) {
  1571. /*
  1572. * Copy in-scope namespace nodes.
  1573. *
  1574. * REVISIT: Since we try to reuse existing in-scope ns-decls by
  1575. * using xmlSearchNsByHref(), this will eventually change
  1576. * the prefix of an original ns-binding; thus it might
  1577. * break QNames in element/attribute content.
  1578. * OPTIMIZE TODO: If we had a xmlNsPtr * on the transformation
  1579. * context, plus a ns-lookup function, which writes directly
  1580. * to a given list, then we wouldn't need to create/free the
  1581. * nsList every time.
  1582. */
  1583. if ((topElemVisited == 0) &&
  1584. (node->parent != NULL) &&
  1585. (node->parent->type != XML_DOCUMENT_NODE) &&
  1586. (node->parent->type != XML_HTML_DOCUMENT_NODE))
  1587. {
  1588. xmlNsPtr *nsList, *curns, ns;
  1589. /*
  1590. * If this is a top-most element in a tree to be
  1591. * copied, then we need to ensure that all in-scope
  1592. * namespaces are copied over. For nodes deeper in the
  1593. * tree, it is sufficient to reconcile only the ns-decls
  1594. * (node->nsDef entries).
  1595. */
  1596. nsList = xmlGetNsList(node->doc, node);
  1597. if (nsList != NULL) {
  1598. curns = nsList;
  1599. do {
  1600. /*
  1601. * Search by prefix first in order to break as less
  1602. * QNames in element/attribute content as possible.
  1603. */
  1604. ns = xmlSearchNs(insert->doc, insert,
  1605. (*curns)->prefix);
  1606. if ((ns == NULL) ||
  1607. (! xmlStrEqual(ns->href, (*curns)->href)))
  1608. {
  1609. ns = NULL;
  1610. /*
  1611. * Search by namespace name.
  1612. * REVISIT TODO: Currently disabled.
  1613. */
  1614. #if 0
  1615. ns = xmlSearchNsByHref(insert->doc,
  1616. insert, (*curns)->href);
  1617. #endif
  1618. }
  1619. if (ns == NULL) {
  1620. /*
  1621. * Declare a new namespace on the copied element.
  1622. */
  1623. ns = xmlNewNs(copy, (*curns)->href,
  1624. (*curns)->prefix);
  1625. /* TODO: Handle errors */
  1626. }
  1627. if (node->ns == *curns) {
  1628. /*
  1629. * If this was the original's namespace then set
  1630. * the generated counterpart on the copy.
  1631. */
  1632. copy->ns = ns;
  1633. }
  1634. curns++;
  1635. } while (*curns != NULL);
  1636. xmlFree(nsList);
  1637. }
  1638. } else if (node->nsDef != NULL) {
  1639. /*
  1640. * Copy over all namespace declaration attributes.
  1641. */
  1642. if (node->nsDef != NULL) {
  1643. if (isLRE)
  1644. xsltCopyNamespaceList(ctxt, copy, node->nsDef);
  1645. else
  1646. xsltCopyNamespaceListInternal(copy, node->nsDef);
  1647. }
  1648. }
  1649. /*
  1650. * Set the namespace.
  1651. */
  1652. if (node->ns != NULL) {
  1653. if (copy->ns == NULL) {
  1654. /*
  1655. * This will map copy->ns to one of the newly created
  1656. * in-scope ns-decls, OR create a new ns-decl on @copy.
  1657. */
  1658. copy->ns = xsltGetSpecialNamespace(ctxt, invocNode,
  1659. node->ns->href, node->ns->prefix, copy);
  1660. }
  1661. } else if ((insert->type == XML_ELEMENT_NODE) &&
  1662. (insert->ns != NULL))
  1663. {
  1664. /*
  1665. * "Undeclare" the default namespace on @copy with xmlns="".
  1666. */
  1667. xsltGetSpecialNamespace(ctxt, invocNode, NULL, NULL, copy);
  1668. }
  1669. /*
  1670. * Copy attribute nodes.
  1671. */
  1672. if (node->properties != NULL) {
  1673. xsltCopyAttrListNoOverwrite(ctxt, invocNode,
  1674. copy, node->properties);
  1675. }
  1676. if (topElemVisited == 0)
  1677. topElemVisited = 1;
  1678. }
  1679. /*
  1680. * Copy the subtree.
  1681. */
  1682. if (node->children != NULL) {
  1683. xsltCopyTreeList(ctxt, invocNode,
  1684. node->children, copy, isLRE, topElemVisited);
  1685. }
  1686. } else {
  1687. xsltTransformError(ctxt, NULL, invocNode,
  1688. "xsltCopyTree: Copying of '%s' failed.\n", node->name);
  1689. }
  1690. return(copy);
  1691. }
  1692. /************************************************************************
  1693. * *
  1694. * Error/fallback processing *
  1695. * *
  1696. ************************************************************************/
  1697. /**
  1698. * xsltApplyFallbacks:
  1699. * @ctxt: a XSLT process context
  1700. * @node: the node in the source tree.
  1701. * @inst: the node generating the error
  1702. *
  1703. * Process possible xsl:fallback nodes present under @inst
  1704. *
  1705. * Returns the number of xsl:fallback element found and processed
  1706. */
  1707. static int
  1708. xsltApplyFallbacks(xsltTransformContextPtr ctxt, xmlNodePtr node,
  1709. xmlNodePtr inst) {
  1710. xmlNodePtr child;
  1711. int ret = 0;
  1712. if ((ctxt == NULL) || (node == NULL) || (inst == NULL) ||
  1713. (inst->children == NULL))
  1714. return(0);
  1715. child = inst->children;
  1716. while (child != NULL) {
  1717. if ((IS_XSLT_ELEM(child)) &&
  1718. (xmlStrEqual(child->name, BAD_CAST "fallback"))) {
  1719. #ifdef WITH_XSLT_DEBUG_PARSING
  1720. xsltGenericDebug(xsltGenericDebugContext,
  1721. "applying xsl:fallback\n");
  1722. #endif
  1723. ret++;
  1724. xsltApplySequenceConstructor(ctxt, node, child->children,
  1725. NULL);
  1726. }
  1727. child = child->next;
  1728. }
  1729. return(ret);
  1730. }
  1731. /************************************************************************
  1732. * *
  1733. * Default processing *
  1734. * *
  1735. ************************************************************************/
  1736. /**
  1737. * xsltDefaultProcessOneNode:
  1738. * @ctxt: a XSLT process context
  1739. * @node: the node in the source tree.
  1740. * @params: extra parameters passed to the template if any
  1741. *
  1742. * Process the source node with the default built-in template rule:
  1743. * <xsl:template match="*|/">
  1744. * <xsl:apply-templates/>
  1745. * </xsl:template>
  1746. *
  1747. * and
  1748. *
  1749. * <xsl:template match="text()|@*">
  1750. * <xsl:value-of select="."/>
  1751. * </xsl:template>
  1752. *
  1753. * Note also that namespace declarations are copied directly:
  1754. *
  1755. * the built-in template rule is the only template rule that is applied
  1756. * for namespace nodes.
  1757. */
  1758. static void
  1759. xsltDefaultProcessOneNode(xsltTransformContextPtr ctxt, xmlNodePtr node,
  1760. xsltStackElemPtr params) {
  1761. xmlNodePtr copy;
  1762. xmlNodePtr delete = NULL, cur;
  1763. int nbchild = 0, oldSize;
  1764. int childno = 0, oldPos;
  1765. xsltTemplatePtr template;
  1766. CHECK_STOPPED;
  1767. /*
  1768. * Handling of leaves
  1769. */
  1770. switch (node->type) {
  1771. case XML_DOCUMENT_NODE:
  1772. case XML_HTML_DOCUMENT_NODE:
  1773. case XML_ELEMENT_NODE:
  1774. break;
  1775. case XML_CDATA_SECTION_NODE:
  1776. #ifdef WITH_XSLT_DEBUG_PROCESS
  1777. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1778. "xsltDefaultProcessOneNode: copy CDATA %s\n",
  1779. node->content));
  1780. #endif
  1781. copy = xsltCopyText(ctxt, ctxt->insert, node, 0);
  1782. if (copy == NULL) {
  1783. xsltTransformError(ctxt, NULL, node,
  1784. "xsltDefaultProcessOneNode: cdata copy failed\n");
  1785. }
  1786. return;
  1787. case XML_TEXT_NODE:
  1788. #ifdef WITH_XSLT_DEBUG_PROCESS
  1789. if (node->content == NULL) {
  1790. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1791. "xsltDefaultProcessOneNode: copy empty text\n"));
  1792. return;
  1793. } else {
  1794. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1795. "xsltDefaultProcessOneNode: copy text %s\n",
  1796. node->content));
  1797. }
  1798. #endif
  1799. copy = xsltCopyText(ctxt, ctxt->insert, node, 0);
  1800. if (copy == NULL) {
  1801. xsltTransformError(ctxt, NULL, node,
  1802. "xsltDefaultProcessOneNode: text copy failed\n");
  1803. }
  1804. return;
  1805. case XML_ATTRIBUTE_NODE:
  1806. cur = node->children;
  1807. while ((cur != NULL) && (cur->type != XML_TEXT_NODE))
  1808. cur = cur->next;
  1809. if (cur == NULL) {
  1810. xsltTransformError(ctxt, NULL, node,
  1811. "xsltDefaultProcessOneNode: no text for attribute\n");
  1812. } else {
  1813. #ifdef WITH_XSLT_DEBUG_PROCESS
  1814. if (cur->content == NULL) {
  1815. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1816. "xsltDefaultProcessOneNode: copy empty text\n"));
  1817. } else {
  1818. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1819. "xsltDefaultProcessOneNode: copy text %s\n",
  1820. cur->content));
  1821. }
  1822. #endif
  1823. copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
  1824. if (copy == NULL) {
  1825. xsltTransformError(ctxt, NULL, node,
  1826. "xsltDefaultProcessOneNode: text copy failed\n");
  1827. }
  1828. }
  1829. return;
  1830. default:
  1831. return;
  1832. }
  1833. /*
  1834. * Handling of Elements: first pass, cleanup and counting
  1835. */
  1836. cur = node->children;
  1837. while (cur != NULL) {
  1838. switch (cur->type) {
  1839. case XML_TEXT_NODE:
  1840. case XML_CDATA_SECTION_NODE:
  1841. case XML_DOCUMENT_NODE:
  1842. case XML_HTML_DOCUMENT_NODE:
  1843. case XML_ELEMENT_NODE:
  1844. case XML_PI_NODE:
  1845. case XML_COMMENT_NODE:
  1846. nbchild++;
  1847. break;
  1848. case XML_DTD_NODE:
  1849. /* Unlink the DTD, it's still reachable using doc->intSubset */
  1850. if (cur->next != NULL)
  1851. cur->next->prev = cur->prev;
  1852. if (cur->prev != NULL)
  1853. cur->prev->next = cur->next;
  1854. break;
  1855. default:
  1856. #ifdef WITH_XSLT_DEBUG_PROCESS
  1857. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1858. "xsltDefaultProcessOneNode: skipping node type %d\n",
  1859. cur->type));
  1860. #endif
  1861. delete = cur;
  1862. }
  1863. cur = cur->next;
  1864. if (delete != NULL) {
  1865. #ifdef WITH_XSLT_DEBUG_PROCESS
  1866. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1867. "xsltDefaultProcessOneNode: removing ignorable blank node\n"));
  1868. #endif
  1869. xmlUnlinkNode(delete);
  1870. xmlFreeNode(delete);
  1871. delete = NULL;
  1872. }
  1873. }
  1874. if (delete != NULL) {
  1875. #ifdef WITH_XSLT_DEBUG_PROCESS
  1876. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1877. "xsltDefaultProcessOneNode: removing ignorable blank node\n"));
  1878. #endif
  1879. xmlUnlinkNode(delete);
  1880. xmlFreeNode(delete);
  1881. delete = NULL;
  1882. }
  1883. /*
  1884. * Handling of Elements: second pass, actual processing
  1885. *
  1886. * Note that params are passed to the next template. This matches
  1887. * XSLT 2.0 behavior but doesn't conform to XSLT 1.0.
  1888. */
  1889. oldSize = ctxt->xpathCtxt->contextSize;
  1890. oldPos = ctxt->xpathCtxt->proximityPosition;
  1891. cur = node->children;
  1892. while (cur != NULL) {
  1893. childno++;
  1894. switch (cur->type) {
  1895. case XML_DOCUMENT_NODE:
  1896. case XML_HTML_DOCUMENT_NODE:
  1897. case XML_ELEMENT_NODE:
  1898. ctxt->xpathCtxt->contextSize = nbchild;
  1899. ctxt->xpathCtxt->proximityPosition = childno;
  1900. xsltProcessOneNode(ctxt, cur, params);
  1901. break;
  1902. case XML_CDATA_SECTION_NODE:
  1903. template = xsltGetTemplate(ctxt, cur, NULL);
  1904. if (template) {
  1905. #ifdef WITH_XSLT_DEBUG_PROCESS
  1906. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1907. "xsltDefaultProcessOneNode: applying template for CDATA %s\n",
  1908. cur->content));
  1909. #endif
  1910. /*
  1911. * Instantiate the xsl:template.
  1912. */
  1913. xsltApplyXSLTTemplate(ctxt, cur, template->content,
  1914. template, params);
  1915. } else /* if (ctxt->mode == NULL) */ {
  1916. #ifdef WITH_XSLT_DEBUG_PROCESS
  1917. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1918. "xsltDefaultProcessOneNode: copy CDATA %s\n",
  1919. cur->content));
  1920. #endif
  1921. copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
  1922. if (copy == NULL) {
  1923. xsltTransformError(ctxt, NULL, cur,
  1924. "xsltDefaultProcessOneNode: cdata copy failed\n");
  1925. }
  1926. }
  1927. break;
  1928. case XML_TEXT_NODE:
  1929. template = xsltGetTemplate(ctxt, cur, NULL);
  1930. if (template) {
  1931. #ifdef WITH_XSLT_DEBUG_PROCESS
  1932. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1933. "xsltDefaultProcessOneNode: applying template for text %s\n",
  1934. cur->content));
  1935. #endif
  1936. ctxt->xpathCtxt->contextSize = nbchild;
  1937. ctxt->xpathCtxt->proximityPosition = childno;
  1938. /*
  1939. * Instantiate the xsl:template.
  1940. */
  1941. xsltApplyXSLTTemplate(ctxt, cur, template->content,
  1942. template, params);
  1943. } else /* if (ctxt->mode == NULL) */ {
  1944. #ifdef WITH_XSLT_DEBUG_PROCESS
  1945. if (cur->content == NULL) {
  1946. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1947. "xsltDefaultProcessOneNode: copy empty text\n"));
  1948. } else {
  1949. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1950. "xsltDefaultProcessOneNode: copy text %s\n",
  1951. cur->content));
  1952. }
  1953. #endif
  1954. copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
  1955. if (copy == NULL) {
  1956. xsltTransformError(ctxt, NULL, cur,
  1957. "xsltDefaultProcessOneNode: text copy failed\n");
  1958. }
  1959. }
  1960. break;
  1961. case XML_PI_NODE:
  1962. case XML_COMMENT_NODE:
  1963. template = xsltGetTemplate(ctxt, cur, NULL);
  1964. if (template) {
  1965. #ifdef WITH_XSLT_DEBUG_PROCESS
  1966. if (cur->type == XML_PI_NODE) {
  1967. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1968. "xsltDefaultProcessOneNode: template found for PI %s\n",
  1969. cur->name));
  1970. } else if (cur->type == XML_COMMENT_NODE) {
  1971. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1972. "xsltDefaultProcessOneNode: template found for comment\n"));
  1973. }
  1974. #endif
  1975. ctxt->xpathCtxt->contextSize = nbchild;
  1976. ctxt->xpathCtxt->proximityPosition = childno;
  1977. /*
  1978. * Instantiate the xsl:template.
  1979. */
  1980. xsltApplyXSLTTemplate(ctxt, cur, template->content,
  1981. template, params);
  1982. }
  1983. break;
  1984. default:
  1985. break;
  1986. }
  1987. cur = cur->next;
  1988. }
  1989. ctxt->xpathCtxt->contextSize = oldSize;
  1990. ctxt->xpathCtxt->proximityPosition = oldPos;
  1991. }
  1992. /**
  1993. * xsltProcessOneNode:
  1994. * @ctxt: a XSLT process context
  1995. * @contextNode: the "current node" in the source tree
  1996. * @withParams: extra parameters (e.g. xsl:with-param) passed to the
  1997. * template if any
  1998. *
  1999. * Process the source node.
  2000. */
  2001. void
  2002. xsltProcessOneNode(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  2003. xsltStackElemPtr withParams)
  2004. {
  2005. xsltTemplatePtr templ;
  2006. xmlNodePtr oldNode;
  2007. templ = xsltGetTemplate(ctxt, contextNode, NULL);
  2008. /*
  2009. * If no template is found, apply the default rule.
  2010. */
  2011. if (templ == NULL) {
  2012. #ifdef WITH_XSLT_DEBUG_PROCESS
  2013. if (contextNode->type == XML_DOCUMENT_NODE) {
  2014. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  2015. "xsltProcessOneNode: no template found for /\n"));
  2016. } else if (contextNode->type == XML_CDATA_SECTION_NODE) {
  2017. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  2018. "xsltProcessOneNode: no template found for CDATA\n"));
  2019. } else if (contextNode->type == XML_ATTRIBUTE_NODE) {
  2020. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  2021. "xsltProcessOneNode: no template found for attribute %s\n",
  2022. ((xmlAttrPtr) contextNode)->name));
  2023. } else {
  2024. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  2025. "xsltProcessOneNode: no template found for %s\n", contextNode->name));
  2026. }
  2027. #endif
  2028. oldNode = ctxt->node;
  2029. ctxt->node = contextNode;
  2030. xsltDefaultProcessOneNode(ctxt, contextNode, withParams);
  2031. ctxt->node = oldNode;
  2032. return;
  2033. }
  2034. if (contextNode->type == XML_ATTRIBUTE_NODE) {
  2035. xsltTemplatePtr oldCurTempRule = ctxt->currentTemplateRule;
  2036. /*
  2037. * Set the "current template rule".
  2038. */
  2039. ctxt->currentTemplateRule = templ;
  2040. #ifdef WITH_XSLT_DEBUG_PROCESS
  2041. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  2042. "xsltProcessOneNode: applying template '%s' for attribute %s\n",
  2043. templ->match, contextNode->name));
  2044. #endif
  2045. xsltApplyXSLTTemplate(ctxt, contextNode, templ->content, templ, withParams);
  2046. ctxt->currentTemplateRule = oldCurTempRule;
  2047. } else {
  2048. xsltTemplatePtr oldCurTempRule = ctxt->currentTemplateRule;
  2049. /*
  2050. * Set the "current template rule".
  2051. */
  2052. ctxt->currentTemplateRule = templ;
  2053. #ifdef WITH_XSLT_DEBUG_PROCESS
  2054. if (contextNode->type == XML_DOCUMENT_NODE) {
  2055. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  2056. "xsltProcessOneNode: applying template '%s' for /\n",
  2057. templ->match));
  2058. } else {
  2059. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  2060. "xsltProcessOneNode: applying template '%s' for %s\n",
  2061. templ->match, contextNode->name));
  2062. }
  2063. #endif
  2064. xsltApplyXSLTTemplate(ctxt, contextNode, templ->content, templ, withParams);
  2065. ctxt->currentTemplateRule = oldCurTempRule;
  2066. }
  2067. }
  2068. #ifdef WITH_DEBUGGER
  2069. static xmlNodePtr
  2070. xsltDebuggerStartSequenceConstructor(xsltTransformContextPtr ctxt,
  2071. xmlNodePtr contextNode,
  2072. xmlNodePtr list,
  2073. xsltTemplatePtr templ,
  2074. int *addCallResult)
  2075. {
  2076. xmlNodePtr debugedNode = NULL;
  2077. if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
  2078. if (templ) {
  2079. *addCallResult = xslAddCall(templ, templ->elem);
  2080. } else {
  2081. *addCallResult = xslAddCall(NULL, list);
  2082. }
  2083. switch (ctxt->debugStatus) {
  2084. case XSLT_DEBUG_RUN_RESTART:
  2085. case XSLT_DEBUG_QUIT:
  2086. if (*addCallResult)
  2087. xslDropCall();
  2088. return(NULL);
  2089. }
  2090. if (templ) {
  2091. xslHandleDebugger(templ->elem, contextNode, templ, ctxt);
  2092. debugedNode = templ->elem;
  2093. } else if (list) {
  2094. xslHandleDebugger(list, contextNode, templ, ctxt);
  2095. debugedNode = list;
  2096. } else if (ctxt->inst) {
  2097. xslHandleDebugger(ctxt->inst, contextNode, templ, ctxt);
  2098. debugedNode = ctxt->inst;
  2099. }
  2100. }
  2101. return(debugedNode);
  2102. }
  2103. #endif /* WITH_DEBUGGER */
  2104. /**
  2105. * xsltLocalVariablePush:
  2106. * @ctxt: the transformation context
  2107. * @variable: variable to be pushed to the variable stack
  2108. * @level: new value for variable's level
  2109. *
  2110. * Places the variable onto the local variable stack
  2111. *
  2112. * Returns: 0 for success, -1 for any error
  2113. * **NOTE:**
  2114. * This is an internal routine and should not be called by users!
  2115. */
  2116. int
  2117. xsltLocalVariablePush(xsltTransformContextPtr ctxt,
  2118. xsltStackElemPtr variable,
  2119. int level)
  2120. {
  2121. if (ctxt->varsMax == 0) {
  2122. ctxt->varsMax = 10;
  2123. ctxt->varsTab =
  2124. (xsltStackElemPtr *) xmlMalloc(ctxt->varsMax *
  2125. sizeof(ctxt->varsTab[0]));
  2126. if (ctxt->varsTab == NULL) {
  2127. xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
  2128. return (-1);
  2129. }
  2130. }
  2131. if (ctxt->varsNr >= ctxt->varsMax) {
  2132. ctxt->varsMax *= 2;
  2133. ctxt->varsTab =
  2134. (xsltStackElemPtr *) xmlRealloc(ctxt->varsTab,
  2135. ctxt->varsMax *
  2136. sizeof(ctxt->varsTab[0]));
  2137. if (ctxt->varsTab == NULL) {
  2138. xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
  2139. return (-1);
  2140. }
  2141. }
  2142. ctxt->varsTab[ctxt->varsNr++] = variable;
  2143. ctxt->vars = variable;
  2144. variable->level = level;
  2145. return(0);
  2146. }
  2147. /**
  2148. * xsltReleaseLocalRVTs:
  2149. *
  2150. * Fragments which are results of extension instructions
  2151. * are preserved; all other fragments are freed/cached.
  2152. */
  2153. static void
  2154. xsltReleaseLocalRVTs(xsltTransformContextPtr ctxt, xmlDocPtr base)
  2155. {
  2156. xmlDocPtr cur = ctxt->localRVT, tmp;
  2157. if (cur == base)
  2158. return;
  2159. if (cur->prev != NULL)
  2160. xsltTransformError(ctxt, NULL, NULL, "localRVT not head of list\n");
  2161. /* Reset localRVT early because some RVTs might be registered again. */
  2162. ctxt->localRVT = base;
  2163. if (base != NULL)
  2164. base->prev = NULL;
  2165. do {
  2166. tmp = cur;
  2167. cur = (xmlDocPtr) cur->next;
  2168. if (tmp->psvi == XSLT_RVT_LOCAL) {
  2169. xsltReleaseRVT(ctxt, tmp);
  2170. } else if (tmp->psvi == XSLT_RVT_GLOBAL) {
  2171. xsltRegisterPersistRVT(ctxt, tmp);
  2172. } else if (tmp->psvi == XSLT_RVT_FUNC_RESULT) {
  2173. /*
  2174. * This will either register the RVT again or move it to the
  2175. * context variable.
  2176. */
  2177. xsltRegisterLocalRVT(ctxt, tmp);
  2178. tmp->psvi = XSLT_RVT_FUNC_RESULT;
  2179. } else {
  2180. xmlGenericError(xmlGenericErrorContext,
  2181. "xsltReleaseLocalRVTs: Unexpected RVT flag %p\n",
  2182. tmp->psvi);
  2183. }
  2184. } while (cur != base);
  2185. }
  2186. /**
  2187. * xsltApplySequenceConstructor:
  2188. * @ctxt: a XSLT process context
  2189. * @contextNode: the "current node" in the source tree
  2190. * @list: the nodes of a sequence constructor;
  2191. * (plus leading xsl:param elements)
  2192. * @templ: the compiled xsl:template (optional)
  2193. *
  2194. * Processes a sequence constructor.
  2195. *
  2196. * NOTE: ctxt->currentTemplateRule was introduced to reflect the
  2197. * semantics of "current template rule". I.e. the field ctxt->templ
  2198. * is not intended to reflect this, thus always pushed onto the
  2199. * template stack.
  2200. */
  2201. static void
  2202. xsltApplySequenceConstructor(xsltTransformContextPtr ctxt,
  2203. xmlNodePtr contextNode, xmlNodePtr list,
  2204. xsltTemplatePtr templ)
  2205. {
  2206. xmlNodePtr oldInsert, oldInst, oldCurInst, oldContextNode;
  2207. xmlNodePtr cur, insert, copy = NULL;
  2208. int level = 0, oldVarsNr;
  2209. xmlDocPtr oldLocalFragmentTop;
  2210. #ifdef XSLT_REFACTORED
  2211. xsltStylePreCompPtr info;
  2212. #endif
  2213. #ifdef WITH_DEBUGGER
  2214. int addCallResult = 0;
  2215. xmlNodePtr debuggedNode = NULL;
  2216. #endif
  2217. if (ctxt == NULL)
  2218. return;
  2219. #ifdef WITH_DEBUGGER
  2220. if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
  2221. debuggedNode =
  2222. xsltDebuggerStartSequenceConstructor(ctxt, contextNode,
  2223. list, templ, &addCallResult);
  2224. if (debuggedNode == NULL)
  2225. return;
  2226. }
  2227. #endif
  2228. if (list == NULL)
  2229. return;
  2230. CHECK_STOPPED;
  2231. /*
  2232. * Check for infinite recursion: stop if the maximum of nested templates
  2233. * is excceeded. Adjust xsltMaxDepth if you need more.
  2234. */
  2235. if (ctxt->depth >= ctxt->maxTemplateDepth) {
  2236. xsltTransformError(ctxt, NULL, list,
  2237. "xsltApplySequenceConstructor: A potential infinite template "
  2238. "recursion was detected.\n"
  2239. "You can adjust xsltMaxDepth (--maxdepth) in order to "
  2240. "raise the maximum number of nested template calls and "
  2241. "variables/params (currently set to %d).\n",
  2242. ctxt->maxTemplateDepth);
  2243. xsltDebug(ctxt, contextNode, list, NULL);
  2244. ctxt->state = XSLT_STATE_STOPPED;
  2245. return;
  2246. }
  2247. ctxt->depth++;
  2248. oldLocalFragmentTop = ctxt->localRVT;
  2249. oldInsert = insert = ctxt->insert;
  2250. oldInst = oldCurInst = ctxt->inst;
  2251. oldContextNode = ctxt->node;
  2252. /*
  2253. * Save current number of variables on the stack; new vars are popped when
  2254. * exiting.
  2255. */
  2256. oldVarsNr = ctxt->varsNr;
  2257. /*
  2258. * Process the sequence constructor.
  2259. */
  2260. cur = list;
  2261. while (cur != NULL) {
  2262. if (ctxt->opLimit != 0) {
  2263. if (ctxt->opCount >= ctxt->opLimit) {
  2264. xsltTransformError(ctxt, NULL, cur,
  2265. "xsltApplySequenceConstructor: "
  2266. "Operation limit exceeded\n");
  2267. ctxt->state = XSLT_STATE_STOPPED;
  2268. goto error;
  2269. }
  2270. ctxt->opCount += 1;
  2271. }
  2272. ctxt->inst = cur;
  2273. #ifdef WITH_DEBUGGER
  2274. switch (ctxt->debugStatus) {
  2275. case XSLT_DEBUG_RUN_RESTART:
  2276. case XSLT_DEBUG_QUIT:
  2277. break;
  2278. }
  2279. #endif
  2280. /*
  2281. * Test; we must have a valid insertion point.
  2282. */
  2283. if (insert == NULL) {
  2284. #ifdef WITH_XSLT_DEBUG_PROCESS
  2285. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2286. "xsltApplySequenceConstructor: insert == NULL !\n"));
  2287. #endif
  2288. goto error;
  2289. }
  2290. #ifdef WITH_DEBUGGER
  2291. if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (debuggedNode != cur))
  2292. xslHandleDebugger(cur, contextNode, templ, ctxt);
  2293. #endif
  2294. #ifdef XSLT_REFACTORED
  2295. if (cur->type == XML_ELEMENT_NODE) {
  2296. info = (xsltStylePreCompPtr) cur->psvi;
  2297. /*
  2298. * We expect a compiled representation on:
  2299. * 1) XSLT instructions of this XSLT version (1.0)
  2300. * (with a few exceptions)
  2301. * 2) Literal result elements
  2302. * 3) Extension instructions
  2303. * 4) XSLT instructions of future XSLT versions
  2304. * (forwards-compatible mode).
  2305. */
  2306. if (info == NULL) {
  2307. /*
  2308. * Handle the rare cases where we don't expect a compiled
  2309. * representation on an XSLT element.
  2310. */
  2311. if (IS_XSLT_ELEM_FAST(cur) && IS_XSLT_NAME(cur, "message")) {
  2312. xsltMessage(ctxt, contextNode, cur);
  2313. goto skip_children;
  2314. }
  2315. /*
  2316. * Something really went wrong:
  2317. */
  2318. xsltTransformError(ctxt, NULL, cur,
  2319. "Internal error in xsltApplySequenceConstructor(): "
  2320. "The element '%s' in the stylesheet has no compiled "
  2321. "representation.\n",
  2322. cur->name);
  2323. goto skip_children;
  2324. }
  2325. if (info->type == XSLT_FUNC_LITERAL_RESULT_ELEMENT) {
  2326. xsltStyleItemLRElementInfoPtr lrInfo =
  2327. (xsltStyleItemLRElementInfoPtr) info;
  2328. /*
  2329. * Literal result elements
  2330. * --------------------------------------------------------
  2331. */
  2332. #ifdef WITH_XSLT_DEBUG_PROCESS
  2333. XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
  2334. xsltGenericDebug(xsltGenericDebugContext,
  2335. "xsltApplySequenceConstructor: copy literal result "
  2336. "element '%s'\n", cur->name));
  2337. #endif
  2338. /*
  2339. * Copy the raw element-node.
  2340. * OLD: if ((copy = xsltShallowCopyElem(ctxt, cur, insert))
  2341. * == NULL)
  2342. * goto error;
  2343. */
  2344. copy = xmlDocCopyNode(cur, insert->doc, 0);
  2345. if (copy == NULL) {
  2346. xsltTransformError(ctxt, NULL, cur,
  2347. "Internal error in xsltApplySequenceConstructor(): "
  2348. "Failed to copy literal result element '%s'.\n",
  2349. cur->name);
  2350. goto error;
  2351. } else {
  2352. /*
  2353. * Add the element-node to the result tree.
  2354. */
  2355. copy->doc = ctxt->output;
  2356. copy = xsltAddChild(insert, copy);
  2357. /*
  2358. * Create effective namespaces declarations.
  2359. * OLD: xsltCopyNamespaceList(ctxt, copy, cur->nsDef);
  2360. */
  2361. if (lrInfo->effectiveNs != NULL) {
  2362. xsltEffectiveNsPtr effNs = lrInfo->effectiveNs;
  2363. xmlNsPtr ns, lastns = NULL;
  2364. while (effNs != NULL) {
  2365. /*
  2366. * Avoid generating redundant namespace
  2367. * declarations; thus lookup if there is already
  2368. * such a ns-decl in the result.
  2369. */
  2370. ns = xmlSearchNs(copy->doc, copy, effNs->prefix);
  2371. if ((ns != NULL) &&
  2372. (xmlStrEqual(ns->href, effNs->nsName)))
  2373. {
  2374. effNs = effNs->next;
  2375. continue;
  2376. }
  2377. ns = xmlNewNs(copy, effNs->nsName, effNs->prefix);
  2378. if (ns == NULL) {
  2379. xsltTransformError(ctxt, NULL, cur,
  2380. "Internal error in "
  2381. "xsltApplySequenceConstructor(): "
  2382. "Failed to copy a namespace "
  2383. "declaration.\n");
  2384. goto error;
  2385. }
  2386. if (lastns == NULL)
  2387. copy->nsDef = ns;
  2388. else
  2389. lastns->next =ns;
  2390. lastns = ns;
  2391. effNs = effNs->next;
  2392. }
  2393. }
  2394. /*
  2395. * NOTE that we don't need to apply ns-alising: this was
  2396. * already done at compile-time.
  2397. */
  2398. if (cur->ns != NULL) {
  2399. /*
  2400. * If there's no such ns-decl in the result tree,
  2401. * then xsltGetSpecialNamespace() will
  2402. * create a ns-decl on the copied node.
  2403. */
  2404. copy->ns = xsltGetSpecialNamespace(ctxt, cur,
  2405. cur->ns->href, cur->ns->prefix, copy);
  2406. } else {
  2407. /*
  2408. * Undeclare the default namespace if needed.
  2409. * This can be skipped, if the result element has
  2410. * no ns-decls, in which case the result element
  2411. * obviously does not declare a default namespace;
  2412. * AND there's either no parent, or the parent
  2413. * element is in no namespace; this means there's no
  2414. * default namespace is scope to care about.
  2415. *
  2416. * REVISIT: This might result in massive
  2417. * generation of ns-decls if nodes in a default
  2418. * namespaces are mixed with nodes in no namespace.
  2419. *
  2420. */
  2421. if (copy->nsDef ||
  2422. ((insert != NULL) &&
  2423. (insert->type == XML_ELEMENT_NODE) &&
  2424. (insert->ns != NULL)))
  2425. {
  2426. xsltGetSpecialNamespace(ctxt, cur,
  2427. NULL, NULL, copy);
  2428. }
  2429. }
  2430. }
  2431. /*
  2432. * SPEC XSLT 2.0 "Each attribute of the literal result
  2433. * element, other than an attribute in the XSLT namespace,
  2434. * is processed to produce an attribute for the element in
  2435. * the result tree."
  2436. * NOTE: See bug #341325.
  2437. */
  2438. if (cur->properties != NULL) {
  2439. xsltAttrListTemplateProcess(ctxt, copy, cur->properties);
  2440. }
  2441. } else if (IS_XSLT_ELEM_FAST(cur)) {
  2442. /*
  2443. * XSLT instructions
  2444. * --------------------------------------------------------
  2445. */
  2446. if (info->type == XSLT_FUNC_UNKOWN_FORWARDS_COMPAT) {
  2447. /*
  2448. * We hit an unknown XSLT element.
  2449. * Try to apply one of the fallback cases.
  2450. */
  2451. ctxt->insert = insert;
  2452. if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
  2453. xsltTransformError(ctxt, NULL, cur,
  2454. "The is no fallback behaviour defined for "
  2455. "the unknown XSLT element '%s'.\n",
  2456. cur->name);
  2457. }
  2458. ctxt->insert = oldInsert;
  2459. } else if (info->func != NULL) {
  2460. /*
  2461. * Execute the XSLT instruction.
  2462. */
  2463. ctxt->insert = insert;
  2464. info->func(ctxt, contextNode, cur,
  2465. (xsltElemPreCompPtr) info);
  2466. /*
  2467. * Cleanup temporary tree fragments.
  2468. */
  2469. if (oldLocalFragmentTop != ctxt->localRVT)
  2470. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  2471. ctxt->insert = oldInsert;
  2472. } else if (info->type == XSLT_FUNC_VARIABLE) {
  2473. xsltStackElemPtr tmpvar = ctxt->vars;
  2474. xsltParseStylesheetVariable(ctxt, cur);
  2475. if (tmpvar != ctxt->vars) {
  2476. /*
  2477. * TODO: Using a @tmpvar is an annoying workaround, but
  2478. * the current mechanisms do not provide any other way
  2479. * of knowing if the var was really pushed onto the
  2480. * stack.
  2481. */
  2482. ctxt->vars->level = level;
  2483. }
  2484. } else if (info->type == XSLT_FUNC_MESSAGE) {
  2485. /*
  2486. * TODO: Won't be hit, since we don't compile xsl:message.
  2487. */
  2488. xsltMessage(ctxt, contextNode, cur);
  2489. } else {
  2490. xsltTransformError(ctxt, NULL, cur,
  2491. "Unexpected XSLT element '%s'.\n", cur->name);
  2492. }
  2493. goto skip_children;
  2494. } else {
  2495. xsltTransformFunction func;
  2496. /*
  2497. * Extension intructions (elements)
  2498. * --------------------------------------------------------
  2499. */
  2500. if (cur->psvi == xsltExtMarker) {
  2501. /*
  2502. * The xsltExtMarker was set during the compilation
  2503. * of extension instructions if there was no registered
  2504. * handler for this specific extension function at
  2505. * compile-time.
  2506. * Libxslt will now lookup if a handler is
  2507. * registered in the context of this transformation.
  2508. */
  2509. func = xsltExtElementLookup(ctxt, cur->name,
  2510. cur->ns->href);
  2511. } else
  2512. func = ((xsltElemPreCompPtr) cur->psvi)->func;
  2513. if (func == NULL) {
  2514. /*
  2515. * No handler available.
  2516. * Try to execute fallback behaviour via xsl:fallback.
  2517. */
  2518. #ifdef WITH_XSLT_DEBUG_PROCESS
  2519. XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
  2520. xsltGenericDebug(xsltGenericDebugContext,
  2521. "xsltApplySequenceConstructor: unknown extension %s\n",
  2522. cur->name));
  2523. #endif
  2524. ctxt->insert = insert;
  2525. if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
  2526. xsltTransformError(ctxt, NULL, cur,
  2527. "Unknown extension instruction '{%s}%s'.\n",
  2528. cur->ns->href, cur->name);
  2529. }
  2530. ctxt->insert = oldInsert;
  2531. } else {
  2532. /*
  2533. * Execute the handler-callback.
  2534. */
  2535. #ifdef WITH_XSLT_DEBUG_PROCESS
  2536. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2537. "xsltApplySequenceConstructor: extension construct %s\n",
  2538. cur->name));
  2539. #endif
  2540. /*
  2541. * Disable the xsltCopyTextString optimization for
  2542. * extension elements. Extensions could append text using
  2543. * xmlAddChild which will free the buffer pointed to by
  2544. * 'lasttext'. This buffer could later be reallocated with
  2545. * a different size than recorded in 'lasttsize'. See bug
  2546. * #777432.
  2547. */
  2548. if (cur->psvi == xsltExtMarker) {
  2549. ctxt->lasttext = NULL;
  2550. }
  2551. ctxt->insert = insert;
  2552. func(ctxt, contextNode, cur, cur->psvi);
  2553. /*
  2554. * Cleanup temporary tree fragments.
  2555. */
  2556. if (oldLocalFragmentTop != ctxt->localRVT)
  2557. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  2558. ctxt->insert = oldInsert;
  2559. }
  2560. goto skip_children;
  2561. }
  2562. } else if (XSLT_IS_TEXT_NODE(cur)) {
  2563. /*
  2564. * Text
  2565. * ------------------------------------------------------------
  2566. */
  2567. #ifdef WITH_XSLT_DEBUG_PROCESS
  2568. if (cur->name == xmlStringTextNoenc) {
  2569. XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
  2570. xsltGenericDebug(xsltGenericDebugContext,
  2571. "xsltApplySequenceConstructor: copy unescaped text '%s'\n",
  2572. cur->content));
  2573. } else {
  2574. XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
  2575. xsltGenericDebug(xsltGenericDebugContext,
  2576. "xsltApplySequenceConstructor: copy text '%s'\n",
  2577. cur->content));
  2578. }
  2579. #endif
  2580. if (xsltCopyText(ctxt, insert, cur, ctxt->internalized) == NULL)
  2581. goto error;
  2582. }
  2583. #else /* XSLT_REFACTORED */
  2584. if (IS_XSLT_ELEM(cur)) {
  2585. /*
  2586. * This is an XSLT node
  2587. */
  2588. xsltStylePreCompPtr info = (xsltStylePreCompPtr) cur->psvi;
  2589. if (info == NULL) {
  2590. if (IS_XSLT_NAME(cur, "message")) {
  2591. xsltMessage(ctxt, contextNode, cur);
  2592. } else {
  2593. /*
  2594. * That's an error try to apply one of the fallback cases
  2595. */
  2596. ctxt->insert = insert;
  2597. if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
  2598. xsltGenericError(xsltGenericErrorContext,
  2599. "xsltApplySequenceConstructor: %s was not compiled\n",
  2600. cur->name);
  2601. }
  2602. ctxt->insert = oldInsert;
  2603. }
  2604. goto skip_children;
  2605. }
  2606. if (info->func != NULL) {
  2607. oldCurInst = ctxt->inst;
  2608. ctxt->inst = cur;
  2609. ctxt->insert = insert;
  2610. info->func(ctxt, contextNode, cur, (xsltElemPreCompPtr) info);
  2611. /*
  2612. * Cleanup temporary tree fragments.
  2613. */
  2614. if (oldLocalFragmentTop != ctxt->localRVT)
  2615. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  2616. ctxt->insert = oldInsert;
  2617. ctxt->inst = oldCurInst;
  2618. goto skip_children;
  2619. }
  2620. if (IS_XSLT_NAME(cur, "variable")) {
  2621. xsltStackElemPtr tmpvar = ctxt->vars;
  2622. oldCurInst = ctxt->inst;
  2623. ctxt->inst = cur;
  2624. xsltParseStylesheetVariable(ctxt, cur);
  2625. ctxt->inst = oldCurInst;
  2626. if (tmpvar != ctxt->vars) {
  2627. /*
  2628. * TODO: Using a @tmpvar is an annoying workaround, but
  2629. * the current mechanisms do not provide any other way
  2630. * of knowing if the var was really pushed onto the
  2631. * stack.
  2632. */
  2633. ctxt->vars->level = level;
  2634. }
  2635. } else if (IS_XSLT_NAME(cur, "message")) {
  2636. xsltMessage(ctxt, contextNode, cur);
  2637. } else {
  2638. xsltTransformError(ctxt, NULL, cur,
  2639. "Unexpected XSLT element '%s'.\n", cur->name);
  2640. }
  2641. goto skip_children;
  2642. } else if ((cur->type == XML_TEXT_NODE) ||
  2643. (cur->type == XML_CDATA_SECTION_NODE)) {
  2644. /*
  2645. * This text comes from the stylesheet
  2646. * For stylesheets, the set of whitespace-preserving
  2647. * element names consists of just xsl:text.
  2648. */
  2649. #ifdef WITH_XSLT_DEBUG_PROCESS
  2650. if (cur->type == XML_CDATA_SECTION_NODE) {
  2651. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2652. "xsltApplySequenceConstructor: copy CDATA text %s\n",
  2653. cur->content));
  2654. } else if (cur->name == xmlStringTextNoenc) {
  2655. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2656. "xsltApplySequenceConstructor: copy unescaped text %s\n",
  2657. cur->content));
  2658. } else {
  2659. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2660. "xsltApplySequenceConstructor: copy text %s\n",
  2661. cur->content));
  2662. }
  2663. #endif
  2664. if (xsltCopyText(ctxt, insert, cur, ctxt->internalized) == NULL)
  2665. goto error;
  2666. } else if ((cur->type == XML_ELEMENT_NODE) &&
  2667. (cur->ns != NULL) && (cur->psvi != NULL)) {
  2668. xsltTransformFunction function;
  2669. oldCurInst = ctxt->inst;
  2670. ctxt->inst = cur;
  2671. /*
  2672. * Flagged as an extension element
  2673. */
  2674. if (cur->psvi == xsltExtMarker)
  2675. function = xsltExtElementLookup(ctxt, cur->name,
  2676. cur->ns->href);
  2677. else
  2678. function = ((xsltElemPreCompPtr) cur->psvi)->func;
  2679. if (function == NULL) {
  2680. xmlNodePtr child;
  2681. int found = 0;
  2682. #ifdef WITH_XSLT_DEBUG_PROCESS
  2683. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2684. "xsltApplySequenceConstructor: unknown extension %s\n",
  2685. cur->name));
  2686. #endif
  2687. /*
  2688. * Search if there are fallbacks
  2689. */
  2690. ctxt->insert = insert;
  2691. child = cur->children;
  2692. while (child != NULL) {
  2693. if ((IS_XSLT_ELEM(child)) &&
  2694. (IS_XSLT_NAME(child, "fallback")))
  2695. {
  2696. found = 1;
  2697. xsltApplySequenceConstructor(ctxt, contextNode,
  2698. child->children, NULL);
  2699. }
  2700. child = child->next;
  2701. }
  2702. ctxt->insert = oldInsert;
  2703. if (!found) {
  2704. xsltTransformError(ctxt, NULL, cur,
  2705. "xsltApplySequenceConstructor: failed to find extension %s\n",
  2706. cur->name);
  2707. }
  2708. } else {
  2709. #ifdef WITH_XSLT_DEBUG_PROCESS
  2710. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2711. "xsltApplySequenceConstructor: extension construct %s\n",
  2712. cur->name));
  2713. #endif
  2714. /*
  2715. * Disable the xsltCopyTextString optimization for
  2716. * extension elements. Extensions could append text using
  2717. * xmlAddChild which will free the buffer pointed to by
  2718. * 'lasttext'. This buffer could later be reallocated with
  2719. * a different size than recorded in 'lasttsize'. See bug
  2720. * #777432.
  2721. */
  2722. if (cur->psvi == xsltExtMarker) {
  2723. ctxt->lasttext = NULL;
  2724. }
  2725. ctxt->insert = insert;
  2726. function(ctxt, contextNode, cur, cur->psvi);
  2727. /*
  2728. * Cleanup temporary tree fragments.
  2729. */
  2730. if (oldLocalFragmentTop != ctxt->localRVT)
  2731. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  2732. ctxt->insert = oldInsert;
  2733. }
  2734. ctxt->inst = oldCurInst;
  2735. goto skip_children;
  2736. } else if (cur->type == XML_ELEMENT_NODE) {
  2737. #ifdef WITH_XSLT_DEBUG_PROCESS
  2738. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2739. "xsltApplySequenceConstructor: copy node %s\n",
  2740. cur->name));
  2741. #endif
  2742. oldCurInst = ctxt->inst;
  2743. ctxt->inst = cur;
  2744. if ((copy = xsltShallowCopyElem(ctxt, cur, insert, 1)) == NULL)
  2745. goto error;
  2746. /*
  2747. * Add extra namespaces inherited from the current template
  2748. * if we are in the first level children and this is a
  2749. * "real" template.
  2750. */
  2751. if ((templ != NULL) && (oldInsert == insert) &&
  2752. (ctxt->templ != NULL) && (ctxt->templ->inheritedNs != NULL)) {
  2753. int i;
  2754. xmlNsPtr ns, ret;
  2755. for (i = 0; i < ctxt->templ->inheritedNsNr; i++) {
  2756. const xmlChar *URI = NULL;
  2757. xsltStylesheetPtr style;
  2758. ns = ctxt->templ->inheritedNs[i];
  2759. /* Note that the XSLT namespace was already excluded
  2760. * in xsltGetInheritedNsList().
  2761. */
  2762. #if 0
  2763. if (xmlStrEqual(ns->href, XSLT_NAMESPACE))
  2764. continue;
  2765. #endif
  2766. style = ctxt->style;
  2767. while (style != NULL) {
  2768. if (style->nsAliases != NULL)
  2769. URI = (const xmlChar *)
  2770. xmlHashLookup(style->nsAliases, ns->href);
  2771. if (URI != NULL)
  2772. break;
  2773. style = xsltNextImport(style);
  2774. }
  2775. if (URI == UNDEFINED_DEFAULT_NS)
  2776. continue;
  2777. if (URI == NULL)
  2778. URI = ns->href;
  2779. /*
  2780. * TODO: The following will still be buggy for the
  2781. * non-refactored code.
  2782. */
  2783. ret = xmlSearchNs(copy->doc, copy, ns->prefix);
  2784. if ((ret == NULL) || (!xmlStrEqual(ret->href, URI)))
  2785. {
  2786. xmlNewNs(copy, URI, ns->prefix);
  2787. }
  2788. }
  2789. if (copy->ns != NULL) {
  2790. /*
  2791. * Fix the node namespace if needed
  2792. */
  2793. copy->ns = xsltGetNamespace(ctxt, cur, copy->ns, copy);
  2794. }
  2795. }
  2796. /*
  2797. * all the attributes are directly inherited
  2798. */
  2799. if (cur->properties != NULL) {
  2800. xsltAttrListTemplateProcess(ctxt, copy, cur->properties);
  2801. }
  2802. ctxt->inst = oldCurInst;
  2803. }
  2804. #endif /* else of XSLT_REFACTORED */
  2805. /*
  2806. * Descend into content in document order.
  2807. */
  2808. if (cur->children != NULL) {
  2809. if (cur->children->type != XML_ENTITY_DECL) {
  2810. cur = cur->children;
  2811. level++;
  2812. if (copy != NULL)
  2813. insert = copy;
  2814. continue;
  2815. }
  2816. }
  2817. skip_children:
  2818. /*
  2819. * If xslt:message was just processed, we might have hit a
  2820. * terminate='yes'; if so, then break the loop and clean up.
  2821. * TODO: Do we need to check this also before trying to descend
  2822. * into the content?
  2823. */
  2824. if (ctxt->state == XSLT_STATE_STOPPED)
  2825. break;
  2826. if (cur->next != NULL) {
  2827. cur = cur->next;
  2828. continue;
  2829. }
  2830. do {
  2831. cur = cur->parent;
  2832. level--;
  2833. /*
  2834. * Pop variables/params (xsl:variable and xsl:param).
  2835. */
  2836. if ((ctxt->varsNr > oldVarsNr) && (ctxt->vars->level > level)) {
  2837. xsltLocalVariablePop(ctxt, oldVarsNr, level);
  2838. }
  2839. insert = insert->parent;
  2840. if (cur == NULL)
  2841. break;
  2842. if (cur == list->parent) {
  2843. cur = NULL;
  2844. break;
  2845. }
  2846. if (cur->next != NULL) {
  2847. cur = cur->next;
  2848. break;
  2849. }
  2850. } while (cur != NULL);
  2851. }
  2852. error:
  2853. /*
  2854. * In case of errors: pop remaining variables.
  2855. */
  2856. if (ctxt->varsNr > oldVarsNr)
  2857. xsltLocalVariablePop(ctxt, oldVarsNr, -1);
  2858. ctxt->node = oldContextNode;
  2859. ctxt->inst = oldInst;
  2860. ctxt->insert = oldInsert;
  2861. ctxt->depth--;
  2862. #ifdef WITH_DEBUGGER
  2863. if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (addCallResult)) {
  2864. xslDropCall();
  2865. }
  2866. #endif
  2867. }
  2868. /*
  2869. * xsltApplyXSLTTemplate:
  2870. * @ctxt: a XSLT transformation context
  2871. * @contextNode: the node in the source tree.
  2872. * @list: the nodes of a sequence constructor;
  2873. * (plus leading xsl:param elements)
  2874. * @templ: the compiled xsl:template declaration;
  2875. * NULL if a sequence constructor
  2876. * @withParams: a set of caller-parameters (xsl:with-param) or NULL
  2877. *
  2878. * Called by:
  2879. * - xsltApplyImports()
  2880. * - xsltCallTemplate()
  2881. * - xsltDefaultProcessOneNode()
  2882. * - xsltProcessOneNode()
  2883. */
  2884. static void
  2885. xsltApplyXSLTTemplate(xsltTransformContextPtr ctxt,
  2886. xmlNodePtr contextNode,
  2887. xmlNodePtr list,
  2888. xsltTemplatePtr templ,
  2889. xsltStackElemPtr withParams)
  2890. {
  2891. int oldVarsBase = 0;
  2892. xmlNodePtr cur;
  2893. xsltStackElemPtr tmpParam = NULL;
  2894. xmlDocPtr oldUserFragmentTop;
  2895. #ifdef WITH_PROFILER
  2896. long start = 0;
  2897. #endif
  2898. #ifdef XSLT_REFACTORED
  2899. xsltStyleItemParamPtr iparam;
  2900. #else
  2901. xsltStylePreCompPtr iparam;
  2902. #endif
  2903. #ifdef WITH_DEBUGGER
  2904. int addCallResult = 0;
  2905. #endif
  2906. if (ctxt == NULL)
  2907. return;
  2908. if (templ == NULL) {
  2909. xsltTransformError(ctxt, NULL, list,
  2910. "xsltApplyXSLTTemplate: Bad arguments; @templ is mandatory.\n");
  2911. return;
  2912. }
  2913. #ifdef WITH_DEBUGGER
  2914. if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
  2915. if (xsltDebuggerStartSequenceConstructor(ctxt, contextNode,
  2916. list, templ, &addCallResult) == NULL)
  2917. return;
  2918. }
  2919. #endif
  2920. if (list == NULL)
  2921. return;
  2922. CHECK_STOPPED;
  2923. if (ctxt->varsNr >= ctxt->maxTemplateVars)
  2924. {
  2925. xsltTransformError(ctxt, NULL, list,
  2926. "xsltApplyXSLTTemplate: A potential infinite template recursion "
  2927. "was detected.\n"
  2928. "You can adjust maxTemplateVars (--maxvars) in order to "
  2929. "raise the maximum number of variables/params (currently set to %d).\n",
  2930. ctxt->maxTemplateVars);
  2931. xsltDebug(ctxt, contextNode, list, NULL);
  2932. ctxt->state = XSLT_STATE_STOPPED;
  2933. return;
  2934. }
  2935. oldUserFragmentTop = ctxt->tmpRVT;
  2936. ctxt->tmpRVT = NULL;
  2937. /*
  2938. * Initiate a distinct scope of local params/variables.
  2939. */
  2940. oldVarsBase = ctxt->varsBase;
  2941. ctxt->varsBase = ctxt->varsNr;
  2942. ctxt->node = contextNode;
  2943. #ifdef WITH_PROFILER
  2944. if (ctxt->profile) {
  2945. templ->nbCalls++;
  2946. start = xsltTimestamp();
  2947. profPush(ctxt, 0);
  2948. profCallgraphAdd(templ, ctxt->templ);
  2949. }
  2950. #endif
  2951. /*
  2952. * Push the xsl:template declaration onto the stack.
  2953. */
  2954. templPush(ctxt, templ);
  2955. #ifdef WITH_XSLT_DEBUG_PROCESS
  2956. if (templ->name != NULL)
  2957. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2958. "applying xsl:template '%s'\n", templ->name));
  2959. #endif
  2960. /*
  2961. * Process xsl:param instructions and skip those elements for
  2962. * further processing.
  2963. */
  2964. cur = list;
  2965. do {
  2966. if (cur->type == XML_TEXT_NODE) {
  2967. cur = cur->next;
  2968. continue;
  2969. }
  2970. if ((cur->type != XML_ELEMENT_NODE) ||
  2971. (cur->name[0] != 'p') ||
  2972. (cur->psvi == NULL) ||
  2973. (! xmlStrEqual(cur->name, BAD_CAST "param")) ||
  2974. (! IS_XSLT_ELEM(cur)))
  2975. {
  2976. break;
  2977. }
  2978. list = cur->next;
  2979. #ifdef XSLT_REFACTORED
  2980. iparam = (xsltStyleItemParamPtr) cur->psvi;
  2981. #else
  2982. iparam = (xsltStylePreCompPtr) cur->psvi;
  2983. #endif
  2984. /*
  2985. * Substitute xsl:param for a given xsl:with-param.
  2986. * Since the XPath expression will reference the params/vars
  2987. * by index, we need to slot the xsl:with-params in the
  2988. * order of encountered xsl:params to keep the sequence of
  2989. * params/variables in the stack exactly as it was at
  2990. * compile time,
  2991. */
  2992. tmpParam = NULL;
  2993. if (withParams) {
  2994. tmpParam = withParams;
  2995. do {
  2996. if ((tmpParam->name == (iparam->name)) &&
  2997. (tmpParam->nameURI == (iparam->ns)))
  2998. {
  2999. /*
  3000. * Push the caller-parameter.
  3001. */
  3002. xsltLocalVariablePush(ctxt, tmpParam, -1);
  3003. break;
  3004. }
  3005. tmpParam = tmpParam->next;
  3006. } while (tmpParam != NULL);
  3007. }
  3008. /*
  3009. * Push the xsl:param.
  3010. */
  3011. if (tmpParam == NULL) {
  3012. /*
  3013. * Note that we must assume that the added parameter
  3014. * has a @depth of 0.
  3015. */
  3016. xsltParseStylesheetParam(ctxt, cur);
  3017. }
  3018. cur = cur->next;
  3019. } while (cur != NULL);
  3020. /*
  3021. * Process the sequence constructor.
  3022. */
  3023. xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
  3024. /*
  3025. * Remove remaining xsl:param and xsl:with-param items from
  3026. * the stack. Don't free xsl:with-param items.
  3027. */
  3028. if (ctxt->varsNr > ctxt->varsBase)
  3029. xsltTemplateParamsCleanup(ctxt);
  3030. ctxt->varsBase = oldVarsBase;
  3031. /*
  3032. * Release user-created fragments stored in the scope
  3033. * of xsl:template. Note that this mechanism is deprecated:
  3034. * user code should now use xsltRegisterLocalRVT() instead
  3035. * of the obsolete xsltRegisterTmpRVT().
  3036. */
  3037. if (ctxt->tmpRVT) {
  3038. xmlDocPtr curdoc = ctxt->tmpRVT, tmp;
  3039. while (curdoc != NULL) {
  3040. tmp = curdoc;
  3041. curdoc = (xmlDocPtr) curdoc->next;
  3042. xsltReleaseRVT(ctxt, tmp);
  3043. }
  3044. }
  3045. ctxt->tmpRVT = oldUserFragmentTop;
  3046. /*
  3047. * Pop the xsl:template declaration from the stack.
  3048. */
  3049. templPop(ctxt);
  3050. #ifdef WITH_PROFILER
  3051. if (ctxt->profile) {
  3052. long spent, child, total, end;
  3053. end = xsltTimestamp();
  3054. child = profPop(ctxt);
  3055. total = end - start;
  3056. spent = total - child;
  3057. if (spent <= 0) {
  3058. /*
  3059. * Not possible unless the original calibration failed
  3060. * we can try to correct it on the fly.
  3061. */
  3062. xsltCalibrateAdjust(spent);
  3063. spent = 0;
  3064. }
  3065. templ->time += spent;
  3066. if (ctxt->profNr > 0)
  3067. ctxt->profTab[ctxt->profNr - 1] += total;
  3068. }
  3069. #endif
  3070. #ifdef WITH_DEBUGGER
  3071. if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (addCallResult)) {
  3072. xslDropCall();
  3073. }
  3074. #endif
  3075. }
  3076. /**
  3077. * xsltApplyOneTemplate:
  3078. * @ctxt: a XSLT process context
  3079. * @contextNode: the node in the source tree.
  3080. * @list: the nodes of a sequence constructor
  3081. * @templ: not used
  3082. * @params: a set of parameters (xsl:param) or NULL
  3083. *
  3084. * Processes a sequence constructor on the current node in the source tree.
  3085. *
  3086. * @params are the already computed variable stack items; this function
  3087. * pushes them on the variable stack, and pops them before exiting; it's
  3088. * left to the caller to free or reuse @params afterwards. The initial
  3089. * states of the variable stack will always be restored before this
  3090. * function exits.
  3091. * NOTE that this does *not* initiate a new distinct variable scope; i.e.
  3092. * variables already on the stack are visible to the process. The caller's
  3093. * side needs to start a new variable scope if needed (e.g. in exsl:function).
  3094. *
  3095. * @templ is obsolete and not used anymore (e.g. <exslt:function> does not
  3096. * provide a @templ); a non-NULL @templ might raise an error in the future.
  3097. *
  3098. * BIG NOTE: This function is not intended to process the content of an
  3099. * xsl:template; it does not expect xsl:param instructions in @list and
  3100. * will report errors if found.
  3101. *
  3102. * Called by:
  3103. * - xsltEvalVariable() (variables.c)
  3104. * - exsltFuncFunctionFunction() (libexsl/functions.c)
  3105. */
  3106. void
  3107. xsltApplyOneTemplate(xsltTransformContextPtr ctxt,
  3108. xmlNodePtr contextNode,
  3109. xmlNodePtr list,
  3110. xsltTemplatePtr templ ATTRIBUTE_UNUSED,
  3111. xsltStackElemPtr params)
  3112. {
  3113. if ((ctxt == NULL) || (list == NULL))
  3114. return;
  3115. CHECK_STOPPED;
  3116. if (params) {
  3117. /*
  3118. * This code should be obsolete - was previously used
  3119. * by libexslt/functions.c, but due to bug 381319 the
  3120. * logic there was changed.
  3121. */
  3122. int oldVarsNr = ctxt->varsNr;
  3123. /*
  3124. * Push the given xsl:param(s) onto the variable stack.
  3125. */
  3126. while (params != NULL) {
  3127. xsltLocalVariablePush(ctxt, params, -1);
  3128. params = params->next;
  3129. }
  3130. xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
  3131. /*
  3132. * Pop the given xsl:param(s) from the stack but don't free them.
  3133. */
  3134. xsltLocalVariablePop(ctxt, oldVarsNr, -2);
  3135. } else
  3136. xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
  3137. }
  3138. /************************************************************************
  3139. * *
  3140. * XSLT-1.1 extensions *
  3141. * *
  3142. ************************************************************************/
  3143. /**
  3144. * xsltDocumentElem:
  3145. * @ctxt: an XSLT processing context
  3146. * @node: The current node
  3147. * @inst: the instruction in the stylesheet
  3148. * @castedComp: precomputed information
  3149. *
  3150. * Process an EXSLT/XSLT-1.1 document element
  3151. */
  3152. void
  3153. xsltDocumentElem(xsltTransformContextPtr ctxt, xmlNodePtr node,
  3154. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  3155. {
  3156. #ifdef XSLT_REFACTORED
  3157. xsltStyleItemDocumentPtr comp = (xsltStyleItemDocumentPtr) castedComp;
  3158. #else
  3159. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  3160. #endif
  3161. xsltStylesheetPtr style = NULL;
  3162. int ret;
  3163. xmlChar *filename = NULL, *prop, *elements;
  3164. xmlChar *element, *end;
  3165. xmlDocPtr res = NULL;
  3166. xmlDocPtr oldOutput;
  3167. xmlNodePtr oldInsert, root;
  3168. const char *oldOutputFile;
  3169. xsltOutputType oldType;
  3170. xmlChar *URL = NULL;
  3171. const xmlChar *method;
  3172. const xmlChar *doctypePublic;
  3173. const xmlChar *doctypeSystem;
  3174. const xmlChar *version;
  3175. const xmlChar *encoding;
  3176. int redirect_write_append = 0;
  3177. if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
  3178. return;
  3179. if (comp->filename == NULL) {
  3180. if (xmlStrEqual(inst->name, (const xmlChar *) "output")) {
  3181. /*
  3182. * The element "output" is in the namespace XSLT_SAXON_NAMESPACE
  3183. * (http://icl.com/saxon)
  3184. * The @file is in no namespace.
  3185. */
  3186. #ifdef WITH_XSLT_DEBUG_EXTRA
  3187. xsltGenericDebug(xsltGenericDebugContext,
  3188. "Found saxon:output extension\n");
  3189. #endif
  3190. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3191. (const xmlChar *) "file",
  3192. XSLT_SAXON_NAMESPACE);
  3193. if (URL == NULL)
  3194. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3195. (const xmlChar *) "href",
  3196. XSLT_SAXON_NAMESPACE);
  3197. } else if (xmlStrEqual(inst->name, (const xmlChar *) "write")) {
  3198. #ifdef WITH_XSLT_DEBUG_EXTRA
  3199. xsltGenericDebug(xsltGenericDebugContext,
  3200. "Found xalan:write extension\n");
  3201. #endif
  3202. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3203. (const xmlChar *)
  3204. "select",
  3205. XSLT_XALAN_NAMESPACE);
  3206. if (URL != NULL) {
  3207. xmlXPathCompExprPtr cmp;
  3208. xmlChar *val;
  3209. /*
  3210. * Trying to handle bug #59212
  3211. * The value of the "select" attribute is an
  3212. * XPath expression.
  3213. * (see http://xml.apache.org/xalan-j/extensionslib.html#redirect)
  3214. */
  3215. cmp = xmlXPathCtxtCompile(ctxt->xpathCtxt, URL);
  3216. val = xsltEvalXPathString(ctxt, cmp);
  3217. xmlXPathFreeCompExpr(cmp);
  3218. xmlFree(URL);
  3219. URL = val;
  3220. }
  3221. if (URL == NULL)
  3222. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3223. (const xmlChar *)
  3224. "file",
  3225. XSLT_XALAN_NAMESPACE);
  3226. if (URL == NULL)
  3227. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3228. (const xmlChar *)
  3229. "href",
  3230. XSLT_XALAN_NAMESPACE);
  3231. } else if (xmlStrEqual(inst->name, (const xmlChar *) "document")) {
  3232. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3233. (const xmlChar *) "href",
  3234. NULL);
  3235. }
  3236. } else {
  3237. URL = xmlStrdup(comp->filename);
  3238. }
  3239. if (URL == NULL) {
  3240. xsltTransformError(ctxt, NULL, inst,
  3241. "xsltDocumentElem: href/URI-Reference not found\n");
  3242. return;
  3243. }
  3244. /*
  3245. * If the computation failed, it's likely that the URL wasn't escaped
  3246. */
  3247. filename = xmlBuildURI(URL, (const xmlChar *) ctxt->outputFile);
  3248. if (filename == NULL) {
  3249. xmlChar *escURL;
  3250. escURL=xmlURIEscapeStr(URL, BAD_CAST ":/.?,");
  3251. if (escURL != NULL) {
  3252. filename = xmlBuildURI(escURL, (const xmlChar *) ctxt->outputFile);
  3253. xmlFree(escURL);
  3254. }
  3255. }
  3256. if (filename == NULL) {
  3257. xsltTransformError(ctxt, NULL, inst,
  3258. "xsltDocumentElem: URL computation failed for %s\n",
  3259. URL);
  3260. xmlFree(URL);
  3261. return;
  3262. }
  3263. /*
  3264. * Security checking: can we write to this resource
  3265. */
  3266. if (ctxt->sec != NULL) {
  3267. ret = xsltCheckWrite(ctxt->sec, ctxt, filename);
  3268. if (ret <= 0) {
  3269. if (ret == 0)
  3270. xsltTransformError(ctxt, NULL, inst,
  3271. "xsltDocumentElem: write rights for %s denied\n",
  3272. filename);
  3273. xmlFree(URL);
  3274. xmlFree(filename);
  3275. return;
  3276. }
  3277. }
  3278. oldOutputFile = ctxt->outputFile;
  3279. oldOutput = ctxt->output;
  3280. oldInsert = ctxt->insert;
  3281. oldType = ctxt->type;
  3282. ctxt->outputFile = (const char *) filename;
  3283. style = xsltNewStylesheet();
  3284. if (style == NULL) {
  3285. xsltTransformError(ctxt, NULL, inst,
  3286. "xsltDocumentElem: out of memory\n");
  3287. goto error;
  3288. }
  3289. /*
  3290. * Version described in 1.1 draft allows full parameterization
  3291. * of the output.
  3292. */
  3293. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3294. (const xmlChar *) "version",
  3295. NULL);
  3296. if (prop != NULL) {
  3297. if (style->version != NULL)
  3298. xmlFree(style->version);
  3299. style->version = prop;
  3300. }
  3301. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3302. (const xmlChar *) "encoding",
  3303. NULL);
  3304. if (prop != NULL) {
  3305. if (style->encoding != NULL)
  3306. xmlFree(style->encoding);
  3307. style->encoding = prop;
  3308. }
  3309. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3310. (const xmlChar *) "method",
  3311. NULL);
  3312. if (prop != NULL) {
  3313. const xmlChar *URI;
  3314. if (style->method != NULL)
  3315. xmlFree(style->method);
  3316. style->method = NULL;
  3317. if (style->methodURI != NULL)
  3318. xmlFree(style->methodURI);
  3319. style->methodURI = NULL;
  3320. URI = xsltGetQNameURI(inst, &prop);
  3321. if (prop == NULL) {
  3322. if (style != NULL) style->errors++;
  3323. } else if (URI == NULL) {
  3324. if ((xmlStrEqual(prop, (const xmlChar *) "xml")) ||
  3325. (xmlStrEqual(prop, (const xmlChar *) "html")) ||
  3326. (xmlStrEqual(prop, (const xmlChar *) "text"))) {
  3327. style->method = prop;
  3328. } else {
  3329. xsltTransformError(ctxt, NULL, inst,
  3330. "invalid value for method: %s\n", prop);
  3331. if (style != NULL) style->warnings++;
  3332. }
  3333. } else {
  3334. style->method = prop;
  3335. style->methodURI = xmlStrdup(URI);
  3336. }
  3337. }
  3338. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3339. (const xmlChar *)
  3340. "doctype-system", NULL);
  3341. if (prop != NULL) {
  3342. if (style->doctypeSystem != NULL)
  3343. xmlFree(style->doctypeSystem);
  3344. style->doctypeSystem = prop;
  3345. }
  3346. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3347. (const xmlChar *)
  3348. "doctype-public", NULL);
  3349. if (prop != NULL) {
  3350. if (style->doctypePublic != NULL)
  3351. xmlFree(style->doctypePublic);
  3352. style->doctypePublic = prop;
  3353. }
  3354. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3355. (const xmlChar *) "standalone",
  3356. NULL);
  3357. if (prop != NULL) {
  3358. if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
  3359. style->standalone = 1;
  3360. } else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
  3361. style->standalone = 0;
  3362. } else {
  3363. xsltTransformError(ctxt, NULL, inst,
  3364. "invalid value for standalone: %s\n",
  3365. prop);
  3366. if (style != NULL) style->warnings++;
  3367. }
  3368. xmlFree(prop);
  3369. }
  3370. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3371. (const xmlChar *) "indent",
  3372. NULL);
  3373. if (prop != NULL) {
  3374. if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
  3375. style->indent = 1;
  3376. } else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
  3377. style->indent = 0;
  3378. } else {
  3379. xsltTransformError(ctxt, NULL, inst,
  3380. "invalid value for indent: %s\n", prop);
  3381. if (style != NULL) style->warnings++;
  3382. }
  3383. xmlFree(prop);
  3384. }
  3385. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3386. (const xmlChar *)
  3387. "omit-xml-declaration",
  3388. NULL);
  3389. if (prop != NULL) {
  3390. if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
  3391. style->omitXmlDeclaration = 1;
  3392. } else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
  3393. style->omitXmlDeclaration = 0;
  3394. } else {
  3395. xsltTransformError(ctxt, NULL, inst,
  3396. "invalid value for omit-xml-declaration: %s\n",
  3397. prop);
  3398. if (style != NULL) style->warnings++;
  3399. }
  3400. xmlFree(prop);
  3401. }
  3402. elements = xsltEvalAttrValueTemplate(ctxt, inst,
  3403. (const xmlChar *)
  3404. "cdata-section-elements",
  3405. NULL);
  3406. if (elements != NULL) {
  3407. if (style->stripSpaces == NULL)
  3408. style->stripSpaces = xmlHashCreate(10);
  3409. if (style->stripSpaces == NULL)
  3410. return;
  3411. element = elements;
  3412. while (*element != 0) {
  3413. while (IS_BLANK_CH(*element))
  3414. element++;
  3415. if (*element == 0)
  3416. break;
  3417. end = element;
  3418. while ((*end != 0) && (!IS_BLANK_CH(*end)))
  3419. end++;
  3420. element = xmlStrndup(element, end - element);
  3421. if (element) {
  3422. const xmlChar *URI;
  3423. #ifdef WITH_XSLT_DEBUG_PARSING
  3424. xsltGenericDebug(xsltGenericDebugContext,
  3425. "add cdata section output element %s\n",
  3426. element);
  3427. #endif
  3428. URI = xsltGetQNameURI(inst, &element);
  3429. xmlHashAddEntry2(style->stripSpaces, element, URI,
  3430. (xmlChar *) "cdata");
  3431. xmlFree(element);
  3432. }
  3433. element = end;
  3434. }
  3435. xmlFree(elements);
  3436. }
  3437. /*
  3438. * Create a new document tree and process the element template
  3439. */
  3440. XSLT_GET_IMPORT_PTR(method, style, method)
  3441. XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
  3442. XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
  3443. XSLT_GET_IMPORT_PTR(version, style, version)
  3444. XSLT_GET_IMPORT_PTR(encoding, style, encoding)
  3445. if ((method != NULL) &&
  3446. (!xmlStrEqual(method, (const xmlChar *) "xml"))) {
  3447. if (xmlStrEqual(method, (const xmlChar *) "html")) {
  3448. ctxt->type = XSLT_OUTPUT_HTML;
  3449. if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
  3450. res = htmlNewDoc(doctypeSystem, doctypePublic);
  3451. else {
  3452. if (version != NULL) {
  3453. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  3454. xsltGetHTMLIDs(version, &doctypePublic, &doctypeSystem);
  3455. #endif
  3456. }
  3457. res = htmlNewDocNoDtD(doctypeSystem, doctypePublic);
  3458. }
  3459. if (res == NULL)
  3460. goto error;
  3461. res->dict = ctxt->dict;
  3462. xmlDictReference(res->dict);
  3463. } else if (xmlStrEqual(method, (const xmlChar *) "xhtml")) {
  3464. xsltTransformError(ctxt, NULL, inst,
  3465. "xsltDocumentElem: unsupported method xhtml\n");
  3466. ctxt->type = XSLT_OUTPUT_HTML;
  3467. res = htmlNewDocNoDtD(doctypeSystem, doctypePublic);
  3468. if (res == NULL)
  3469. goto error;
  3470. res->dict = ctxt->dict;
  3471. xmlDictReference(res->dict);
  3472. } else if (xmlStrEqual(method, (const xmlChar *) "text")) {
  3473. ctxt->type = XSLT_OUTPUT_TEXT;
  3474. res = xmlNewDoc(style->version);
  3475. if (res == NULL)
  3476. goto error;
  3477. res->dict = ctxt->dict;
  3478. xmlDictReference(res->dict);
  3479. #ifdef WITH_XSLT_DEBUG
  3480. xsltGenericDebug(xsltGenericDebugContext,
  3481. "reusing transformation dict for output\n");
  3482. #endif
  3483. } else {
  3484. xsltTransformError(ctxt, NULL, inst,
  3485. "xsltDocumentElem: unsupported method (%s)\n",
  3486. method);
  3487. goto error;
  3488. }
  3489. } else {
  3490. ctxt->type = XSLT_OUTPUT_XML;
  3491. res = xmlNewDoc(style->version);
  3492. if (res == NULL)
  3493. goto error;
  3494. res->dict = ctxt->dict;
  3495. xmlDictReference(res->dict);
  3496. #ifdef WITH_XSLT_DEBUG
  3497. xsltGenericDebug(xsltGenericDebugContext,
  3498. "reusing transformation dict for output\n");
  3499. #endif
  3500. }
  3501. res->charset = XML_CHAR_ENCODING_UTF8;
  3502. if (encoding != NULL)
  3503. res->encoding = xmlStrdup(encoding);
  3504. ctxt->output = res;
  3505. ctxt->insert = (xmlNodePtr) res;
  3506. xsltApplySequenceConstructor(ctxt, node, inst->children, NULL);
  3507. /*
  3508. * Do some post processing work depending on the generated output
  3509. */
  3510. root = xmlDocGetRootElement(res);
  3511. if (root != NULL) {
  3512. const xmlChar *doctype = NULL;
  3513. if ((root->ns != NULL) && (root->ns->prefix != NULL))
  3514. doctype = xmlDictQLookup(ctxt->dict, root->ns->prefix, root->name);
  3515. if (doctype == NULL)
  3516. doctype = root->name;
  3517. /*
  3518. * Apply the default selection of the method
  3519. */
  3520. if ((method == NULL) &&
  3521. (root->ns == NULL) &&
  3522. (!xmlStrcasecmp(root->name, (const xmlChar *) "html"))) {
  3523. xmlNodePtr tmp;
  3524. tmp = res->children;
  3525. while ((tmp != NULL) && (tmp != root)) {
  3526. if (tmp->type == XML_ELEMENT_NODE)
  3527. break;
  3528. if ((tmp->type == XML_TEXT_NODE) && (!xmlIsBlankNode(tmp)))
  3529. break;
  3530. tmp = tmp->next;
  3531. }
  3532. if (tmp == root) {
  3533. ctxt->type = XSLT_OUTPUT_HTML;
  3534. res->type = XML_HTML_DOCUMENT_NODE;
  3535. if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
  3536. res->intSubset = xmlCreateIntSubset(res, doctype,
  3537. doctypePublic,
  3538. doctypeSystem);
  3539. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  3540. } else if (version != NULL) {
  3541. xsltGetHTMLIDs(version, &doctypePublic,
  3542. &doctypeSystem);
  3543. if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
  3544. res->intSubset =
  3545. xmlCreateIntSubset(res, doctype,
  3546. doctypePublic,
  3547. doctypeSystem);
  3548. #endif
  3549. }
  3550. }
  3551. }
  3552. if (ctxt->type == XSLT_OUTPUT_XML) {
  3553. XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
  3554. XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
  3555. if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
  3556. res->intSubset = xmlCreateIntSubset(res, doctype,
  3557. doctypePublic,
  3558. doctypeSystem);
  3559. }
  3560. }
  3561. /*
  3562. * Calls to redirect:write also take an optional attribute append.
  3563. * Attribute append="true|yes" which will attempt to simply append
  3564. * to an existing file instead of always opening a new file. The
  3565. * default behavior of always overwriting the file still happens
  3566. * if we do not specify append.
  3567. * Note that append use will forbid use of remote URI target.
  3568. */
  3569. prop = xsltEvalAttrValueTemplate(ctxt, inst, (const xmlChar *)"append",
  3570. NULL);
  3571. if (prop != NULL) {
  3572. if (xmlStrEqual(prop, (const xmlChar *) "true") ||
  3573. xmlStrEqual(prop, (const xmlChar *) "yes")) {
  3574. style->omitXmlDeclaration = 1;
  3575. redirect_write_append = 1;
  3576. } else
  3577. style->omitXmlDeclaration = 0;
  3578. xmlFree(prop);
  3579. }
  3580. if (redirect_write_append) {
  3581. FILE *f;
  3582. f = fopen((const char *) filename, "ab");
  3583. if (f == NULL) {
  3584. ret = -1;
  3585. } else {
  3586. ret = xsltSaveResultToFile(f, res, style);
  3587. fclose(f);
  3588. }
  3589. } else {
  3590. ret = xsltSaveResultToFilename((const char *) filename, res, style, 0);
  3591. }
  3592. if (ret < 0) {
  3593. xsltTransformError(ctxt, NULL, inst,
  3594. "xsltDocumentElem: unable to save to %s\n",
  3595. filename);
  3596. #ifdef WITH_XSLT_DEBUG_EXTRA
  3597. } else {
  3598. xsltGenericDebug(xsltGenericDebugContext,
  3599. "Wrote %d bytes to %s\n", ret, filename);
  3600. #endif
  3601. }
  3602. error:
  3603. ctxt->output = oldOutput;
  3604. ctxt->insert = oldInsert;
  3605. ctxt->type = oldType;
  3606. ctxt->outputFile = oldOutputFile;
  3607. if (URL != NULL)
  3608. xmlFree(URL);
  3609. if (filename != NULL)
  3610. xmlFree(filename);
  3611. if (style != NULL)
  3612. xsltFreeStylesheet(style);
  3613. if (res != NULL)
  3614. xmlFreeDoc(res);
  3615. }
  3616. /************************************************************************
  3617. * *
  3618. * Most of the XSLT-1.0 transformations *
  3619. * *
  3620. ************************************************************************/
  3621. /**
  3622. * xsltSort:
  3623. * @ctxt: a XSLT process context
  3624. * @node: the node in the source tree.
  3625. * @inst: the xslt sort node
  3626. * @comp: precomputed information
  3627. *
  3628. * function attached to xsl:sort nodes, but this should not be
  3629. * called directly
  3630. */
  3631. void
  3632. xsltSort(xsltTransformContextPtr ctxt,
  3633. xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst,
  3634. xsltElemPreCompPtr comp) {
  3635. if (comp == NULL) {
  3636. xsltTransformError(ctxt, NULL, inst,
  3637. "xsl:sort : compilation failed\n");
  3638. return;
  3639. }
  3640. xsltTransformError(ctxt, NULL, inst,
  3641. "xsl:sort : improper use this should not be reached\n");
  3642. }
  3643. /**
  3644. * xsltCopy:
  3645. * @ctxt: an XSLT process context
  3646. * @node: the node in the source tree
  3647. * @inst: the element node of the XSLT-copy instruction
  3648. * @castedComp: computed information of the XSLT-copy instruction
  3649. *
  3650. * Execute the XSLT-copy instruction on the source node.
  3651. */
  3652. void
  3653. xsltCopy(xsltTransformContextPtr ctxt, xmlNodePtr node,
  3654. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  3655. {
  3656. #ifdef XSLT_REFACTORED
  3657. xsltStyleItemCopyPtr comp = (xsltStyleItemCopyPtr) castedComp;
  3658. #else
  3659. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  3660. #endif
  3661. xmlNodePtr copy, oldInsert;
  3662. oldInsert = ctxt->insert;
  3663. if (ctxt->insert != NULL) {
  3664. switch (node->type) {
  3665. case XML_TEXT_NODE:
  3666. case XML_CDATA_SECTION_NODE:
  3667. /*
  3668. * This text comes from the stylesheet
  3669. * For stylesheets, the set of whitespace-preserving
  3670. * element names consists of just xsl:text.
  3671. */
  3672. #ifdef WITH_XSLT_DEBUG_PROCESS
  3673. if (node->type == XML_CDATA_SECTION_NODE) {
  3674. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3675. "xsltCopy: CDATA text %s\n", node->content));
  3676. } else {
  3677. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3678. "xsltCopy: text %s\n", node->content));
  3679. }
  3680. #endif
  3681. xsltCopyText(ctxt, ctxt->insert, node, 0);
  3682. break;
  3683. case XML_DOCUMENT_NODE:
  3684. case XML_HTML_DOCUMENT_NODE:
  3685. break;
  3686. case XML_ELEMENT_NODE:
  3687. /*
  3688. * REVISIT NOTE: The "fake" is a doc-node, not an element node.
  3689. * REMOVED:
  3690. * if (xmlStrEqual(node->name, BAD_CAST " fake node libxslt"))
  3691. * return;
  3692. */
  3693. #ifdef WITH_XSLT_DEBUG_PROCESS
  3694. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3695. "xsltCopy: node %s\n", node->name));
  3696. #endif
  3697. copy = xsltShallowCopyElem(ctxt, node, ctxt->insert, 0);
  3698. ctxt->insert = copy;
  3699. if (comp->use != NULL) {
  3700. xsltApplyAttributeSet(ctxt, node, inst, comp->use);
  3701. }
  3702. break;
  3703. case XML_ATTRIBUTE_NODE: {
  3704. #ifdef WITH_XSLT_DEBUG_PROCESS
  3705. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3706. "xsltCopy: attribute %s\n", node->name));
  3707. #endif
  3708. /*
  3709. * REVISIT: We could also raise an error if the parent is not
  3710. * an element node.
  3711. * OPTIMIZE TODO: Can we set the value/children of the
  3712. * attribute without an intermediate copy of the string value?
  3713. */
  3714. xsltShallowCopyAttr(ctxt, inst, ctxt->insert, (xmlAttrPtr) node);
  3715. break;
  3716. }
  3717. case XML_PI_NODE:
  3718. #ifdef WITH_XSLT_DEBUG_PROCESS
  3719. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3720. "xsltCopy: PI %s\n", node->name));
  3721. #endif
  3722. copy = xmlNewDocPI(ctxt->insert->doc, node->name,
  3723. node->content);
  3724. copy = xsltAddChild(ctxt->insert, copy);
  3725. break;
  3726. case XML_COMMENT_NODE:
  3727. #ifdef WITH_XSLT_DEBUG_PROCESS
  3728. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3729. "xsltCopy: comment\n"));
  3730. #endif
  3731. copy = xmlNewComment(node->content);
  3732. copy = xsltAddChild(ctxt->insert, copy);
  3733. break;
  3734. case XML_NAMESPACE_DECL:
  3735. #ifdef WITH_XSLT_DEBUG_PROCESS
  3736. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3737. "xsltCopy: namespace declaration\n"));
  3738. #endif
  3739. xsltShallowCopyNsNode(ctxt, inst, ctxt->insert, (xmlNsPtr)node);
  3740. break;
  3741. default:
  3742. break;
  3743. }
  3744. }
  3745. switch (node->type) {
  3746. case XML_DOCUMENT_NODE:
  3747. case XML_HTML_DOCUMENT_NODE:
  3748. case XML_ELEMENT_NODE:
  3749. xsltApplySequenceConstructor(ctxt, ctxt->node, inst->children,
  3750. NULL);
  3751. break;
  3752. default:
  3753. break;
  3754. }
  3755. ctxt->insert = oldInsert;
  3756. }
  3757. /**
  3758. * xsltText:
  3759. * @ctxt: a XSLT process context
  3760. * @node: the node in the source tree.
  3761. * @inst: the xslt text node
  3762. * @comp: precomputed information
  3763. *
  3764. * Process the xslt text node on the source node
  3765. */
  3766. void
  3767. xsltText(xsltTransformContextPtr ctxt, xmlNodePtr node ATTRIBUTE_UNUSED,
  3768. xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED) {
  3769. if ((inst->children != NULL) && (comp != NULL)) {
  3770. xmlNodePtr text = inst->children;
  3771. xmlNodePtr copy;
  3772. while (text != NULL) {
  3773. if ((text->type != XML_TEXT_NODE) &&
  3774. (text->type != XML_CDATA_SECTION_NODE)) {
  3775. xsltTransformError(ctxt, NULL, inst,
  3776. "xsl:text content problem\n");
  3777. break;
  3778. }
  3779. copy = xmlNewDocText(ctxt->output, text->content);
  3780. if (text->type != XML_CDATA_SECTION_NODE) {
  3781. #ifdef WITH_XSLT_DEBUG_PARSING
  3782. xsltGenericDebug(xsltGenericDebugContext,
  3783. "Disable escaping: %s\n", text->content);
  3784. #endif
  3785. copy->name = xmlStringTextNoenc;
  3786. }
  3787. copy = xsltAddChild(ctxt->insert, copy);
  3788. text = text->next;
  3789. }
  3790. }
  3791. }
  3792. /**
  3793. * xsltElement:
  3794. * @ctxt: a XSLT process context
  3795. * @node: the node in the source tree.
  3796. * @inst: the xslt element node
  3797. * @castedComp: precomputed information
  3798. *
  3799. * Process the xslt element node on the source node
  3800. */
  3801. void
  3802. xsltElement(xsltTransformContextPtr ctxt, xmlNodePtr node,
  3803. xmlNodePtr inst, xsltElemPreCompPtr castedComp) {
  3804. #ifdef XSLT_REFACTORED
  3805. xsltStyleItemElementPtr comp = (xsltStyleItemElementPtr) castedComp;
  3806. #else
  3807. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  3808. #endif
  3809. xmlChar *prop = NULL;
  3810. const xmlChar *name, *prefix = NULL, *nsName = NULL;
  3811. xmlNodePtr copy;
  3812. xmlNodePtr oldInsert;
  3813. if (ctxt->insert == NULL)
  3814. return;
  3815. /*
  3816. * A comp->has_name == 0 indicates that we need to skip this instruction,
  3817. * since it was evaluated to be invalid already during compilation.
  3818. */
  3819. if (!comp->has_name)
  3820. return;
  3821. /*
  3822. * stack and saves
  3823. */
  3824. oldInsert = ctxt->insert;
  3825. if (comp->name == NULL) {
  3826. /* TODO: fix attr acquisition wrt to the XSLT namespace */
  3827. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3828. (const xmlChar *) "name", XSLT_NAMESPACE);
  3829. if (prop == NULL) {
  3830. xsltTransformError(ctxt, NULL, inst,
  3831. "xsl:element: The attribute 'name' is missing.\n");
  3832. goto error;
  3833. }
  3834. if (xmlValidateQName(prop, 0)) {
  3835. xsltTransformError(ctxt, NULL, inst,
  3836. "xsl:element: The effective name '%s' is not a "
  3837. "valid QName.\n", prop);
  3838. /* we fall through to catch any further errors, if possible */
  3839. }
  3840. name = xsltSplitQName(ctxt->dict, prop, &prefix);
  3841. xmlFree(prop);
  3842. } else {
  3843. /*
  3844. * The "name" value was static.
  3845. */
  3846. #ifdef XSLT_REFACTORED
  3847. prefix = comp->nsPrefix;
  3848. name = comp->name;
  3849. #else
  3850. name = xsltSplitQName(ctxt->dict, comp->name, &prefix);
  3851. #endif
  3852. }
  3853. /*
  3854. * Create the new element
  3855. */
  3856. if (ctxt->output->dict == ctxt->dict) {
  3857. copy = xmlNewDocNodeEatName(ctxt->output, NULL, (xmlChar *)name, NULL);
  3858. } else {
  3859. copy = xmlNewDocNode(ctxt->output, NULL, (xmlChar *)name, NULL);
  3860. }
  3861. if (copy == NULL) {
  3862. xsltTransformError(ctxt, NULL, inst,
  3863. "xsl:element : creation of %s failed\n", name);
  3864. return;
  3865. }
  3866. copy = xsltAddChild(ctxt->insert, copy);
  3867. if (copy == NULL) {
  3868. xsltTransformError(ctxt, NULL, inst,
  3869. "xsl:element : xsltAddChild failed\n");
  3870. return;
  3871. }
  3872. /*
  3873. * Namespace
  3874. * ---------
  3875. */
  3876. if (comp->has_ns) {
  3877. if (comp->ns != NULL) {
  3878. /*
  3879. * No AVT; just plain text for the namespace name.
  3880. */
  3881. if (comp->ns[0] != 0)
  3882. nsName = comp->ns;
  3883. } else {
  3884. xmlChar *tmpNsName;
  3885. /*
  3886. * Eval the AVT.
  3887. */
  3888. /* TODO: check attr acquisition wrt to the XSLT namespace */
  3889. tmpNsName = xsltEvalAttrValueTemplate(ctxt, inst,
  3890. (const xmlChar *) "namespace", XSLT_NAMESPACE);
  3891. /*
  3892. * SPEC XSLT 1.0:
  3893. * "If the string is empty, then the expanded-name of the
  3894. * attribute has a null namespace URI."
  3895. */
  3896. if ((tmpNsName != NULL) && (tmpNsName[0] != 0))
  3897. nsName = xmlDictLookup(ctxt->dict, BAD_CAST tmpNsName, -1);
  3898. xmlFree(tmpNsName);
  3899. }
  3900. if (xmlStrEqual(nsName, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
  3901. xsltTransformError(ctxt, NULL, inst,
  3902. "xsl:attribute: Namespace http://www.w3.org/2000/xmlns/ "
  3903. "forbidden.\n");
  3904. goto error;
  3905. }
  3906. if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
  3907. prefix = BAD_CAST "xml";
  3908. } else if (xmlStrEqual(prefix, BAD_CAST "xml")) {
  3909. prefix = NULL;
  3910. }
  3911. } else {
  3912. xmlNsPtr ns;
  3913. /*
  3914. * SPEC XSLT 1.0:
  3915. * "If the namespace attribute is not present, then the QName is
  3916. * expanded into an expanded-name using the namespace declarations
  3917. * in effect for the xsl:element element, including any default
  3918. * namespace declaration.
  3919. */
  3920. ns = xmlSearchNs(inst->doc, inst, prefix);
  3921. if (ns == NULL) {
  3922. /*
  3923. * TODO: Check this in the compilation layer in case it's a
  3924. * static value.
  3925. */
  3926. if (prefix != NULL) {
  3927. xsltTransformError(ctxt, NULL, inst,
  3928. "xsl:element: The QName '%s:%s' has no "
  3929. "namespace binding in scope in the stylesheet; "
  3930. "this is an error, since the namespace was not "
  3931. "specified by the instruction itself.\n", prefix, name);
  3932. }
  3933. } else
  3934. nsName = ns->href;
  3935. }
  3936. /*
  3937. * Find/create a matching ns-decl in the result tree.
  3938. */
  3939. if (nsName != NULL) {
  3940. if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
  3941. /* Don't use a prefix of "xmlns" */
  3942. xmlChar *pref = xmlStrdup(BAD_CAST "ns_1");
  3943. copy->ns = xsltGetSpecialNamespace(ctxt, inst, nsName, pref, copy);
  3944. xmlFree(pref);
  3945. } else {
  3946. copy->ns = xsltGetSpecialNamespace(ctxt, inst, nsName, prefix,
  3947. copy);
  3948. }
  3949. } else if ((copy->parent != NULL) &&
  3950. (copy->parent->type == XML_ELEMENT_NODE) &&
  3951. (copy->parent->ns != NULL))
  3952. {
  3953. /*
  3954. * "Undeclare" the default namespace.
  3955. */
  3956. xsltGetSpecialNamespace(ctxt, inst, NULL, NULL, copy);
  3957. }
  3958. ctxt->insert = copy;
  3959. if (comp->has_use) {
  3960. if (comp->use != NULL) {
  3961. xsltApplyAttributeSet(ctxt, node, inst, comp->use);
  3962. } else {
  3963. xmlChar *attrSets = NULL;
  3964. /*
  3965. * BUG TODO: use-attribute-sets is not a value template.
  3966. * use-attribute-sets = qnames
  3967. */
  3968. attrSets = xsltEvalAttrValueTemplate(ctxt, inst,
  3969. (const xmlChar *)"use-attribute-sets", NULL);
  3970. if (attrSets != NULL) {
  3971. xsltApplyAttributeSet(ctxt, node, inst, attrSets);
  3972. xmlFree(attrSets);
  3973. }
  3974. }
  3975. }
  3976. /*
  3977. * Instantiate the sequence constructor.
  3978. */
  3979. if (inst->children != NULL)
  3980. xsltApplySequenceConstructor(ctxt, ctxt->node, inst->children,
  3981. NULL);
  3982. error:
  3983. ctxt->insert = oldInsert;
  3984. return;
  3985. }
  3986. /**
  3987. * xsltComment:
  3988. * @ctxt: a XSLT process context
  3989. * @node: the node in the source tree.
  3990. * @inst: the xslt comment node
  3991. * @comp: precomputed information
  3992. *
  3993. * Process the xslt comment node on the source node
  3994. */
  3995. void
  3996. xsltComment(xsltTransformContextPtr ctxt, xmlNodePtr node,
  3997. xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED) {
  3998. xmlChar *value = NULL;
  3999. xmlNodePtr commentNode;
  4000. int len;
  4001. value = xsltEvalTemplateString(ctxt, node, inst);
  4002. /* TODO: use or generate the compiled form */
  4003. len = xmlStrlen(value);
  4004. if (len > 0) {
  4005. if ((value[len-1] == '-') ||
  4006. (xmlStrstr(value, BAD_CAST "--"))) {
  4007. xsltTransformError(ctxt, NULL, inst,
  4008. "xsl:comment : '--' or ending '-' not allowed in comment\n");
  4009. /* fall through to try to catch further errors */
  4010. }
  4011. }
  4012. #ifdef WITH_XSLT_DEBUG_PROCESS
  4013. if (value == NULL) {
  4014. XSLT_TRACE(ctxt,XSLT_TRACE_COMMENT,xsltGenericDebug(xsltGenericDebugContext,
  4015. "xsltComment: empty\n"));
  4016. } else {
  4017. XSLT_TRACE(ctxt,XSLT_TRACE_COMMENT,xsltGenericDebug(xsltGenericDebugContext,
  4018. "xsltComment: content %s\n", value));
  4019. }
  4020. #endif
  4021. commentNode = xmlNewComment(value);
  4022. commentNode = xsltAddChild(ctxt->insert, commentNode);
  4023. if (value != NULL)
  4024. xmlFree(value);
  4025. }
  4026. /**
  4027. * xsltProcessingInstruction:
  4028. * @ctxt: a XSLT process context
  4029. * @node: the node in the source tree.
  4030. * @inst: the xslt processing-instruction node
  4031. * @castedComp: precomputed information
  4032. *
  4033. * Process the xslt processing-instruction node on the source node
  4034. */
  4035. void
  4036. xsltProcessingInstruction(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4037. xmlNodePtr inst, xsltElemPreCompPtr castedComp) {
  4038. #ifdef XSLT_REFACTORED
  4039. xsltStyleItemPIPtr comp = (xsltStyleItemPIPtr) castedComp;
  4040. #else
  4041. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4042. #endif
  4043. const xmlChar *name;
  4044. xmlChar *value = NULL;
  4045. xmlNodePtr pi;
  4046. if (ctxt->insert == NULL)
  4047. return;
  4048. if (comp->has_name == 0)
  4049. return;
  4050. if (comp->name == NULL) {
  4051. name = xsltEvalAttrValueTemplate(ctxt, inst,
  4052. (const xmlChar *)"name", NULL);
  4053. if (name == NULL) {
  4054. xsltTransformError(ctxt, NULL, inst,
  4055. "xsl:processing-instruction : name is missing\n");
  4056. goto error;
  4057. }
  4058. } else {
  4059. name = comp->name;
  4060. }
  4061. /* TODO: check that it's both an an NCName and a PITarget. */
  4062. value = xsltEvalTemplateString(ctxt, node, inst);
  4063. if (xmlStrstr(value, BAD_CAST "?>") != NULL) {
  4064. xsltTransformError(ctxt, NULL, inst,
  4065. "xsl:processing-instruction: '?>' not allowed within PI content\n");
  4066. goto error;
  4067. }
  4068. #ifdef WITH_XSLT_DEBUG_PROCESS
  4069. if (value == NULL) {
  4070. XSLT_TRACE(ctxt,XSLT_TRACE_PI,xsltGenericDebug(xsltGenericDebugContext,
  4071. "xsltProcessingInstruction: %s empty\n", name));
  4072. } else {
  4073. XSLT_TRACE(ctxt,XSLT_TRACE_PI,xsltGenericDebug(xsltGenericDebugContext,
  4074. "xsltProcessingInstruction: %s content %s\n", name, value));
  4075. }
  4076. #endif
  4077. pi = xmlNewDocPI(ctxt->insert->doc, name, value);
  4078. pi = xsltAddChild(ctxt->insert, pi);
  4079. error:
  4080. if ((name != NULL) && (name != comp->name))
  4081. xmlFree((xmlChar *) name);
  4082. if (value != NULL)
  4083. xmlFree(value);
  4084. }
  4085. /**
  4086. * xsltCopyOf:
  4087. * @ctxt: an XSLT transformation context
  4088. * @node: the current node in the source tree
  4089. * @inst: the element node of the XSLT copy-of instruction
  4090. * @castedComp: precomputed information of the XSLT copy-of instruction
  4091. *
  4092. * Process the XSLT copy-of instruction.
  4093. */
  4094. void
  4095. xsltCopyOf(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4096. xmlNodePtr inst, xsltElemPreCompPtr castedComp) {
  4097. #ifdef XSLT_REFACTORED
  4098. xsltStyleItemCopyOfPtr comp = (xsltStyleItemCopyOfPtr) castedComp;
  4099. #else
  4100. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4101. #endif
  4102. xmlXPathObjectPtr res = NULL;
  4103. xmlNodeSetPtr list = NULL;
  4104. int i;
  4105. if ((ctxt == NULL) || (node == NULL) || (inst == NULL))
  4106. return;
  4107. if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) {
  4108. xsltTransformError(ctxt, NULL, inst,
  4109. "xsl:copy-of : compilation failed\n");
  4110. return;
  4111. }
  4112. /*
  4113. * SPEC XSLT 1.0:
  4114. * "The xsl:copy-of element can be used to insert a result tree
  4115. * fragment into the result tree, without first converting it to
  4116. * a string as xsl:value-of does (see [7.6.1 Generating Text with
  4117. * xsl:value-of]). The required select attribute contains an
  4118. * expression. When the result of evaluating the expression is a
  4119. * result tree fragment, the complete fragment is copied into the
  4120. * result tree. When the result is a node-set, all the nodes in the
  4121. * set are copied in document order into the result tree; copying
  4122. * an element node copies the attribute nodes, namespace nodes and
  4123. * children of the element node as well as the element node itself;
  4124. * a root node is copied by copying its children. When the result
  4125. * is neither a node-set nor a result tree fragment, the result is
  4126. * converted to a string and then inserted into the result tree,
  4127. * as with xsl:value-of.
  4128. */
  4129. #ifdef WITH_XSLT_DEBUG_PROCESS
  4130. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
  4131. "xsltCopyOf: select %s\n", comp->select));
  4132. #endif
  4133. /*
  4134. * Evaluate the "select" expression.
  4135. */
  4136. res = xsltPreCompEval(ctxt, node, comp);
  4137. if (res != NULL) {
  4138. if (res->type == XPATH_NODESET) {
  4139. /*
  4140. * Node-set
  4141. * --------
  4142. */
  4143. #ifdef WITH_XSLT_DEBUG_PROCESS
  4144. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
  4145. "xsltCopyOf: result is a node set\n"));
  4146. #endif
  4147. list = res->nodesetval;
  4148. if (list != NULL) {
  4149. xmlNodePtr cur;
  4150. /*
  4151. * The list is already sorted in document order by XPath.
  4152. * Append everything in this order under ctxt->insert.
  4153. */
  4154. for (i = 0;i < list->nodeNr;i++) {
  4155. cur = list->nodeTab[i];
  4156. if (cur == NULL)
  4157. continue;
  4158. if ((cur->type == XML_DOCUMENT_NODE) ||
  4159. (cur->type == XML_HTML_DOCUMENT_NODE))
  4160. {
  4161. xsltCopyTreeList(ctxt, inst,
  4162. cur->children, ctxt->insert, 0, 0);
  4163. } else if (cur->type == XML_ATTRIBUTE_NODE) {
  4164. xsltShallowCopyAttr(ctxt, inst,
  4165. ctxt->insert, (xmlAttrPtr) cur);
  4166. } else {
  4167. xsltCopyTree(ctxt, inst, cur, ctxt->insert, 0, 0);
  4168. }
  4169. }
  4170. }
  4171. } else if (res->type == XPATH_XSLT_TREE) {
  4172. /*
  4173. * Result tree fragment
  4174. * --------------------
  4175. * E.g. via <xsl:variable ...><foo/></xsl:variable>
  4176. * Note that the root node of such trees is an xmlDocPtr in Libxslt.
  4177. */
  4178. #ifdef WITH_XSLT_DEBUG_PROCESS
  4179. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
  4180. "xsltCopyOf: result is a result tree fragment\n"));
  4181. #endif
  4182. list = res->nodesetval;
  4183. if ((list != NULL) && (list->nodeTab != NULL) &&
  4184. (list->nodeTab[0] != NULL) &&
  4185. (IS_XSLT_REAL_NODE(list->nodeTab[0])))
  4186. {
  4187. xsltCopyTreeList(ctxt, inst,
  4188. list->nodeTab[0]->children, ctxt->insert, 0, 0);
  4189. }
  4190. } else {
  4191. xmlChar *value = NULL;
  4192. /*
  4193. * Convert to a string.
  4194. */
  4195. value = xmlXPathCastToString(res);
  4196. if (value == NULL) {
  4197. xsltTransformError(ctxt, NULL, inst,
  4198. "Internal error in xsltCopyOf(): "
  4199. "failed to cast an XPath object to string.\n");
  4200. ctxt->state = XSLT_STATE_STOPPED;
  4201. } else {
  4202. if (value[0] != 0) {
  4203. /*
  4204. * Append content as text node.
  4205. */
  4206. xsltCopyTextString(ctxt, ctxt->insert, value, 0);
  4207. }
  4208. xmlFree(value);
  4209. #ifdef WITH_XSLT_DEBUG_PROCESS
  4210. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
  4211. "xsltCopyOf: result %s\n", res->stringval));
  4212. #endif
  4213. }
  4214. }
  4215. } else {
  4216. ctxt->state = XSLT_STATE_STOPPED;
  4217. }
  4218. if (res != NULL)
  4219. xmlXPathFreeObject(res);
  4220. }
  4221. /**
  4222. * xsltValueOf:
  4223. * @ctxt: a XSLT process context
  4224. * @node: the node in the source tree.
  4225. * @inst: the xslt value-of node
  4226. * @castedComp: precomputed information
  4227. *
  4228. * Process the xslt value-of node on the source node
  4229. */
  4230. void
  4231. xsltValueOf(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4232. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4233. {
  4234. #ifdef XSLT_REFACTORED
  4235. xsltStyleItemValueOfPtr comp = (xsltStyleItemValueOfPtr) castedComp;
  4236. #else
  4237. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4238. #endif
  4239. xmlXPathObjectPtr res = NULL;
  4240. xmlChar *value = NULL;
  4241. if ((ctxt == NULL) || (node == NULL) || (inst == NULL))
  4242. return;
  4243. if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) {
  4244. xsltTransformError(ctxt, NULL, inst,
  4245. "Internal error in xsltValueOf(): "
  4246. "The XSLT 'value-of' instruction was not compiled.\n");
  4247. return;
  4248. }
  4249. #ifdef WITH_XSLT_DEBUG_PROCESS
  4250. XSLT_TRACE(ctxt,XSLT_TRACE_VALUE_OF,xsltGenericDebug(xsltGenericDebugContext,
  4251. "xsltValueOf: select %s\n", comp->select));
  4252. #endif
  4253. res = xsltPreCompEval(ctxt, node, comp);
  4254. /*
  4255. * Cast the XPath object to string.
  4256. */
  4257. if (res != NULL) {
  4258. value = xmlXPathCastToString(res);
  4259. if (value == NULL) {
  4260. xsltTransformError(ctxt, NULL, inst,
  4261. "Internal error in xsltValueOf(): "
  4262. "failed to cast an XPath object to string.\n");
  4263. ctxt->state = XSLT_STATE_STOPPED;
  4264. goto error;
  4265. }
  4266. if (value[0] != 0) {
  4267. xsltCopyTextString(ctxt, ctxt->insert, value, comp->noescape);
  4268. }
  4269. } else {
  4270. xsltTransformError(ctxt, NULL, inst,
  4271. "XPath evaluation returned no result.\n");
  4272. ctxt->state = XSLT_STATE_STOPPED;
  4273. goto error;
  4274. }
  4275. #ifdef WITH_XSLT_DEBUG_PROCESS
  4276. if (value) {
  4277. XSLT_TRACE(ctxt,XSLT_TRACE_VALUE_OF,xsltGenericDebug(xsltGenericDebugContext,
  4278. "xsltValueOf: result '%s'\n", value));
  4279. }
  4280. #endif
  4281. error:
  4282. if (value != NULL)
  4283. xmlFree(value);
  4284. if (res != NULL)
  4285. xmlXPathFreeObject(res);
  4286. }
  4287. /**
  4288. * xsltNumber:
  4289. * @ctxt: a XSLT process context
  4290. * @node: the node in the source tree.
  4291. * @inst: the xslt number node
  4292. * @castedComp: precomputed information
  4293. *
  4294. * Process the xslt number node on the source node
  4295. */
  4296. void
  4297. xsltNumber(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4298. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4299. {
  4300. #ifdef XSLT_REFACTORED
  4301. xsltStyleItemNumberPtr comp = (xsltStyleItemNumberPtr) castedComp;
  4302. #else
  4303. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4304. #endif
  4305. xmlXPathContextPtr xpctxt;
  4306. xmlNsPtr *oldXPNamespaces;
  4307. int oldXPNsNr;
  4308. if (comp == NULL) {
  4309. xsltTransformError(ctxt, NULL, inst,
  4310. "xsl:number : compilation failed\n");
  4311. return;
  4312. }
  4313. if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
  4314. return;
  4315. comp->numdata.doc = inst->doc;
  4316. comp->numdata.node = inst;
  4317. xpctxt = ctxt->xpathCtxt;
  4318. oldXPNsNr = xpctxt->nsNr;
  4319. oldXPNamespaces = xpctxt->namespaces;
  4320. #ifdef XSLT_REFACTORED
  4321. if (comp->inScopeNs != NULL) {
  4322. xpctxt->namespaces = comp->inScopeNs->list;
  4323. xpctxt->nsNr = comp->inScopeNs->xpathNumber;
  4324. } else {
  4325. xpctxt->namespaces = NULL;
  4326. xpctxt->nsNr = 0;
  4327. }
  4328. #else
  4329. xpctxt->namespaces = comp->nsList;
  4330. xpctxt->nsNr = comp->nsNr;
  4331. #endif
  4332. xsltNumberFormat(ctxt, &comp->numdata, node);
  4333. xpctxt->nsNr = oldXPNsNr;
  4334. xpctxt->namespaces = oldXPNamespaces;
  4335. }
  4336. /**
  4337. * xsltApplyImports:
  4338. * @ctxt: an XSLT transformation context
  4339. * @contextNode: the current node in the source tree.
  4340. * @inst: the element node of the XSLT 'apply-imports' instruction
  4341. * @comp: the compiled instruction
  4342. *
  4343. * Process the XSLT apply-imports element.
  4344. */
  4345. void
  4346. xsltApplyImports(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  4347. xmlNodePtr inst,
  4348. xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
  4349. {
  4350. xsltTemplatePtr templ;
  4351. if ((ctxt == NULL) || (inst == NULL))
  4352. return;
  4353. if (comp == NULL) {
  4354. xsltTransformError(ctxt, NULL, inst,
  4355. "Internal error in xsltApplyImports(): "
  4356. "The XSLT 'apply-imports' instruction was not compiled.\n");
  4357. return;
  4358. }
  4359. /*
  4360. * NOTE that ctxt->currentTemplateRule and ctxt->templ is not the
  4361. * same; the former is the "Current Template Rule" as defined by the
  4362. * XSLT spec, the latter is simply the template struct being
  4363. * currently processed.
  4364. */
  4365. if (ctxt->currentTemplateRule == NULL) {
  4366. /*
  4367. * SPEC XSLT 2.0:
  4368. * "[ERR XTDE0560] It is a non-recoverable dynamic error if
  4369. * xsl:apply-imports or xsl:next-match is evaluated when the
  4370. * current template rule is null."
  4371. */
  4372. xsltTransformError(ctxt, NULL, inst,
  4373. "It is an error to call 'apply-imports' "
  4374. "when there's no current template rule.\n");
  4375. return;
  4376. }
  4377. /*
  4378. * TODO: Check if this is correct.
  4379. */
  4380. templ = xsltGetTemplate(ctxt, contextNode,
  4381. ctxt->currentTemplateRule->style);
  4382. if (templ != NULL) {
  4383. xsltTemplatePtr oldCurTemplRule = ctxt->currentTemplateRule;
  4384. /*
  4385. * Set the current template rule.
  4386. */
  4387. ctxt->currentTemplateRule = templ;
  4388. /*
  4389. * URGENT TODO: Need xsl:with-param be handled somehow here?
  4390. */
  4391. xsltApplyXSLTTemplate(ctxt, contextNode, templ->content,
  4392. templ, NULL);
  4393. ctxt->currentTemplateRule = oldCurTemplRule;
  4394. }
  4395. else {
  4396. /* Use built-in templates. */
  4397. xsltDefaultProcessOneNode(ctxt, contextNode, NULL);
  4398. }
  4399. }
  4400. /**
  4401. * xsltCallTemplate:
  4402. * @ctxt: a XSLT transformation context
  4403. * @node: the "current node" in the source tree
  4404. * @inst: the XSLT 'call-template' instruction
  4405. * @castedComp: the compiled information of the instruction
  4406. *
  4407. * Processes the XSLT call-template instruction on the source node.
  4408. */
  4409. void
  4410. xsltCallTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4411. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4412. {
  4413. #ifdef XSLT_REFACTORED
  4414. xsltStyleItemCallTemplatePtr comp =
  4415. (xsltStyleItemCallTemplatePtr) castedComp;
  4416. #else
  4417. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4418. #endif
  4419. xsltStackElemPtr withParams = NULL;
  4420. if (ctxt->insert == NULL)
  4421. return;
  4422. if (comp == NULL) {
  4423. xsltTransformError(ctxt, NULL, inst,
  4424. "The XSLT 'call-template' instruction was not compiled.\n");
  4425. return;
  4426. }
  4427. /*
  4428. * The template must have been precomputed
  4429. */
  4430. if (comp->templ == NULL) {
  4431. comp->templ = xsltFindTemplate(ctxt, comp->name, comp->ns);
  4432. if (comp->templ == NULL) {
  4433. if (comp->ns != NULL) {
  4434. xsltTransformError(ctxt, NULL, inst,
  4435. "The called template '{%s}%s' was not found.\n",
  4436. comp->ns, comp->name);
  4437. } else {
  4438. xsltTransformError(ctxt, NULL, inst,
  4439. "The called template '%s' was not found.\n",
  4440. comp->name);
  4441. }
  4442. return;
  4443. }
  4444. }
  4445. #ifdef WITH_XSLT_DEBUG_PROCESS
  4446. if ((comp != NULL) && (comp->name != NULL))
  4447. XSLT_TRACE(ctxt,XSLT_TRACE_CALL_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  4448. "call-template: name %s\n", comp->name));
  4449. #endif
  4450. if (inst->children) {
  4451. xmlNodePtr cur;
  4452. xsltStackElemPtr param;
  4453. cur = inst->children;
  4454. while (cur != NULL) {
  4455. #ifdef WITH_DEBUGGER
  4456. if (ctxt->debugStatus != XSLT_DEBUG_NONE)
  4457. xslHandleDebugger(cur, node, comp->templ, ctxt);
  4458. #endif
  4459. if (ctxt->state == XSLT_STATE_STOPPED) break;
  4460. /*
  4461. * TODO: The "with-param"s could be part of the "call-template"
  4462. * structure. Avoid to "search" for params dynamically
  4463. * in the XML tree every time.
  4464. */
  4465. if (IS_XSLT_ELEM(cur)) {
  4466. if (IS_XSLT_NAME(cur, "with-param")) {
  4467. param = xsltParseStylesheetCallerParam(ctxt, cur);
  4468. if (param != NULL) {
  4469. param->next = withParams;
  4470. withParams = param;
  4471. }
  4472. } else {
  4473. xsltGenericError(xsltGenericErrorContext,
  4474. "xsl:call-template: misplaced xsl:%s\n", cur->name);
  4475. }
  4476. } else {
  4477. xsltGenericError(xsltGenericErrorContext,
  4478. "xsl:call-template: misplaced %s element\n", cur->name);
  4479. }
  4480. cur = cur->next;
  4481. }
  4482. }
  4483. /*
  4484. * Create a new frame using the params first
  4485. */
  4486. xsltApplyXSLTTemplate(ctxt, node, comp->templ->content, comp->templ,
  4487. withParams);
  4488. if (withParams != NULL)
  4489. xsltFreeStackElemList(withParams);
  4490. #ifdef WITH_XSLT_DEBUG_PROCESS
  4491. if ((comp != NULL) && (comp->name != NULL))
  4492. XSLT_TRACE(ctxt,XSLT_TRACE_CALL_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  4493. "call-template returned: name %s\n", comp->name));
  4494. #endif
  4495. }
  4496. /**
  4497. * xsltApplyTemplates:
  4498. * @ctxt: a XSLT transformation context
  4499. * @node: the 'current node' in the source tree
  4500. * @inst: the element node of an XSLT 'apply-templates' instruction
  4501. * @castedComp: the compiled instruction
  4502. *
  4503. * Processes the XSLT 'apply-templates' instruction on the current node.
  4504. */
  4505. void
  4506. xsltApplyTemplates(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4507. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4508. {
  4509. #ifdef XSLT_REFACTORED
  4510. xsltStyleItemApplyTemplatesPtr comp =
  4511. (xsltStyleItemApplyTemplatesPtr) castedComp;
  4512. #else
  4513. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4514. #endif
  4515. int i;
  4516. xmlNodePtr cur, delNode = NULL, oldContextNode;
  4517. xmlNodeSetPtr list = NULL, oldList;
  4518. xsltStackElemPtr withParams = NULL;
  4519. int oldXPProximityPosition, oldXPContextSize;
  4520. const xmlChar *oldMode, *oldModeURI;
  4521. xmlDocPtr oldXPDoc;
  4522. xsltDocumentPtr oldDocInfo;
  4523. xmlXPathContextPtr xpctxt;
  4524. if (comp == NULL) {
  4525. xsltTransformError(ctxt, NULL, inst,
  4526. "xsl:apply-templates : compilation failed\n");
  4527. return;
  4528. }
  4529. if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
  4530. return;
  4531. #ifdef WITH_XSLT_DEBUG_PROCESS
  4532. if ((node != NULL) && (node->name != NULL))
  4533. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4534. "xsltApplyTemplates: node: '%s'\n", node->name));
  4535. #endif
  4536. xpctxt = ctxt->xpathCtxt;
  4537. /*
  4538. * Save context states.
  4539. */
  4540. oldContextNode = ctxt->node;
  4541. oldMode = ctxt->mode;
  4542. oldModeURI = ctxt->modeURI;
  4543. oldDocInfo = ctxt->document;
  4544. oldList = ctxt->nodeList;
  4545. /*
  4546. * The xpath context size and proximity position, as
  4547. * well as the xpath and context documents, may be changed
  4548. * so we save their initial state and will restore on exit
  4549. */
  4550. oldXPContextSize = xpctxt->contextSize;
  4551. oldXPProximityPosition = xpctxt->proximityPosition;
  4552. oldXPDoc = xpctxt->doc;
  4553. /*
  4554. * Set up contexts.
  4555. */
  4556. ctxt->mode = comp->mode;
  4557. ctxt->modeURI = comp->modeURI;
  4558. if (comp->select != NULL) {
  4559. xmlXPathObjectPtr res = NULL;
  4560. if (comp->comp == NULL) {
  4561. xsltTransformError(ctxt, NULL, inst,
  4562. "xsl:apply-templates : compilation failed\n");
  4563. goto error;
  4564. }
  4565. #ifdef WITH_XSLT_DEBUG_PROCESS
  4566. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4567. "xsltApplyTemplates: select %s\n", comp->select));
  4568. #endif
  4569. res = xsltPreCompEval(ctxt, node, comp);
  4570. if (res != NULL) {
  4571. if (res->type == XPATH_NODESET) {
  4572. list = res->nodesetval; /* consume the node set */
  4573. res->nodesetval = NULL;
  4574. } else {
  4575. xsltTransformError(ctxt, NULL, inst,
  4576. "The 'select' expression did not evaluate to a "
  4577. "node set.\n");
  4578. ctxt->state = XSLT_STATE_STOPPED;
  4579. xmlXPathFreeObject(res);
  4580. goto error;
  4581. }
  4582. xmlXPathFreeObject(res);
  4583. /*
  4584. * Note: An xsl:apply-templates with a 'select' attribute,
  4585. * can change the current source doc.
  4586. */
  4587. } else {
  4588. xsltTransformError(ctxt, NULL, inst,
  4589. "Failed to evaluate the 'select' expression.\n");
  4590. ctxt->state = XSLT_STATE_STOPPED;
  4591. goto error;
  4592. }
  4593. if (list == NULL) {
  4594. #ifdef WITH_XSLT_DEBUG_PROCESS
  4595. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4596. "xsltApplyTemplates: select didn't evaluate to a node list\n"));
  4597. #endif
  4598. goto exit;
  4599. }
  4600. /*
  4601. *
  4602. * NOTE: Previously a document info (xsltDocument) was
  4603. * created and attached to the Result Tree Fragment.
  4604. * But such a document info is created on demand in
  4605. * xsltKeyFunction() (functions.c), so we need to create
  4606. * it here beforehand.
  4607. * In order to take care of potential keys we need to
  4608. * do some extra work for the case when a Result Tree Fragment
  4609. * is converted into a nodeset (e.g. exslt:node-set()) :
  4610. * We attach a "pseudo-doc" (xsltDocument) to _private.
  4611. * This xsltDocument, together with the keyset, will be freed
  4612. * when the Result Tree Fragment is freed.
  4613. *
  4614. */
  4615. #if 0
  4616. if ((ctxt->nbKeys > 0) &&
  4617. (list->nodeNr != 0) &&
  4618. (list->nodeTab[0]->doc != NULL) &&
  4619. XSLT_IS_RES_TREE_FRAG(list->nodeTab[0]->doc))
  4620. {
  4621. /*
  4622. * NOTE that it's also OK if @effectiveDocInfo will be
  4623. * set to NULL.
  4624. */
  4625. isRTF = 1;
  4626. effectiveDocInfo = list->nodeTab[0]->doc->_private;
  4627. }
  4628. #endif
  4629. } else {
  4630. /*
  4631. * Build an XPath node set with the children
  4632. */
  4633. list = xmlXPathNodeSetCreate(NULL);
  4634. if (list == NULL)
  4635. goto error;
  4636. if (node->type != XML_NAMESPACE_DECL)
  4637. cur = node->children;
  4638. else
  4639. cur = NULL;
  4640. while (cur != NULL) {
  4641. switch (cur->type) {
  4642. case XML_TEXT_NODE:
  4643. if ((IS_BLANK_NODE(cur)) &&
  4644. (cur->parent != NULL) &&
  4645. (cur->parent->type == XML_ELEMENT_NODE) &&
  4646. (ctxt->style->stripSpaces != NULL)) {
  4647. const xmlChar *val;
  4648. if (cur->parent->ns != NULL) {
  4649. val = (const xmlChar *)
  4650. xmlHashLookup2(ctxt->style->stripSpaces,
  4651. cur->parent->name,
  4652. cur->parent->ns->href);
  4653. if (val == NULL) {
  4654. val = (const xmlChar *)
  4655. xmlHashLookup2(ctxt->style->stripSpaces,
  4656. BAD_CAST "*",
  4657. cur->parent->ns->href);
  4658. }
  4659. } else {
  4660. val = (const xmlChar *)
  4661. xmlHashLookup2(ctxt->style->stripSpaces,
  4662. cur->parent->name, NULL);
  4663. }
  4664. if ((val != NULL) &&
  4665. (xmlStrEqual(val, (xmlChar *) "strip"))) {
  4666. delNode = cur;
  4667. break;
  4668. }
  4669. }
  4670. /* Intentional fall-through */
  4671. case XML_ELEMENT_NODE:
  4672. case XML_DOCUMENT_NODE:
  4673. case XML_HTML_DOCUMENT_NODE:
  4674. case XML_CDATA_SECTION_NODE:
  4675. case XML_PI_NODE:
  4676. case XML_COMMENT_NODE:
  4677. xmlXPathNodeSetAddUnique(list, cur);
  4678. break;
  4679. case XML_DTD_NODE:
  4680. /* Unlink the DTD, it's still reachable
  4681. * using doc->intSubset */
  4682. if (cur->next != NULL)
  4683. cur->next->prev = cur->prev;
  4684. if (cur->prev != NULL)
  4685. cur->prev->next = cur->next;
  4686. break;
  4687. case XML_NAMESPACE_DECL:
  4688. break;
  4689. default:
  4690. #ifdef WITH_XSLT_DEBUG_PROCESS
  4691. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4692. "xsltApplyTemplates: skipping cur type %d\n",
  4693. cur->type));
  4694. #endif
  4695. delNode = cur;
  4696. }
  4697. cur = cur->next;
  4698. if (delNode != NULL) {
  4699. #ifdef WITH_XSLT_DEBUG_PROCESS
  4700. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4701. "xsltApplyTemplates: removing ignorable blank cur\n"));
  4702. #endif
  4703. xmlUnlinkNode(delNode);
  4704. xmlFreeNode(delNode);
  4705. delNode = NULL;
  4706. }
  4707. }
  4708. }
  4709. #ifdef WITH_XSLT_DEBUG_PROCESS
  4710. if (list != NULL)
  4711. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4712. "xsltApplyTemplates: list of %d nodes\n", list->nodeNr));
  4713. #endif
  4714. if ((list == NULL) || (list->nodeNr == 0))
  4715. goto exit;
  4716. /*
  4717. * Set the context's node set and size; this is also needed for
  4718. * for xsltDoSortFunction().
  4719. */
  4720. ctxt->nodeList = list;
  4721. /*
  4722. * Process xsl:with-param and xsl:sort instructions.
  4723. * (The code became so verbose just to avoid the
  4724. * xmlNodePtr sorts[XSLT_MAX_SORT] if there's no xsl:sort)
  4725. * BUG TODO: We are not using namespaced potentially defined on the
  4726. * xsl:sort or xsl:with-param elements; XPath expression might fail.
  4727. */
  4728. if (inst->children) {
  4729. xsltStackElemPtr param;
  4730. cur = inst->children;
  4731. while (cur) {
  4732. #ifdef WITH_DEBUGGER
  4733. if (ctxt->debugStatus != XSLT_DEBUG_NONE)
  4734. xslHandleDebugger(cur, node, NULL, ctxt);
  4735. #endif
  4736. if (ctxt->state == XSLT_STATE_STOPPED)
  4737. break;
  4738. if (cur->type == XML_TEXT_NODE) {
  4739. cur = cur->next;
  4740. continue;
  4741. }
  4742. if (! IS_XSLT_ELEM(cur))
  4743. break;
  4744. if (IS_XSLT_NAME(cur, "with-param")) {
  4745. param = xsltParseStylesheetCallerParam(ctxt, cur);
  4746. if (param != NULL) {
  4747. param->next = withParams;
  4748. withParams = param;
  4749. }
  4750. }
  4751. if (IS_XSLT_NAME(cur, "sort")) {
  4752. xsltTemplatePtr oldCurTempRule =
  4753. ctxt->currentTemplateRule;
  4754. int nbsorts = 0;
  4755. xmlNodePtr sorts[XSLT_MAX_SORT];
  4756. sorts[nbsorts++] = cur;
  4757. while (cur) {
  4758. #ifdef WITH_DEBUGGER
  4759. if (ctxt->debugStatus != XSLT_DEBUG_NONE)
  4760. xslHandleDebugger(cur, node, NULL, ctxt);
  4761. #endif
  4762. if (ctxt->state == XSLT_STATE_STOPPED)
  4763. break;
  4764. if (cur->type == XML_TEXT_NODE) {
  4765. cur = cur->next;
  4766. continue;
  4767. }
  4768. if (! IS_XSLT_ELEM(cur))
  4769. break;
  4770. if (IS_XSLT_NAME(cur, "with-param")) {
  4771. param = xsltParseStylesheetCallerParam(ctxt, cur);
  4772. if (param != NULL) {
  4773. param->next = withParams;
  4774. withParams = param;
  4775. }
  4776. }
  4777. if (IS_XSLT_NAME(cur, "sort")) {
  4778. if (nbsorts >= XSLT_MAX_SORT) {
  4779. xsltTransformError(ctxt, NULL, cur,
  4780. "The number (%d) of xsl:sort instructions exceeds the "
  4781. "maximum allowed by this processor's settings.\n",
  4782. nbsorts);
  4783. ctxt->state = XSLT_STATE_STOPPED;
  4784. break;
  4785. } else {
  4786. sorts[nbsorts++] = cur;
  4787. }
  4788. }
  4789. cur = cur->next;
  4790. }
  4791. /*
  4792. * The "current template rule" is cleared for xsl:sort.
  4793. */
  4794. ctxt->currentTemplateRule = NULL;
  4795. /*
  4796. * Sort.
  4797. */
  4798. xsltDoSortFunction(ctxt, sorts, nbsorts);
  4799. ctxt->currentTemplateRule = oldCurTempRule;
  4800. break;
  4801. }
  4802. cur = cur->next;
  4803. }
  4804. }
  4805. xpctxt->contextSize = list->nodeNr;
  4806. /*
  4807. * Apply templates for all selected source nodes.
  4808. */
  4809. for (i = 0; i < list->nodeNr; i++) {
  4810. cur = list->nodeTab[i];
  4811. /*
  4812. * The node becomes the "current node".
  4813. */
  4814. ctxt->node = cur;
  4815. /*
  4816. * An xsl:apply-templates can change the current context doc.
  4817. * OPTIMIZE TODO: Get rid of the need to set the context doc.
  4818. */
  4819. if ((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL))
  4820. xpctxt->doc = cur->doc;
  4821. xpctxt->proximityPosition = i + 1;
  4822. /*
  4823. * Find and apply a template for this node.
  4824. */
  4825. xsltProcessOneNode(ctxt, cur, withParams);
  4826. }
  4827. exit:
  4828. error:
  4829. /*
  4830. * Free the parameter list.
  4831. */
  4832. if (withParams != NULL)
  4833. xsltFreeStackElemList(withParams);
  4834. if (list != NULL)
  4835. xmlXPathFreeNodeSet(list);
  4836. /*
  4837. * Restore context states.
  4838. */
  4839. xpctxt->doc = oldXPDoc;
  4840. xpctxt->contextSize = oldXPContextSize;
  4841. xpctxt->proximityPosition = oldXPProximityPosition;
  4842. ctxt->document = oldDocInfo;
  4843. ctxt->nodeList = oldList;
  4844. ctxt->node = oldContextNode;
  4845. ctxt->mode = oldMode;
  4846. ctxt->modeURI = oldModeURI;
  4847. }
  4848. /**
  4849. * xsltChoose:
  4850. * @ctxt: a XSLT process context
  4851. * @contextNode: the current node in the source tree
  4852. * @inst: the xsl:choose instruction
  4853. * @comp: compiled information of the instruction
  4854. *
  4855. * Processes the xsl:choose instruction on the source node.
  4856. */
  4857. void
  4858. xsltChoose(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  4859. xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
  4860. {
  4861. xmlNodePtr cur;
  4862. if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL))
  4863. return;
  4864. /*
  4865. * TODO: Content model checks should be done only at compilation
  4866. * time.
  4867. */
  4868. cur = inst->children;
  4869. if (cur == NULL) {
  4870. xsltTransformError(ctxt, NULL, inst,
  4871. "xsl:choose: The instruction has no content.\n");
  4872. return;
  4873. }
  4874. #ifdef XSLT_REFACTORED
  4875. /*
  4876. * We don't check the content model during transformation.
  4877. */
  4878. #else
  4879. if ((! IS_XSLT_ELEM(cur)) || (! IS_XSLT_NAME(cur, "when"))) {
  4880. xsltTransformError(ctxt, NULL, inst,
  4881. "xsl:choose: xsl:when expected first\n");
  4882. return;
  4883. }
  4884. #endif
  4885. {
  4886. int testRes = 0, res = 0;
  4887. #ifdef XSLT_REFACTORED
  4888. xsltStyleItemWhenPtr wcomp = NULL;
  4889. #else
  4890. xsltStylePreCompPtr wcomp = NULL;
  4891. #endif
  4892. /*
  4893. * Process xsl:when ---------------------------------------------------
  4894. */
  4895. while (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "when")) {
  4896. wcomp = cur->psvi;
  4897. if ((wcomp == NULL) || (wcomp->test == NULL) ||
  4898. (wcomp->comp == NULL))
  4899. {
  4900. xsltTransformError(ctxt, NULL, cur,
  4901. "Internal error in xsltChoose(): "
  4902. "The XSLT 'when' instruction was not compiled.\n");
  4903. goto error;
  4904. }
  4905. #ifdef WITH_DEBUGGER
  4906. if (xslDebugStatus != XSLT_DEBUG_NONE) {
  4907. /*
  4908. * TODO: Isn't comp->templ always NULL for xsl:choose?
  4909. */
  4910. xslHandleDebugger(cur, contextNode, NULL, ctxt);
  4911. }
  4912. #endif
  4913. #ifdef WITH_XSLT_DEBUG_PROCESS
  4914. XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
  4915. "xsltChoose: test %s\n", wcomp->test));
  4916. #endif
  4917. #ifdef XSLT_FAST_IF
  4918. res = xsltPreCompEvalToBoolean(ctxt, contextNode, wcomp);
  4919. if (res == -1) {
  4920. ctxt->state = XSLT_STATE_STOPPED;
  4921. goto error;
  4922. }
  4923. testRes = (res == 1) ? 1 : 0;
  4924. #else /* XSLT_FAST_IF */
  4925. res = xsltPreCompEval(ctxt, cotextNode, wcomp);
  4926. if (res != NULL) {
  4927. if (res->type != XPATH_BOOLEAN)
  4928. res = xmlXPathConvertBoolean(res);
  4929. if (res->type == XPATH_BOOLEAN)
  4930. testRes = res->boolval;
  4931. else {
  4932. #ifdef WITH_XSLT_DEBUG_PROCESS
  4933. XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
  4934. "xsltChoose: test didn't evaluate to a boolean\n"));
  4935. #endif
  4936. goto error;
  4937. }
  4938. xmlXPathFreeObject(res);
  4939. res = NULL;
  4940. } else {
  4941. ctxt->state = XSLT_STATE_STOPPED;
  4942. goto error;
  4943. }
  4944. #endif /* else of XSLT_FAST_IF */
  4945. #ifdef WITH_XSLT_DEBUG_PROCESS
  4946. XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
  4947. "xsltChoose: test evaluate to %d\n", testRes));
  4948. #endif
  4949. if (testRes)
  4950. goto test_is_true;
  4951. cur = cur->next;
  4952. }
  4953. /*
  4954. * Process xsl:otherwise ----------------------------------------------
  4955. */
  4956. if (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "otherwise")) {
  4957. #ifdef WITH_DEBUGGER
  4958. if (xslDebugStatus != XSLT_DEBUG_NONE)
  4959. xslHandleDebugger(cur, contextNode, NULL, ctxt);
  4960. #endif
  4961. #ifdef WITH_XSLT_DEBUG_PROCESS
  4962. XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
  4963. "evaluating xsl:otherwise\n"));
  4964. #endif
  4965. goto test_is_true;
  4966. }
  4967. goto exit;
  4968. test_is_true:
  4969. goto process_sequence;
  4970. }
  4971. process_sequence:
  4972. /*
  4973. * Instantiate the sequence constructor.
  4974. */
  4975. xsltApplySequenceConstructor(ctxt, ctxt->node, cur->children,
  4976. NULL);
  4977. exit:
  4978. error:
  4979. return;
  4980. }
  4981. /**
  4982. * xsltIf:
  4983. * @ctxt: a XSLT process context
  4984. * @contextNode: the current node in the source tree
  4985. * @inst: the xsl:if instruction
  4986. * @castedComp: compiled information of the instruction
  4987. *
  4988. * Processes the xsl:if instruction on the source node.
  4989. */
  4990. void
  4991. xsltIf(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  4992. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4993. {
  4994. int res = 0;
  4995. #ifdef XSLT_REFACTORED
  4996. xsltStyleItemIfPtr comp = (xsltStyleItemIfPtr) castedComp;
  4997. #else
  4998. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4999. #endif
  5000. if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL))
  5001. return;
  5002. if ((comp == NULL) || (comp->test == NULL) || (comp->comp == NULL)) {
  5003. xsltTransformError(ctxt, NULL, inst,
  5004. "Internal error in xsltIf(): "
  5005. "The XSLT 'if' instruction was not compiled.\n");
  5006. return;
  5007. }
  5008. #ifdef WITH_XSLT_DEBUG_PROCESS
  5009. XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
  5010. "xsltIf: test %s\n", comp->test));
  5011. #endif
  5012. #ifdef XSLT_FAST_IF
  5013. {
  5014. xmlDocPtr oldLocalFragmentTop = ctxt->localRVT;
  5015. res = xsltPreCompEvalToBoolean(ctxt, contextNode, comp);
  5016. /*
  5017. * Cleanup fragments created during evaluation of the
  5018. * "select" expression.
  5019. */
  5020. if (oldLocalFragmentTop != ctxt->localRVT)
  5021. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  5022. }
  5023. #ifdef WITH_XSLT_DEBUG_PROCESS
  5024. XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
  5025. "xsltIf: test evaluate to %d\n", res));
  5026. #endif
  5027. if (res == -1) {
  5028. ctxt->state = XSLT_STATE_STOPPED;
  5029. goto error;
  5030. }
  5031. if (res == 1) {
  5032. /*
  5033. * Instantiate the sequence constructor of xsl:if.
  5034. */
  5035. xsltApplySequenceConstructor(ctxt,
  5036. contextNode, inst->children, NULL);
  5037. }
  5038. #else /* XSLT_FAST_IF */
  5039. {
  5040. /*
  5041. * OLD CODE:
  5042. */
  5043. xmlXPathObjectPtr xpobj = xsltPreCompEval(ctxt, contextNode, comp);
  5044. if (xpobj != NULL) {
  5045. if (xpobj->type != XPATH_BOOLEAN)
  5046. xpobj = xmlXPathConvertBoolean(xpobj);
  5047. if (xpobj->type == XPATH_BOOLEAN) {
  5048. res = xpobj->boolval;
  5049. #ifdef WITH_XSLT_DEBUG_PROCESS
  5050. XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
  5051. "xsltIf: test evaluate to %d\n", res));
  5052. #endif
  5053. if (res) {
  5054. xsltApplySequenceConstructor(ctxt,
  5055. contextNode, inst->children, NULL);
  5056. }
  5057. } else {
  5058. #ifdef WITH_XSLT_DEBUG_PROCESS
  5059. XSLT_TRACE(ctxt, XSLT_TRACE_IF,
  5060. xsltGenericDebug(xsltGenericDebugContext,
  5061. "xsltIf: test didn't evaluate to a boolean\n"));
  5062. #endif
  5063. ctxt->state = XSLT_STATE_STOPPED;
  5064. }
  5065. xmlXPathFreeObject(xpobj);
  5066. } else {
  5067. ctxt->state = XSLT_STATE_STOPPED;
  5068. }
  5069. }
  5070. #endif /* else of XSLT_FAST_IF */
  5071. error:
  5072. return;
  5073. }
  5074. /**
  5075. * xsltForEach:
  5076. * @ctxt: an XSLT transformation context
  5077. * @contextNode: the "current node" in the source tree
  5078. * @inst: the element node of the xsl:for-each instruction
  5079. * @castedComp: the compiled information of the instruction
  5080. *
  5081. * Process the xslt for-each node on the source node
  5082. */
  5083. void
  5084. xsltForEach(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  5085. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  5086. {
  5087. #ifdef XSLT_REFACTORED
  5088. xsltStyleItemForEachPtr comp = (xsltStyleItemForEachPtr) castedComp;
  5089. #else
  5090. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  5091. #endif
  5092. int i;
  5093. xmlXPathObjectPtr res = NULL;
  5094. xmlNodePtr cur, curInst;
  5095. xmlNodeSetPtr list = NULL;
  5096. xmlNodeSetPtr oldList;
  5097. int oldXPProximityPosition, oldXPContextSize;
  5098. xmlNodePtr oldContextNode;
  5099. xsltTemplatePtr oldCurTemplRule;
  5100. xmlDocPtr oldXPDoc;
  5101. xsltDocumentPtr oldDocInfo;
  5102. xmlXPathContextPtr xpctxt;
  5103. if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL)) {
  5104. xsltGenericError(xsltGenericErrorContext,
  5105. "xsltForEach(): Bad arguments.\n");
  5106. return;
  5107. }
  5108. if (comp == NULL) {
  5109. xsltTransformError(ctxt, NULL, inst,
  5110. "Internal error in xsltForEach(): "
  5111. "The XSLT 'for-each' instruction was not compiled.\n");
  5112. return;
  5113. }
  5114. if ((comp->select == NULL) || (comp->comp == NULL)) {
  5115. xsltTransformError(ctxt, NULL, inst,
  5116. "Internal error in xsltForEach(): "
  5117. "The selecting expression of the XSLT 'for-each' "
  5118. "instruction was not compiled correctly.\n");
  5119. return;
  5120. }
  5121. xpctxt = ctxt->xpathCtxt;
  5122. #ifdef WITH_XSLT_DEBUG_PROCESS
  5123. XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
  5124. "xsltForEach: select %s\n", comp->select));
  5125. #endif
  5126. /*
  5127. * Save context states.
  5128. */
  5129. oldDocInfo = ctxt->document;
  5130. oldList = ctxt->nodeList;
  5131. oldContextNode = ctxt->node;
  5132. /*
  5133. * The "current template rule" is cleared for the instantiation of
  5134. * xsl:for-each.
  5135. */
  5136. oldCurTemplRule = ctxt->currentTemplateRule;
  5137. ctxt->currentTemplateRule = NULL;
  5138. oldXPDoc = xpctxt->doc;
  5139. oldXPProximityPosition = xpctxt->proximityPosition;
  5140. oldXPContextSize = xpctxt->contextSize;
  5141. /*
  5142. * Evaluate the 'select' expression.
  5143. */
  5144. res = xsltPreCompEval(ctxt, contextNode, comp);
  5145. if (res != NULL) {
  5146. if (res->type == XPATH_NODESET)
  5147. list = res->nodesetval;
  5148. else {
  5149. xsltTransformError(ctxt, NULL, inst,
  5150. "The 'select' expression does not evaluate to a node set.\n");
  5151. #ifdef WITH_XSLT_DEBUG_PROCESS
  5152. XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
  5153. "xsltForEach: select didn't evaluate to a node list\n"));
  5154. #endif
  5155. goto error;
  5156. }
  5157. } else {
  5158. xsltTransformError(ctxt, NULL, inst,
  5159. "Failed to evaluate the 'select' expression.\n");
  5160. ctxt->state = XSLT_STATE_STOPPED;
  5161. goto error;
  5162. }
  5163. if ((list == NULL) || (list->nodeNr <= 0))
  5164. goto exit;
  5165. #ifdef WITH_XSLT_DEBUG_PROCESS
  5166. XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
  5167. "xsltForEach: select evaluates to %d nodes\n", list->nodeNr));
  5168. #endif
  5169. /*
  5170. * Set the list; this has to be done already here for xsltDoSortFunction().
  5171. */
  5172. ctxt->nodeList = list;
  5173. /*
  5174. * Handle xsl:sort instructions and skip them for further processing.
  5175. * BUG TODO: We are not using namespaced potentially defined on the
  5176. * xsl:sort element; XPath expression might fail.
  5177. */
  5178. curInst = inst->children;
  5179. if (IS_XSLT_ELEM(curInst) && IS_XSLT_NAME(curInst, "sort")) {
  5180. int nbsorts = 0;
  5181. xmlNodePtr sorts[XSLT_MAX_SORT];
  5182. sorts[nbsorts++] = curInst;
  5183. #ifdef WITH_DEBUGGER
  5184. if (xslDebugStatus != XSLT_DEBUG_NONE)
  5185. xslHandleDebugger(curInst, contextNode, NULL, ctxt);
  5186. #endif
  5187. curInst = curInst->next;
  5188. while (IS_XSLT_ELEM(curInst) && IS_XSLT_NAME(curInst, "sort")) {
  5189. if (nbsorts >= XSLT_MAX_SORT) {
  5190. xsltTransformError(ctxt, NULL, curInst,
  5191. "The number of xsl:sort instructions exceeds the "
  5192. "maximum (%d) allowed by this processor.\n",
  5193. XSLT_MAX_SORT);
  5194. goto error;
  5195. } else {
  5196. sorts[nbsorts++] = curInst;
  5197. }
  5198. #ifdef WITH_DEBUGGER
  5199. if (xslDebugStatus != XSLT_DEBUG_NONE)
  5200. xslHandleDebugger(curInst, contextNode, NULL, ctxt);
  5201. #endif
  5202. curInst = curInst->next;
  5203. }
  5204. xsltDoSortFunction(ctxt, sorts, nbsorts);
  5205. }
  5206. xpctxt->contextSize = list->nodeNr;
  5207. /*
  5208. * Instantiate the sequence constructor for each selected node.
  5209. */
  5210. for (i = 0; i < list->nodeNr; i++) {
  5211. cur = list->nodeTab[i];
  5212. /*
  5213. * The selected node becomes the "current node".
  5214. */
  5215. ctxt->node = cur;
  5216. /*
  5217. * An xsl:for-each can change the current context doc.
  5218. * OPTIMIZE TODO: Get rid of the need to set the context doc.
  5219. */
  5220. if ((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL))
  5221. xpctxt->doc = cur->doc;
  5222. xpctxt->proximityPosition = i + 1;
  5223. xsltApplySequenceConstructor(ctxt, cur, curInst, NULL);
  5224. }
  5225. exit:
  5226. error:
  5227. if (res != NULL)
  5228. xmlXPathFreeObject(res);
  5229. /*
  5230. * Restore old states.
  5231. */
  5232. ctxt->document = oldDocInfo;
  5233. ctxt->nodeList = oldList;
  5234. ctxt->node = oldContextNode;
  5235. ctxt->currentTemplateRule = oldCurTemplRule;
  5236. xpctxt->doc = oldXPDoc;
  5237. xpctxt->contextSize = oldXPContextSize;
  5238. xpctxt->proximityPosition = oldXPProximityPosition;
  5239. }
  5240. /************************************************************************
  5241. * *
  5242. * Generic interface *
  5243. * *
  5244. ************************************************************************/
  5245. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  5246. typedef struct xsltHTMLVersion {
  5247. const char *version;
  5248. const char *public;
  5249. const char *system;
  5250. } xsltHTMLVersion;
  5251. static xsltHTMLVersion xsltHTMLVersions[] = {
  5252. { "5", NULL, "about:legacy-compat" },
  5253. { "4.01frame", "-//W3C//DTD HTML 4.01 Frameset//EN",
  5254. "http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd"},
  5255. { "4.01strict", "-//W3C//DTD HTML 4.01//EN",
  5256. "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd"},
  5257. { "4.01trans", "-//W3C//DTD HTML 4.01 Transitional//EN",
  5258. "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"},
  5259. { "4.01", "-//W3C//DTD HTML 4.01 Transitional//EN",
  5260. "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"},
  5261. { "4.0strict", "-//W3C//DTD HTML 4.01//EN",
  5262. "http://www.w3.org/TR/html4/strict.dtd"},
  5263. { "4.0trans", "-//W3C//DTD HTML 4.01 Transitional//EN",
  5264. "http://www.w3.org/TR/html4/loose.dtd"},
  5265. { "4.0frame", "-//W3C//DTD HTML 4.01 Frameset//EN",
  5266. "http://www.w3.org/TR/html4/frameset.dtd"},
  5267. { "4.0", "-//W3C//DTD HTML 4.01 Transitional//EN",
  5268. "http://www.w3.org/TR/html4/loose.dtd"},
  5269. { "3.2", "-//W3C//DTD HTML 3.2//EN", NULL }
  5270. };
  5271. /**
  5272. * xsltGetHTMLIDs:
  5273. * @version: the version string
  5274. * @publicID: used to return the public ID
  5275. * @systemID: used to return the system ID
  5276. *
  5277. * Returns -1 if not found, 0 otherwise and the system and public
  5278. * Identifier for this given verion of HTML
  5279. */
  5280. static int
  5281. xsltGetHTMLIDs(const xmlChar *version, const xmlChar **publicID,
  5282. const xmlChar **systemID) {
  5283. unsigned int i;
  5284. if (version == NULL)
  5285. return(-1);
  5286. for (i = 0;i < (sizeof(xsltHTMLVersions)/sizeof(xsltHTMLVersions[1]));
  5287. i++) {
  5288. if (!xmlStrcasecmp(version,
  5289. (const xmlChar *) xsltHTMLVersions[i].version)) {
  5290. if (publicID != NULL)
  5291. *publicID = (const xmlChar *) xsltHTMLVersions[i].public;
  5292. if (systemID != NULL)
  5293. *systemID = (const xmlChar *) xsltHTMLVersions[i].system;
  5294. return(0);
  5295. }
  5296. }
  5297. return(-1);
  5298. }
  5299. #endif
  5300. /**
  5301. * xsltApplyStripSpaces:
  5302. * @ctxt: a XSLT process context
  5303. * @node: the root of the XML tree
  5304. *
  5305. * Strip the unwanted ignorable spaces from the input tree
  5306. */
  5307. void
  5308. xsltApplyStripSpaces(xsltTransformContextPtr ctxt, xmlNodePtr node) {
  5309. xmlNodePtr current;
  5310. #ifdef WITH_XSLT_DEBUG_PROCESS
  5311. int nb = 0;
  5312. #endif
  5313. current = node;
  5314. while (current != NULL) {
  5315. /*
  5316. * Cleanup children empty nodes if asked for
  5317. */
  5318. if ((IS_XSLT_REAL_NODE(current)) &&
  5319. (current->children != NULL) &&
  5320. (xsltFindElemSpaceHandling(ctxt, current))) {
  5321. xmlNodePtr delete = NULL, cur = current->children;
  5322. while (cur != NULL) {
  5323. if (IS_BLANK_NODE(cur))
  5324. delete = cur;
  5325. cur = cur->next;
  5326. if (delete != NULL) {
  5327. xmlUnlinkNode(delete);
  5328. xmlFreeNode(delete);
  5329. delete = NULL;
  5330. #ifdef WITH_XSLT_DEBUG_PROCESS
  5331. nb++;
  5332. #endif
  5333. }
  5334. }
  5335. }
  5336. /*
  5337. * Skip to next node in document order.
  5338. */
  5339. if (node->type == XML_ENTITY_REF_NODE) {
  5340. /* process deep in entities */
  5341. xsltApplyStripSpaces(ctxt, node->children);
  5342. }
  5343. if ((current->children != NULL) &&
  5344. (current->type != XML_ENTITY_REF_NODE)) {
  5345. current = current->children;
  5346. } else if (current->next != NULL) {
  5347. current = current->next;
  5348. } else {
  5349. do {
  5350. current = current->parent;
  5351. if (current == NULL)
  5352. break;
  5353. if (current == node)
  5354. goto done;
  5355. if (current->next != NULL) {
  5356. current = current->next;
  5357. break;
  5358. }
  5359. } while (current != NULL);
  5360. }
  5361. }
  5362. done:
  5363. #ifdef WITH_XSLT_DEBUG_PROCESS
  5364. XSLT_TRACE(ctxt,XSLT_TRACE_STRIP_SPACES,xsltGenericDebug(xsltGenericDebugContext,
  5365. "xsltApplyStripSpaces: removed %d ignorable blank node\n", nb));
  5366. #endif
  5367. return;
  5368. }
  5369. static int
  5370. xsltCountKeys(xsltTransformContextPtr ctxt)
  5371. {
  5372. xsltStylesheetPtr style;
  5373. xsltKeyDefPtr keyd;
  5374. if (ctxt == NULL)
  5375. return(-1);
  5376. /*
  5377. * Do we have those nastly templates with a key() in the match pattern?
  5378. */
  5379. ctxt->hasTemplKeyPatterns = 0;
  5380. style = ctxt->style;
  5381. while (style != NULL) {
  5382. if (style->keyMatch != NULL) {
  5383. ctxt->hasTemplKeyPatterns = 1;
  5384. break;
  5385. }
  5386. style = xsltNextImport(style);
  5387. }
  5388. /*
  5389. * Count number of key declarations.
  5390. */
  5391. ctxt->nbKeys = 0;
  5392. style = ctxt->style;
  5393. while (style != NULL) {
  5394. keyd = style->keys;
  5395. while (keyd) {
  5396. ctxt->nbKeys++;
  5397. keyd = keyd->next;
  5398. }
  5399. style = xsltNextImport(style);
  5400. }
  5401. return(ctxt->nbKeys);
  5402. }
  5403. /**
  5404. * xsltApplyStylesheetInternal:
  5405. * @style: a parsed XSLT stylesheet
  5406. * @doc: a parsed XML document
  5407. * @params: a NULL terminated array of parameters names/values tuples
  5408. * @output: the targetted output
  5409. * @profile: profile FILE * output or NULL
  5410. * @user: user provided parameter
  5411. *
  5412. * Apply the stylesheet to the document
  5413. * NOTE: This may lead to a non-wellformed output XML wise !
  5414. *
  5415. * Returns the result document or NULL in case of error
  5416. */
  5417. static xmlDocPtr
  5418. xsltApplyStylesheetInternal(xsltStylesheetPtr style, xmlDocPtr doc,
  5419. const char **params, const char *output,
  5420. FILE * profile, xsltTransformContextPtr userCtxt)
  5421. {
  5422. xmlDocPtr res = NULL;
  5423. xsltTransformContextPtr ctxt = NULL;
  5424. xmlNodePtr root, node;
  5425. const xmlChar *method;
  5426. const xmlChar *doctypePublic;
  5427. const xmlChar *doctypeSystem;
  5428. const xmlChar *version;
  5429. const xmlChar *encoding;
  5430. xsltStackElemPtr variables;
  5431. xsltStackElemPtr vptr;
  5432. xsltInitGlobals();
  5433. if ((style == NULL) || (doc == NULL))
  5434. return (NULL);
  5435. if (style->internalized == 0) {
  5436. #ifdef WITH_XSLT_DEBUG
  5437. xsltGenericDebug(xsltGenericDebugContext,
  5438. "Stylesheet was not fully internalized !\n");
  5439. #endif
  5440. }
  5441. if (doc->intSubset != NULL) {
  5442. /*
  5443. * Avoid hitting the DTD when scanning nodes
  5444. * but keep it linked as doc->intSubset
  5445. */
  5446. xmlNodePtr cur = (xmlNodePtr) doc->intSubset;
  5447. if (cur->next != NULL)
  5448. cur->next->prev = cur->prev;
  5449. if (cur->prev != NULL)
  5450. cur->prev->next = cur->next;
  5451. if (doc->children == cur)
  5452. doc->children = cur->next;
  5453. if (doc->last == cur)
  5454. doc->last = cur->prev;
  5455. cur->prev = cur->next = NULL;
  5456. }
  5457. /*
  5458. * Check for XPath document order availability
  5459. */
  5460. root = xmlDocGetRootElement(doc);
  5461. if (root != NULL) {
  5462. if (((ptrdiff_t) root->content >= 0) &&
  5463. (xslDebugStatus == XSLT_DEBUG_NONE))
  5464. xmlXPathOrderDocElems(doc);
  5465. }
  5466. if (userCtxt != NULL)
  5467. ctxt = userCtxt;
  5468. else
  5469. ctxt = xsltNewTransformContext(style, doc);
  5470. if (ctxt == NULL)
  5471. return (NULL);
  5472. ctxt->initialContextDoc = doc;
  5473. ctxt->initialContextNode = (xmlNodePtr) doc;
  5474. if (profile != NULL) {
  5475. #ifdef WITH_PROFILER
  5476. ctxt->profile = 1;
  5477. #else
  5478. xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
  5479. "xsltApplyStylesheetInternal: "
  5480. "libxslt compiled without profiler\n");
  5481. goto error;
  5482. #endif
  5483. }
  5484. if (output != NULL)
  5485. ctxt->outputFile = output;
  5486. else
  5487. ctxt->outputFile = NULL;
  5488. /*
  5489. * internalize the modes if needed
  5490. */
  5491. if (ctxt->dict != NULL) {
  5492. if (ctxt->mode != NULL)
  5493. ctxt->mode = xmlDictLookup(ctxt->dict, ctxt->mode, -1);
  5494. if (ctxt->modeURI != NULL)
  5495. ctxt->modeURI = xmlDictLookup(ctxt->dict, ctxt->modeURI, -1);
  5496. }
  5497. XSLT_GET_IMPORT_PTR(method, style, method)
  5498. XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
  5499. XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
  5500. XSLT_GET_IMPORT_PTR(version, style, version)
  5501. XSLT_GET_IMPORT_PTR(encoding, style, encoding)
  5502. if ((method != NULL) &&
  5503. (!xmlStrEqual(method, (const xmlChar *) "xml")))
  5504. {
  5505. if (xmlStrEqual(method, (const xmlChar *) "html")) {
  5506. ctxt->type = XSLT_OUTPUT_HTML;
  5507. if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
  5508. res = htmlNewDoc(doctypeSystem, doctypePublic);
  5509. } else {
  5510. if (version == NULL) {
  5511. xmlDtdPtr dtd;
  5512. res = htmlNewDoc(NULL, NULL);
  5513. /*
  5514. * Make sure no DTD node is generated in this case
  5515. */
  5516. if (res != NULL) {
  5517. dtd = xmlGetIntSubset(res);
  5518. if (dtd != NULL) {
  5519. xmlUnlinkNode((xmlNodePtr) dtd);
  5520. xmlFreeDtd(dtd);
  5521. }
  5522. res->intSubset = NULL;
  5523. res->extSubset = NULL;
  5524. }
  5525. } else {
  5526. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  5527. xsltGetHTMLIDs(version, &doctypePublic, &doctypeSystem);
  5528. #endif
  5529. res = htmlNewDoc(doctypeSystem, doctypePublic);
  5530. }
  5531. }
  5532. if (res == NULL)
  5533. goto error;
  5534. res->dict = ctxt->dict;
  5535. xmlDictReference(res->dict);
  5536. #ifdef WITH_XSLT_DEBUG
  5537. xsltGenericDebug(xsltGenericDebugContext,
  5538. "reusing transformation dict for output\n");
  5539. #endif
  5540. } else if (xmlStrEqual(method, (const xmlChar *) "xhtml")) {
  5541. xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
  5542. "xsltApplyStylesheetInternal: unsupported method xhtml, using html\n");
  5543. ctxt->type = XSLT_OUTPUT_HTML;
  5544. res = htmlNewDoc(doctypeSystem, doctypePublic);
  5545. if (res == NULL)
  5546. goto error;
  5547. res->dict = ctxt->dict;
  5548. xmlDictReference(res->dict);
  5549. #ifdef WITH_XSLT_DEBUG
  5550. xsltGenericDebug(xsltGenericDebugContext,
  5551. "reusing transformation dict for output\n");
  5552. #endif
  5553. } else if (xmlStrEqual(method, (const xmlChar *) "text")) {
  5554. ctxt->type = XSLT_OUTPUT_TEXT;
  5555. res = xmlNewDoc(style->version);
  5556. if (res == NULL)
  5557. goto error;
  5558. res->dict = ctxt->dict;
  5559. xmlDictReference(res->dict);
  5560. #ifdef WITH_XSLT_DEBUG
  5561. xsltGenericDebug(xsltGenericDebugContext,
  5562. "reusing transformation dict for output\n");
  5563. #endif
  5564. } else {
  5565. xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
  5566. "xsltApplyStylesheetInternal: unsupported method (%s)\n",
  5567. method);
  5568. goto error;
  5569. }
  5570. } else {
  5571. ctxt->type = XSLT_OUTPUT_XML;
  5572. res = xmlNewDoc(style->version);
  5573. if (res == NULL)
  5574. goto error;
  5575. res->dict = ctxt->dict;
  5576. xmlDictReference(ctxt->dict);
  5577. #ifdef WITH_XSLT_DEBUG
  5578. xsltGenericDebug(xsltGenericDebugContext,
  5579. "reusing transformation dict for output\n");
  5580. #endif
  5581. }
  5582. res->charset = XML_CHAR_ENCODING_UTF8;
  5583. if (encoding != NULL)
  5584. res->encoding = xmlStrdup(encoding);
  5585. variables = style->variables;
  5586. ctxt->node = (xmlNodePtr) doc;
  5587. ctxt->output = res;
  5588. ctxt->xpathCtxt->contextSize = 1;
  5589. ctxt->xpathCtxt->proximityPosition = 1;
  5590. ctxt->xpathCtxt->node = NULL; /* TODO: Set the context node here? */
  5591. /*
  5592. * Start the evaluation, evaluate the params, the stylesheets globals
  5593. * and start by processing the top node.
  5594. */
  5595. if (xsltNeedElemSpaceHandling(ctxt))
  5596. xsltApplyStripSpaces(ctxt, xmlDocGetRootElement(doc));
  5597. /*
  5598. * Evaluate global params and user-provided params.
  5599. */
  5600. if (ctxt->globalVars == NULL)
  5601. ctxt->globalVars = xmlHashCreate(20);
  5602. if (params != NULL) {
  5603. xsltEvalUserParams(ctxt, params);
  5604. }
  5605. /* need to be called before evaluating global variables */
  5606. xsltCountKeys(ctxt);
  5607. xsltEvalGlobalVariables(ctxt);
  5608. /* Clean up any unused RVTs. */
  5609. xsltReleaseLocalRVTs(ctxt, NULL);
  5610. ctxt->insert = (xmlNodePtr) res;
  5611. ctxt->varsBase = ctxt->varsNr - 1;
  5612. /*
  5613. * Start processing the source tree -----------------------------------
  5614. */
  5615. xsltProcessOneNode(ctxt, ctxt->node, NULL);
  5616. /*
  5617. * Remove all remaining vars from the stack.
  5618. */
  5619. xsltLocalVariablePop(ctxt, 0, -2);
  5620. xsltShutdownCtxtExts(ctxt);
  5621. xsltCleanupTemplates(style); /* TODO: <- style should be read only */
  5622. /*
  5623. * Now cleanup our variables so stylesheet can be re-used
  5624. *
  5625. * TODO: this is not needed anymore global variables are copied
  5626. * and not evaluated directly anymore, keep this as a check
  5627. */
  5628. if (style->variables != variables) {
  5629. vptr = style->variables;
  5630. while (vptr->next != variables)
  5631. vptr = vptr->next;
  5632. vptr->next = NULL;
  5633. xsltFreeStackElemList(style->variables);
  5634. style->variables = variables;
  5635. }
  5636. vptr = style->variables;
  5637. while (vptr != NULL) {
  5638. if (vptr->computed) {
  5639. if (vptr->value != NULL) {
  5640. xmlXPathFreeObject(vptr->value);
  5641. vptr->value = NULL;
  5642. vptr->computed = 0;
  5643. }
  5644. }
  5645. vptr = vptr->next;
  5646. }
  5647. #if 0
  5648. /*
  5649. * code disabled by wmb; awaiting kb's review
  5650. * problem is that global variable(s) may contain xpath objects
  5651. * from doc associated with RVT, so can't be freed at this point.
  5652. * xsltFreeTransformContext includes a call to xsltFreeRVTs, so
  5653. * I assume this shouldn't be required at this point.
  5654. */
  5655. /*
  5656. * Free all remaining tree fragments.
  5657. */
  5658. xsltFreeRVTs(ctxt);
  5659. #endif
  5660. /*
  5661. * Do some post processing work depending on the generated output
  5662. */
  5663. root = xmlDocGetRootElement(res);
  5664. if (root != NULL) {
  5665. const xmlChar *doctype = NULL;
  5666. if ((root->ns != NULL) && (root->ns->prefix != NULL))
  5667. doctype = xmlDictQLookup(ctxt->dict, root->ns->prefix, root->name);
  5668. if (doctype == NULL)
  5669. doctype = root->name;
  5670. /*
  5671. * Apply the default selection of the method
  5672. */
  5673. if ((method == NULL) &&
  5674. (root->ns == NULL) &&
  5675. (!xmlStrcasecmp(root->name, (const xmlChar *) "html"))) {
  5676. xmlNodePtr tmp;
  5677. tmp = res->children;
  5678. while ((tmp != NULL) && (tmp != root)) {
  5679. if (tmp->type == XML_ELEMENT_NODE)
  5680. break;
  5681. if ((tmp->type == XML_TEXT_NODE) && (!xmlIsBlankNode(tmp)))
  5682. break;
  5683. tmp = tmp->next;
  5684. }
  5685. if (tmp == root) {
  5686. ctxt->type = XSLT_OUTPUT_HTML;
  5687. /*
  5688. * REVISIT TODO: XML_HTML_DOCUMENT_NODE is set after the
  5689. * transformation on the doc, but functions like
  5690. */
  5691. res->type = XML_HTML_DOCUMENT_NODE;
  5692. if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
  5693. res->intSubset = xmlCreateIntSubset(res, doctype,
  5694. doctypePublic,
  5695. doctypeSystem);
  5696. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  5697. } else if (version != NULL) {
  5698. xsltGetHTMLIDs(version, &doctypePublic,
  5699. &doctypeSystem);
  5700. if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
  5701. res->intSubset =
  5702. xmlCreateIntSubset(res, doctype,
  5703. doctypePublic,
  5704. doctypeSystem);
  5705. #endif
  5706. }
  5707. }
  5708. }
  5709. if (ctxt->type == XSLT_OUTPUT_XML) {
  5710. XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
  5711. XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
  5712. if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
  5713. xmlNodePtr last;
  5714. /* Need a small "hack" here to assure DTD comes before
  5715. possible comment nodes */
  5716. node = res->children;
  5717. last = res->last;
  5718. res->children = NULL;
  5719. res->last = NULL;
  5720. res->intSubset = xmlCreateIntSubset(res, doctype,
  5721. doctypePublic,
  5722. doctypeSystem);
  5723. if (res->children != NULL) {
  5724. res->children->next = node;
  5725. node->prev = res->children;
  5726. res->last = last;
  5727. } else {
  5728. res->children = node;
  5729. res->last = last;
  5730. }
  5731. }
  5732. }
  5733. }
  5734. xmlXPathFreeNodeSet(ctxt->nodeList);
  5735. #ifdef WITH_PROFILER
  5736. if (profile != NULL) {
  5737. xsltSaveProfiling(ctxt, profile);
  5738. }
  5739. #endif
  5740. /*
  5741. * Be pedantic.
  5742. */
  5743. if ((ctxt != NULL) && (ctxt->state != XSLT_STATE_OK)) {
  5744. xmlFreeDoc(res);
  5745. res = NULL;
  5746. }
  5747. if ((res != NULL) && (ctxt != NULL) && (output != NULL)) {
  5748. int ret;
  5749. ret = xsltCheckWrite(ctxt->sec, ctxt, (const xmlChar *) output);
  5750. if (ret == 0) {
  5751. xsltTransformError(ctxt, NULL, NULL,
  5752. "xsltApplyStylesheet: forbidden to save to %s\n",
  5753. output);
  5754. } else if (ret < 0) {
  5755. xsltTransformError(ctxt, NULL, NULL,
  5756. "xsltApplyStylesheet: saving to %s may not be possible\n",
  5757. output);
  5758. }
  5759. }
  5760. #ifdef XSLT_DEBUG_PROFILE_CACHE
  5761. printf("# Cache:\n");
  5762. printf("# Reused tree fragments: %d\n", ctxt->cache->dbgReusedRVTs);
  5763. printf("# Reused variables : %d\n", ctxt->cache->dbgReusedVars);
  5764. #endif
  5765. if ((ctxt != NULL) && (userCtxt == NULL))
  5766. xsltFreeTransformContext(ctxt);
  5767. return (res);
  5768. error:
  5769. if (res != NULL)
  5770. xmlFreeDoc(res);
  5771. #ifdef XSLT_DEBUG_PROFILE_CACHE
  5772. printf("# Cache:\n");
  5773. printf("# Reused tree fragments: %d\n", ctxt->cache->dbgReusedRVTs);
  5774. printf("# Reused variables : %d\n", ctxt->cache->dbgReusedVars);
  5775. #endif
  5776. if ((ctxt != NULL) && (userCtxt == NULL))
  5777. xsltFreeTransformContext(ctxt);
  5778. return (NULL);
  5779. }
  5780. /**
  5781. * xsltApplyStylesheet:
  5782. * @style: a parsed XSLT stylesheet
  5783. * @doc: a parsed XML document
  5784. * @params: a NULL terminated arry of parameters names/values tuples
  5785. *
  5786. * Apply the stylesheet to the document
  5787. * NOTE: This may lead to a non-wellformed output XML wise !
  5788. *
  5789. * Returns the result document or NULL in case of error
  5790. */
  5791. xmlDocPtr
  5792. xsltApplyStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
  5793. const char **params)
  5794. {
  5795. return (xsltApplyStylesheetInternal(style, doc, params, NULL, NULL, NULL));
  5796. }
  5797. /**
  5798. * xsltProfileStylesheet:
  5799. * @style: a parsed XSLT stylesheet
  5800. * @doc: a parsed XML document
  5801. * @params: a NULL terminated arry of parameters names/values tuples
  5802. * @output: a FILE * for the profiling output
  5803. *
  5804. * Apply the stylesheet to the document and dump the profiling to
  5805. * the given output.
  5806. *
  5807. * Returns the result document or NULL in case of error
  5808. */
  5809. xmlDocPtr
  5810. xsltProfileStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
  5811. const char **params, FILE * output)
  5812. {
  5813. xmlDocPtr res;
  5814. res = xsltApplyStylesheetInternal(style, doc, params, NULL, output, NULL);
  5815. return (res);
  5816. }
  5817. /**
  5818. * xsltApplyStylesheetUser:
  5819. * @style: a parsed XSLT stylesheet
  5820. * @doc: a parsed XML document
  5821. * @params: a NULL terminated array of parameters names/values tuples
  5822. * @output: the targetted output
  5823. * @profile: profile FILE * output or NULL
  5824. * @userCtxt: user provided transform context
  5825. *
  5826. * Apply the stylesheet to the document and allow the user to provide
  5827. * its own transformation context.
  5828. *
  5829. * Returns the result document or NULL in case of error
  5830. */
  5831. xmlDocPtr
  5832. xsltApplyStylesheetUser(xsltStylesheetPtr style, xmlDocPtr doc,
  5833. const char **params, const char *output,
  5834. FILE * profile, xsltTransformContextPtr userCtxt)
  5835. {
  5836. xmlDocPtr res;
  5837. res = xsltApplyStylesheetInternal(style, doc, params, output,
  5838. profile, userCtxt);
  5839. return (res);
  5840. }
  5841. /**
  5842. * xsltRunStylesheetUser:
  5843. * @style: a parsed XSLT stylesheet
  5844. * @doc: a parsed XML document
  5845. * @params: a NULL terminated array of parameters names/values tuples
  5846. * @output: the URL/filename ot the generated resource if available
  5847. * @SAX: a SAX handler for progressive callback output (not implemented yet)
  5848. * @IObuf: an output buffer for progressive output (not implemented yet)
  5849. * @profile: profile FILE * output or NULL
  5850. * @userCtxt: user provided transform context
  5851. *
  5852. * Apply the stylesheet to the document and generate the output according
  5853. * to @output @SAX and @IObuf. It's an error to specify both @SAX and @IObuf.
  5854. *
  5855. * NOTE: This may lead to a non-wellformed output XML wise !
  5856. * NOTE: This may also result in multiple files being generated
  5857. * NOTE: using IObuf, the result encoding used will be the one used for
  5858. * creating the output buffer, use the following macro to read it
  5859. * from the stylesheet
  5860. * XSLT_GET_IMPORT_PTR(encoding, style, encoding)
  5861. * NOTE: using SAX, any encoding specified in the stylesheet will be lost
  5862. * since the interface uses only UTF8
  5863. *
  5864. * Returns the number of by written to the main resource or -1 in case of
  5865. * error.
  5866. */
  5867. int
  5868. xsltRunStylesheetUser(xsltStylesheetPtr style, xmlDocPtr doc,
  5869. const char **params, const char *output,
  5870. xmlSAXHandlerPtr SAX, xmlOutputBufferPtr IObuf,
  5871. FILE * profile, xsltTransformContextPtr userCtxt)
  5872. {
  5873. xmlDocPtr tmp;
  5874. int ret;
  5875. if ((output == NULL) && (SAX == NULL) && (IObuf == NULL))
  5876. return (-1);
  5877. if ((SAX != NULL) && (IObuf != NULL))
  5878. return (-1);
  5879. /* unsupported yet */
  5880. if (SAX != NULL) {
  5881. XSLT_TODO /* xsltRunStylesheet xmlSAXHandlerPtr SAX */
  5882. return (-1);
  5883. }
  5884. tmp = xsltApplyStylesheetInternal(style, doc, params, output, profile,
  5885. userCtxt);
  5886. if (tmp == NULL) {
  5887. xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
  5888. "xsltRunStylesheet : run failed\n");
  5889. return (-1);
  5890. }
  5891. if (IObuf != NULL) {
  5892. /* TODO: incomplete, IObuf output not progressive */
  5893. ret = xsltSaveResultTo(IObuf, tmp, style);
  5894. } else {
  5895. ret = xsltSaveResultToFilename(output, tmp, style, 0);
  5896. }
  5897. xmlFreeDoc(tmp);
  5898. return (ret);
  5899. }
  5900. /**
  5901. * xsltRunStylesheet:
  5902. * @style: a parsed XSLT stylesheet
  5903. * @doc: a parsed XML document
  5904. * @params: a NULL terminated array of parameters names/values tuples
  5905. * @output: the URL/filename ot the generated resource if available
  5906. * @SAX: a SAX handler for progressive callback output (not implemented yet)
  5907. * @IObuf: an output buffer for progressive output (not implemented yet)
  5908. *
  5909. * Apply the stylesheet to the document and generate the output according
  5910. * to @output @SAX and @IObuf. It's an error to specify both @SAX and @IObuf.
  5911. *
  5912. * NOTE: This may lead to a non-wellformed output XML wise !
  5913. * NOTE: This may also result in multiple files being generated
  5914. * NOTE: using IObuf, the result encoding used will be the one used for
  5915. * creating the output buffer, use the following macro to read it
  5916. * from the stylesheet
  5917. * XSLT_GET_IMPORT_PTR(encoding, style, encoding)
  5918. * NOTE: using SAX, any encoding specified in the stylesheet will be lost
  5919. * since the interface uses only UTF8
  5920. *
  5921. * Returns the number of bytes written to the main resource or -1 in case of
  5922. * error.
  5923. */
  5924. int
  5925. xsltRunStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
  5926. const char **params, const char *output,
  5927. xmlSAXHandlerPtr SAX, xmlOutputBufferPtr IObuf)
  5928. {
  5929. return(xsltRunStylesheetUser(style, doc, params, output, SAX, IObuf,
  5930. NULL, NULL));
  5931. }
  5932. static void
  5933. xsltMessageWrapper(xsltTransformContextPtr ctxt, xmlNodePtr node,
  5934. xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED) {
  5935. xsltMessage(ctxt, node, inst);
  5936. }
  5937. /**
  5938. * xsltRegisterAllElement:
  5939. * @ctxt: the XPath context
  5940. *
  5941. * Registers all default XSLT elements in this context
  5942. */
  5943. void
  5944. xsltRegisterAllElement(xsltTransformContextPtr ctxt)
  5945. {
  5946. xsltRegisterExtElement(ctxt, (const xmlChar *) "apply-templates",
  5947. XSLT_NAMESPACE,
  5948. xsltApplyTemplates);
  5949. xsltRegisterExtElement(ctxt, (const xmlChar *) "apply-imports",
  5950. XSLT_NAMESPACE,
  5951. xsltApplyImports);
  5952. xsltRegisterExtElement(ctxt, (const xmlChar *) "call-template",
  5953. XSLT_NAMESPACE,
  5954. xsltCallTemplate);
  5955. xsltRegisterExtElement(ctxt, (const xmlChar *) "element",
  5956. XSLT_NAMESPACE,
  5957. xsltElement);
  5958. xsltRegisterExtElement(ctxt, (const xmlChar *) "attribute",
  5959. XSLT_NAMESPACE,
  5960. xsltAttribute);
  5961. xsltRegisterExtElement(ctxt, (const xmlChar *) "text",
  5962. XSLT_NAMESPACE,
  5963. xsltText);
  5964. xsltRegisterExtElement(ctxt, (const xmlChar *) "processing-instruction",
  5965. XSLT_NAMESPACE,
  5966. xsltProcessingInstruction);
  5967. xsltRegisterExtElement(ctxt, (const xmlChar *) "comment",
  5968. XSLT_NAMESPACE,
  5969. xsltComment);
  5970. xsltRegisterExtElement(ctxt, (const xmlChar *) "copy",
  5971. XSLT_NAMESPACE,
  5972. xsltCopy);
  5973. xsltRegisterExtElement(ctxt, (const xmlChar *) "value-of",
  5974. XSLT_NAMESPACE,
  5975. xsltValueOf);
  5976. xsltRegisterExtElement(ctxt, (const xmlChar *) "number",
  5977. XSLT_NAMESPACE,
  5978. xsltNumber);
  5979. xsltRegisterExtElement(ctxt, (const xmlChar *) "for-each",
  5980. XSLT_NAMESPACE,
  5981. xsltForEach);
  5982. xsltRegisterExtElement(ctxt, (const xmlChar *) "if",
  5983. XSLT_NAMESPACE,
  5984. xsltIf);
  5985. xsltRegisterExtElement(ctxt, (const xmlChar *) "choose",
  5986. XSLT_NAMESPACE,
  5987. xsltChoose);
  5988. xsltRegisterExtElement(ctxt, (const xmlChar *) "sort",
  5989. XSLT_NAMESPACE,
  5990. xsltSort);
  5991. xsltRegisterExtElement(ctxt, (const xmlChar *) "copy-of",
  5992. XSLT_NAMESPACE,
  5993. xsltCopyOf);
  5994. xsltRegisterExtElement(ctxt, (const xmlChar *) "message",
  5995. XSLT_NAMESPACE,
  5996. xsltMessageWrapper);
  5997. /*
  5998. * Those don't have callable entry points but are registered anyway
  5999. */
  6000. xsltRegisterExtElement(ctxt, (const xmlChar *) "variable",
  6001. XSLT_NAMESPACE,
  6002. xsltDebug);
  6003. xsltRegisterExtElement(ctxt, (const xmlChar *) "param",
  6004. XSLT_NAMESPACE,
  6005. xsltDebug);
  6006. xsltRegisterExtElement(ctxt, (const xmlChar *) "with-param",
  6007. XSLT_NAMESPACE,
  6008. xsltDebug);
  6009. xsltRegisterExtElement(ctxt, (const xmlChar *) "decimal-format",
  6010. XSLT_NAMESPACE,
  6011. xsltDebug);
  6012. xsltRegisterExtElement(ctxt, (const xmlChar *) "when",
  6013. XSLT_NAMESPACE,
  6014. xsltDebug);
  6015. xsltRegisterExtElement(ctxt, (const xmlChar *) "otherwise",
  6016. XSLT_NAMESPACE,
  6017. xsltDebug);
  6018. xsltRegisterExtElement(ctxt, (const xmlChar *) "fallback",
  6019. XSLT_NAMESPACE,
  6020. xsltDebug);
  6021. }