transform.c 180 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423
  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 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, counting
  1835. */
  1836. cur = node->children;
  1837. while (cur != NULL) {
  1838. if (IS_XSLT_REAL_NODE(cur))
  1839. nbchild++;
  1840. cur = cur->next;
  1841. }
  1842. /*
  1843. * Handling of Elements: second pass, actual processing
  1844. *
  1845. * Note that params are passed to the next template. This matches
  1846. * XSLT 2.0 behavior but doesn't conform to XSLT 1.0.
  1847. */
  1848. oldSize = ctxt->xpathCtxt->contextSize;
  1849. oldPos = ctxt->xpathCtxt->proximityPosition;
  1850. cur = node->children;
  1851. while (cur != NULL) {
  1852. childno++;
  1853. switch (cur->type) {
  1854. case XML_DOCUMENT_NODE:
  1855. case XML_HTML_DOCUMENT_NODE:
  1856. case XML_ELEMENT_NODE:
  1857. ctxt->xpathCtxt->contextSize = nbchild;
  1858. ctxt->xpathCtxt->proximityPosition = childno;
  1859. xsltProcessOneNode(ctxt, cur, params);
  1860. break;
  1861. case XML_CDATA_SECTION_NODE:
  1862. template = xsltGetTemplate(ctxt, cur, NULL);
  1863. if (template) {
  1864. #ifdef WITH_XSLT_DEBUG_PROCESS
  1865. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1866. "xsltDefaultProcessOneNode: applying template for CDATA %s\n",
  1867. cur->content));
  1868. #endif
  1869. /*
  1870. * Instantiate the xsl:template.
  1871. */
  1872. xsltApplyXSLTTemplate(ctxt, cur, template->content,
  1873. template, params);
  1874. } else /* if (ctxt->mode == NULL) */ {
  1875. #ifdef WITH_XSLT_DEBUG_PROCESS
  1876. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1877. "xsltDefaultProcessOneNode: copy CDATA %s\n",
  1878. cur->content));
  1879. #endif
  1880. copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
  1881. if (copy == NULL) {
  1882. xsltTransformError(ctxt, NULL, cur,
  1883. "xsltDefaultProcessOneNode: cdata copy failed\n");
  1884. }
  1885. }
  1886. break;
  1887. case XML_TEXT_NODE:
  1888. template = xsltGetTemplate(ctxt, cur, NULL);
  1889. if (template) {
  1890. #ifdef WITH_XSLT_DEBUG_PROCESS
  1891. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1892. "xsltDefaultProcessOneNode: applying template for text %s\n",
  1893. cur->content));
  1894. #endif
  1895. ctxt->xpathCtxt->contextSize = nbchild;
  1896. ctxt->xpathCtxt->proximityPosition = childno;
  1897. /*
  1898. * Instantiate the xsl:template.
  1899. */
  1900. xsltApplyXSLTTemplate(ctxt, cur, template->content,
  1901. template, params);
  1902. } else /* if (ctxt->mode == NULL) */ {
  1903. #ifdef WITH_XSLT_DEBUG_PROCESS
  1904. if (cur->content == NULL) {
  1905. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1906. "xsltDefaultProcessOneNode: copy empty text\n"));
  1907. } else {
  1908. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1909. "xsltDefaultProcessOneNode: copy text %s\n",
  1910. cur->content));
  1911. }
  1912. #endif
  1913. copy = xsltCopyText(ctxt, ctxt->insert, cur, 0);
  1914. if (copy == NULL) {
  1915. xsltTransformError(ctxt, NULL, cur,
  1916. "xsltDefaultProcessOneNode: text copy failed\n");
  1917. }
  1918. }
  1919. break;
  1920. case XML_PI_NODE:
  1921. case XML_COMMENT_NODE:
  1922. template = xsltGetTemplate(ctxt, cur, NULL);
  1923. if (template) {
  1924. #ifdef WITH_XSLT_DEBUG_PROCESS
  1925. if (cur->type == XML_PI_NODE) {
  1926. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1927. "xsltDefaultProcessOneNode: template found for PI %s\n",
  1928. cur->name));
  1929. } else if (cur->type == XML_COMMENT_NODE) {
  1930. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1931. "xsltDefaultProcessOneNode: template found for comment\n"));
  1932. }
  1933. #endif
  1934. ctxt->xpathCtxt->contextSize = nbchild;
  1935. ctxt->xpathCtxt->proximityPosition = childno;
  1936. /*
  1937. * Instantiate the xsl:template.
  1938. */
  1939. xsltApplyXSLTTemplate(ctxt, cur, template->content,
  1940. template, params);
  1941. }
  1942. break;
  1943. default:
  1944. break;
  1945. }
  1946. cur = cur->next;
  1947. }
  1948. ctxt->xpathCtxt->contextSize = oldSize;
  1949. ctxt->xpathCtxt->proximityPosition = oldPos;
  1950. }
  1951. /**
  1952. * xsltProcessOneNode:
  1953. * @ctxt: a XSLT process context
  1954. * @contextNode: the "current node" in the source tree
  1955. * @withParams: extra parameters (e.g. xsl:with-param) passed to the
  1956. * template if any
  1957. *
  1958. * Process the source node.
  1959. */
  1960. void
  1961. xsltProcessOneNode(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  1962. xsltStackElemPtr withParams)
  1963. {
  1964. xsltTemplatePtr templ;
  1965. xmlNodePtr oldNode;
  1966. templ = xsltGetTemplate(ctxt, contextNode, NULL);
  1967. /*
  1968. * If no template is found, apply the default rule.
  1969. */
  1970. if (templ == NULL) {
  1971. #ifdef WITH_XSLT_DEBUG_PROCESS
  1972. if (contextNode->type == XML_DOCUMENT_NODE) {
  1973. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1974. "xsltProcessOneNode: no template found for /\n"));
  1975. } else if (contextNode->type == XML_CDATA_SECTION_NODE) {
  1976. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1977. "xsltProcessOneNode: no template found for CDATA\n"));
  1978. } else if (contextNode->type == XML_ATTRIBUTE_NODE) {
  1979. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1980. "xsltProcessOneNode: no template found for attribute %s\n",
  1981. ((xmlAttrPtr) contextNode)->name));
  1982. } else {
  1983. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  1984. "xsltProcessOneNode: no template found for %s\n", contextNode->name));
  1985. }
  1986. #endif
  1987. oldNode = ctxt->node;
  1988. ctxt->node = contextNode;
  1989. xsltDefaultProcessOneNode(ctxt, contextNode, withParams);
  1990. ctxt->node = oldNode;
  1991. return;
  1992. }
  1993. if (contextNode->type == XML_ATTRIBUTE_NODE) {
  1994. xsltTemplatePtr oldCurTempRule = ctxt->currentTemplateRule;
  1995. /*
  1996. * Set the "current template rule".
  1997. */
  1998. ctxt->currentTemplateRule = templ;
  1999. #ifdef WITH_XSLT_DEBUG_PROCESS
  2000. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  2001. "xsltProcessOneNode: applying template '%s' for attribute %s\n",
  2002. templ->match, contextNode->name));
  2003. #endif
  2004. xsltApplyXSLTTemplate(ctxt, contextNode, templ->content, templ, withParams);
  2005. ctxt->currentTemplateRule = oldCurTempRule;
  2006. } else {
  2007. xsltTemplatePtr oldCurTempRule = ctxt->currentTemplateRule;
  2008. /*
  2009. * Set the "current template rule".
  2010. */
  2011. ctxt->currentTemplateRule = templ;
  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: applying template '%s' for /\n",
  2016. templ->match));
  2017. } else {
  2018. XSLT_TRACE(ctxt,XSLT_TRACE_PROCESS_NODE,xsltGenericDebug(xsltGenericDebugContext,
  2019. "xsltProcessOneNode: applying template '%s' for %s\n",
  2020. templ->match, contextNode->name));
  2021. }
  2022. #endif
  2023. xsltApplyXSLTTemplate(ctxt, contextNode, templ->content, templ, withParams);
  2024. ctxt->currentTemplateRule = oldCurTempRule;
  2025. }
  2026. }
  2027. #ifdef WITH_DEBUGGER
  2028. static xmlNodePtr
  2029. xsltDebuggerStartSequenceConstructor(xsltTransformContextPtr ctxt,
  2030. xmlNodePtr contextNode,
  2031. xmlNodePtr list,
  2032. xsltTemplatePtr templ,
  2033. int *addCallResult)
  2034. {
  2035. xmlNodePtr debugedNode = NULL;
  2036. if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
  2037. if (templ) {
  2038. *addCallResult = xslAddCall(templ, templ->elem);
  2039. } else {
  2040. *addCallResult = xslAddCall(NULL, list);
  2041. }
  2042. switch (ctxt->debugStatus) {
  2043. case XSLT_DEBUG_RUN_RESTART:
  2044. case XSLT_DEBUG_QUIT:
  2045. if (*addCallResult)
  2046. xslDropCall();
  2047. return(NULL);
  2048. }
  2049. if (templ) {
  2050. xslHandleDebugger(templ->elem, contextNode, templ, ctxt);
  2051. debugedNode = templ->elem;
  2052. } else if (list) {
  2053. xslHandleDebugger(list, contextNode, templ, ctxt);
  2054. debugedNode = list;
  2055. } else if (ctxt->inst) {
  2056. xslHandleDebugger(ctxt->inst, contextNode, templ, ctxt);
  2057. debugedNode = ctxt->inst;
  2058. }
  2059. }
  2060. return(debugedNode);
  2061. }
  2062. #endif /* WITH_DEBUGGER */
  2063. /**
  2064. * xsltLocalVariablePush:
  2065. * @ctxt: the transformation context
  2066. * @variable: variable to be pushed to the variable stack
  2067. * @level: new value for variable's level
  2068. *
  2069. * Places the variable onto the local variable stack
  2070. *
  2071. * Returns: 0 for success, -1 for any error
  2072. * **NOTE:**
  2073. * This is an internal routine and should not be called by users!
  2074. */
  2075. int
  2076. xsltLocalVariablePush(xsltTransformContextPtr ctxt,
  2077. xsltStackElemPtr variable,
  2078. int level)
  2079. {
  2080. if (ctxt->varsMax == 0) {
  2081. ctxt->varsMax = 10;
  2082. ctxt->varsTab =
  2083. (xsltStackElemPtr *) xmlMalloc(ctxt->varsMax *
  2084. sizeof(ctxt->varsTab[0]));
  2085. if (ctxt->varsTab == NULL) {
  2086. xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
  2087. return (-1);
  2088. }
  2089. }
  2090. if (ctxt->varsNr >= ctxt->varsMax) {
  2091. ctxt->varsMax *= 2;
  2092. ctxt->varsTab =
  2093. (xsltStackElemPtr *) xmlRealloc(ctxt->varsTab,
  2094. ctxt->varsMax *
  2095. sizeof(ctxt->varsTab[0]));
  2096. if (ctxt->varsTab == NULL) {
  2097. xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
  2098. return (-1);
  2099. }
  2100. }
  2101. ctxt->varsTab[ctxt->varsNr++] = variable;
  2102. ctxt->vars = variable;
  2103. variable->level = level;
  2104. return(0);
  2105. }
  2106. /**
  2107. * xsltReleaseLocalRVTs:
  2108. *
  2109. * Fragments which are results of extension instructions
  2110. * are preserved; all other fragments are freed/cached.
  2111. */
  2112. static void
  2113. xsltReleaseLocalRVTs(xsltTransformContextPtr ctxt, xmlDocPtr base)
  2114. {
  2115. xmlDocPtr cur = ctxt->localRVT, tmp;
  2116. if (cur == base)
  2117. return;
  2118. if (cur->prev != NULL)
  2119. xsltTransformError(ctxt, NULL, NULL, "localRVT not head of list\n");
  2120. /* Reset localRVT early because some RVTs might be registered again. */
  2121. ctxt->localRVT = base;
  2122. if (base != NULL)
  2123. base->prev = NULL;
  2124. do {
  2125. tmp = cur;
  2126. cur = (xmlDocPtr) cur->next;
  2127. if (tmp->psvi == XSLT_RVT_LOCAL) {
  2128. xsltReleaseRVT(ctxt, tmp);
  2129. } else if (tmp->psvi == XSLT_RVT_GLOBAL) {
  2130. xsltRegisterPersistRVT(ctxt, tmp);
  2131. } else if (tmp->psvi == XSLT_RVT_FUNC_RESULT) {
  2132. /*
  2133. * This will either register the RVT again or move it to the
  2134. * context variable.
  2135. */
  2136. xsltRegisterLocalRVT(ctxt, tmp);
  2137. tmp->psvi = XSLT_RVT_FUNC_RESULT;
  2138. } else {
  2139. xmlGenericError(xmlGenericErrorContext,
  2140. "xsltReleaseLocalRVTs: Unexpected RVT flag %p\n",
  2141. tmp->psvi);
  2142. }
  2143. } while (cur != base);
  2144. }
  2145. /**
  2146. * xsltApplySequenceConstructor:
  2147. * @ctxt: a XSLT process context
  2148. * @contextNode: the "current node" in the source tree
  2149. * @list: the nodes of a sequence constructor;
  2150. * (plus leading xsl:param elements)
  2151. * @templ: the compiled xsl:template (optional)
  2152. *
  2153. * Processes a sequence constructor.
  2154. *
  2155. * NOTE: ctxt->currentTemplateRule was introduced to reflect the
  2156. * semantics of "current template rule". I.e. the field ctxt->templ
  2157. * is not intended to reflect this, thus always pushed onto the
  2158. * template stack.
  2159. */
  2160. static void
  2161. xsltApplySequenceConstructor(xsltTransformContextPtr ctxt,
  2162. xmlNodePtr contextNode, xmlNodePtr list,
  2163. xsltTemplatePtr templ)
  2164. {
  2165. xmlNodePtr oldInsert, oldInst, oldCurInst, oldContextNode;
  2166. xmlNodePtr cur, insert, copy = NULL;
  2167. int level = 0, oldVarsNr;
  2168. xmlDocPtr oldLocalFragmentTop;
  2169. #ifdef XSLT_REFACTORED
  2170. xsltStylePreCompPtr info;
  2171. #endif
  2172. #ifdef WITH_DEBUGGER
  2173. int addCallResult = 0;
  2174. xmlNodePtr debuggedNode = NULL;
  2175. #endif
  2176. if (ctxt == NULL)
  2177. return;
  2178. #ifdef WITH_DEBUGGER
  2179. if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
  2180. debuggedNode =
  2181. xsltDebuggerStartSequenceConstructor(ctxt, contextNode,
  2182. list, templ, &addCallResult);
  2183. if (debuggedNode == NULL)
  2184. return;
  2185. }
  2186. #endif
  2187. if (list == NULL)
  2188. return;
  2189. CHECK_STOPPED;
  2190. /*
  2191. * Check for infinite recursion: stop if the maximum of nested templates
  2192. * is excceeded. Adjust xsltMaxDepth if you need more.
  2193. */
  2194. if (ctxt->depth >= ctxt->maxTemplateDepth) {
  2195. xsltTransformError(ctxt, NULL, list,
  2196. "xsltApplySequenceConstructor: A potential infinite template "
  2197. "recursion was detected.\n"
  2198. "You can adjust xsltMaxDepth (--maxdepth) in order to "
  2199. "raise the maximum number of nested template calls and "
  2200. "variables/params (currently set to %d).\n",
  2201. ctxt->maxTemplateDepth);
  2202. xsltDebug(ctxt, contextNode, list, NULL);
  2203. ctxt->state = XSLT_STATE_STOPPED;
  2204. return;
  2205. }
  2206. ctxt->depth++;
  2207. oldLocalFragmentTop = ctxt->localRVT;
  2208. oldInsert = insert = ctxt->insert;
  2209. oldInst = oldCurInst = ctxt->inst;
  2210. oldContextNode = ctxt->node;
  2211. /*
  2212. * Save current number of variables on the stack; new vars are popped when
  2213. * exiting.
  2214. */
  2215. oldVarsNr = ctxt->varsNr;
  2216. /*
  2217. * Process the sequence constructor.
  2218. */
  2219. cur = list;
  2220. while (cur != NULL) {
  2221. if (ctxt->opLimit != 0) {
  2222. if (ctxt->opCount >= ctxt->opLimit) {
  2223. xsltTransformError(ctxt, NULL, cur,
  2224. "xsltApplySequenceConstructor: "
  2225. "Operation limit exceeded\n");
  2226. ctxt->state = XSLT_STATE_STOPPED;
  2227. goto error;
  2228. }
  2229. ctxt->opCount += 1;
  2230. }
  2231. ctxt->inst = cur;
  2232. #ifdef WITH_DEBUGGER
  2233. switch (ctxt->debugStatus) {
  2234. case XSLT_DEBUG_RUN_RESTART:
  2235. case XSLT_DEBUG_QUIT:
  2236. break;
  2237. }
  2238. #endif
  2239. /*
  2240. * Test; we must have a valid insertion point.
  2241. */
  2242. if (insert == NULL) {
  2243. #ifdef WITH_XSLT_DEBUG_PROCESS
  2244. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2245. "xsltApplySequenceConstructor: insert == NULL !\n"));
  2246. #endif
  2247. goto error;
  2248. }
  2249. #ifdef WITH_DEBUGGER
  2250. if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (debuggedNode != cur))
  2251. xslHandleDebugger(cur, contextNode, templ, ctxt);
  2252. #endif
  2253. #ifdef XSLT_REFACTORED
  2254. if (cur->type == XML_ELEMENT_NODE) {
  2255. info = (xsltStylePreCompPtr) cur->psvi;
  2256. /*
  2257. * We expect a compiled representation on:
  2258. * 1) XSLT instructions of this XSLT version (1.0)
  2259. * (with a few exceptions)
  2260. * 2) Literal result elements
  2261. * 3) Extension instructions
  2262. * 4) XSLT instructions of future XSLT versions
  2263. * (forwards-compatible mode).
  2264. */
  2265. if (info == NULL) {
  2266. /*
  2267. * Handle the rare cases where we don't expect a compiled
  2268. * representation on an XSLT element.
  2269. */
  2270. if (IS_XSLT_ELEM_FAST(cur) && IS_XSLT_NAME(cur, "message")) {
  2271. xsltMessage(ctxt, contextNode, cur);
  2272. goto skip_children;
  2273. }
  2274. /*
  2275. * Something really went wrong:
  2276. */
  2277. xsltTransformError(ctxt, NULL, cur,
  2278. "Internal error in xsltApplySequenceConstructor(): "
  2279. "The element '%s' in the stylesheet has no compiled "
  2280. "representation.\n",
  2281. cur->name);
  2282. goto skip_children;
  2283. }
  2284. if (info->type == XSLT_FUNC_LITERAL_RESULT_ELEMENT) {
  2285. xsltStyleItemLRElementInfoPtr lrInfo =
  2286. (xsltStyleItemLRElementInfoPtr) info;
  2287. /*
  2288. * Literal result elements
  2289. * --------------------------------------------------------
  2290. */
  2291. #ifdef WITH_XSLT_DEBUG_PROCESS
  2292. XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
  2293. xsltGenericDebug(xsltGenericDebugContext,
  2294. "xsltApplySequenceConstructor: copy literal result "
  2295. "element '%s'\n", cur->name));
  2296. #endif
  2297. /*
  2298. * Copy the raw element-node.
  2299. * OLD: if ((copy = xsltShallowCopyElem(ctxt, cur, insert))
  2300. * == NULL)
  2301. * goto error;
  2302. */
  2303. copy = xmlDocCopyNode(cur, insert->doc, 0);
  2304. if (copy == NULL) {
  2305. xsltTransformError(ctxt, NULL, cur,
  2306. "Internal error in xsltApplySequenceConstructor(): "
  2307. "Failed to copy literal result element '%s'.\n",
  2308. cur->name);
  2309. goto error;
  2310. } else {
  2311. /*
  2312. * Add the element-node to the result tree.
  2313. */
  2314. copy->doc = ctxt->output;
  2315. copy = xsltAddChild(insert, copy);
  2316. /*
  2317. * Create effective namespaces declarations.
  2318. * OLD: xsltCopyNamespaceList(ctxt, copy, cur->nsDef);
  2319. */
  2320. if (lrInfo->effectiveNs != NULL) {
  2321. xsltEffectiveNsPtr effNs = lrInfo->effectiveNs;
  2322. xmlNsPtr ns, lastns = NULL;
  2323. while (effNs != NULL) {
  2324. /*
  2325. * Avoid generating redundant namespace
  2326. * declarations; thus lookup if there is already
  2327. * such a ns-decl in the result.
  2328. */
  2329. ns = xmlSearchNs(copy->doc, copy, effNs->prefix);
  2330. if ((ns != NULL) &&
  2331. (xmlStrEqual(ns->href, effNs->nsName)))
  2332. {
  2333. effNs = effNs->next;
  2334. continue;
  2335. }
  2336. ns = xmlNewNs(copy, effNs->nsName, effNs->prefix);
  2337. if (ns == NULL) {
  2338. xsltTransformError(ctxt, NULL, cur,
  2339. "Internal error in "
  2340. "xsltApplySequenceConstructor(): "
  2341. "Failed to copy a namespace "
  2342. "declaration.\n");
  2343. goto error;
  2344. }
  2345. if (lastns == NULL)
  2346. copy->nsDef = ns;
  2347. else
  2348. lastns->next =ns;
  2349. lastns = ns;
  2350. effNs = effNs->next;
  2351. }
  2352. }
  2353. /*
  2354. * NOTE that we don't need to apply ns-alising: this was
  2355. * already done at compile-time.
  2356. */
  2357. if (cur->ns != NULL) {
  2358. /*
  2359. * If there's no such ns-decl in the result tree,
  2360. * then xsltGetSpecialNamespace() will
  2361. * create a ns-decl on the copied node.
  2362. */
  2363. copy->ns = xsltGetSpecialNamespace(ctxt, cur,
  2364. cur->ns->href, cur->ns->prefix, copy);
  2365. } else {
  2366. /*
  2367. * Undeclare the default namespace if needed.
  2368. * This can be skipped, if the result element has
  2369. * no ns-decls, in which case the result element
  2370. * obviously does not declare a default namespace;
  2371. * AND there's either no parent, or the parent
  2372. * element is in no namespace; this means there's no
  2373. * default namespace is scope to care about.
  2374. *
  2375. * REVISIT: This might result in massive
  2376. * generation of ns-decls if nodes in a default
  2377. * namespaces are mixed with nodes in no namespace.
  2378. *
  2379. */
  2380. if (copy->nsDef ||
  2381. ((insert != NULL) &&
  2382. (insert->type == XML_ELEMENT_NODE) &&
  2383. (insert->ns != NULL)))
  2384. {
  2385. xsltGetSpecialNamespace(ctxt, cur,
  2386. NULL, NULL, copy);
  2387. }
  2388. }
  2389. }
  2390. /*
  2391. * SPEC XSLT 2.0 "Each attribute of the literal result
  2392. * element, other than an attribute in the XSLT namespace,
  2393. * is processed to produce an attribute for the element in
  2394. * the result tree."
  2395. * NOTE: See bug #341325.
  2396. */
  2397. if (cur->properties != NULL) {
  2398. xsltAttrListTemplateProcess(ctxt, copy, cur->properties);
  2399. }
  2400. } else if (IS_XSLT_ELEM_FAST(cur)) {
  2401. /*
  2402. * XSLT instructions
  2403. * --------------------------------------------------------
  2404. */
  2405. if (info->type == XSLT_FUNC_UNKOWN_FORWARDS_COMPAT) {
  2406. /*
  2407. * We hit an unknown XSLT element.
  2408. * Try to apply one of the fallback cases.
  2409. */
  2410. ctxt->insert = insert;
  2411. if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
  2412. xsltTransformError(ctxt, NULL, cur,
  2413. "The is no fallback behaviour defined for "
  2414. "the unknown XSLT element '%s'.\n",
  2415. cur->name);
  2416. }
  2417. ctxt->insert = oldInsert;
  2418. } else if (info->func != NULL) {
  2419. /*
  2420. * Execute the XSLT instruction.
  2421. */
  2422. ctxt->insert = insert;
  2423. info->func(ctxt, contextNode, cur,
  2424. (xsltElemPreCompPtr) info);
  2425. /*
  2426. * Cleanup temporary tree fragments.
  2427. */
  2428. if (oldLocalFragmentTop != ctxt->localRVT)
  2429. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  2430. ctxt->insert = oldInsert;
  2431. } else if (info->type == XSLT_FUNC_VARIABLE) {
  2432. xsltStackElemPtr tmpvar = ctxt->vars;
  2433. xsltParseStylesheetVariable(ctxt, cur);
  2434. if (tmpvar != ctxt->vars) {
  2435. /*
  2436. * TODO: Using a @tmpvar is an annoying workaround, but
  2437. * the current mechanisms do not provide any other way
  2438. * of knowing if the var was really pushed onto the
  2439. * stack.
  2440. */
  2441. ctxt->vars->level = level;
  2442. }
  2443. } else if (info->type == XSLT_FUNC_MESSAGE) {
  2444. /*
  2445. * TODO: Won't be hit, since we don't compile xsl:message.
  2446. */
  2447. xsltMessage(ctxt, contextNode, cur);
  2448. } else {
  2449. xsltTransformError(ctxt, NULL, cur,
  2450. "Unexpected XSLT element '%s'.\n", cur->name);
  2451. }
  2452. goto skip_children;
  2453. } else {
  2454. xsltTransformFunction func;
  2455. /*
  2456. * Extension intructions (elements)
  2457. * --------------------------------------------------------
  2458. */
  2459. if (cur->psvi == xsltExtMarker) {
  2460. /*
  2461. * The xsltExtMarker was set during the compilation
  2462. * of extension instructions if there was no registered
  2463. * handler for this specific extension function at
  2464. * compile-time.
  2465. * Libxslt will now lookup if a handler is
  2466. * registered in the context of this transformation.
  2467. */
  2468. func = xsltExtElementLookup(ctxt, cur->name,
  2469. cur->ns->href);
  2470. } else
  2471. func = ((xsltElemPreCompPtr) cur->psvi)->func;
  2472. if (func == NULL) {
  2473. /*
  2474. * No handler available.
  2475. * Try to execute fallback behaviour via xsl:fallback.
  2476. */
  2477. #ifdef WITH_XSLT_DEBUG_PROCESS
  2478. XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
  2479. xsltGenericDebug(xsltGenericDebugContext,
  2480. "xsltApplySequenceConstructor: unknown extension %s\n",
  2481. cur->name));
  2482. #endif
  2483. ctxt->insert = insert;
  2484. if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
  2485. xsltTransformError(ctxt, NULL, cur,
  2486. "Unknown extension instruction '{%s}%s'.\n",
  2487. cur->ns->href, cur->name);
  2488. }
  2489. ctxt->insert = oldInsert;
  2490. } else {
  2491. /*
  2492. * Execute the handler-callback.
  2493. */
  2494. #ifdef WITH_XSLT_DEBUG_PROCESS
  2495. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2496. "xsltApplySequenceConstructor: extension construct %s\n",
  2497. cur->name));
  2498. #endif
  2499. /*
  2500. * Disable the xsltCopyTextString optimization for
  2501. * extension elements. Extensions could append text using
  2502. * xmlAddChild which will free the buffer pointed to by
  2503. * 'lasttext'. This buffer could later be reallocated with
  2504. * a different size than recorded in 'lasttsize'. See bug
  2505. * #777432.
  2506. */
  2507. if (cur->psvi == xsltExtMarker) {
  2508. ctxt->lasttext = NULL;
  2509. }
  2510. ctxt->insert = insert;
  2511. func(ctxt, contextNode, cur, cur->psvi);
  2512. /*
  2513. * Cleanup temporary tree fragments.
  2514. */
  2515. if (oldLocalFragmentTop != ctxt->localRVT)
  2516. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  2517. ctxt->insert = oldInsert;
  2518. }
  2519. goto skip_children;
  2520. }
  2521. } else if (XSLT_IS_TEXT_NODE(cur)) {
  2522. /*
  2523. * Text
  2524. * ------------------------------------------------------------
  2525. */
  2526. #ifdef WITH_XSLT_DEBUG_PROCESS
  2527. if (cur->name == xmlStringTextNoenc) {
  2528. XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
  2529. xsltGenericDebug(xsltGenericDebugContext,
  2530. "xsltApplySequenceConstructor: copy unescaped text '%s'\n",
  2531. cur->content));
  2532. } else {
  2533. XSLT_TRACE(ctxt, XSLT_TRACE_APPLY_TEMPLATE,
  2534. xsltGenericDebug(xsltGenericDebugContext,
  2535. "xsltApplySequenceConstructor: copy text '%s'\n",
  2536. cur->content));
  2537. }
  2538. #endif
  2539. if (xsltCopyText(ctxt, insert, cur, ctxt->internalized) == NULL)
  2540. goto error;
  2541. }
  2542. #else /* XSLT_REFACTORED */
  2543. if (IS_XSLT_ELEM(cur)) {
  2544. /*
  2545. * This is an XSLT node
  2546. */
  2547. xsltStylePreCompPtr info = (xsltStylePreCompPtr) cur->psvi;
  2548. if (info == NULL) {
  2549. if (IS_XSLT_NAME(cur, "message")) {
  2550. xsltMessage(ctxt, contextNode, cur);
  2551. } else {
  2552. /*
  2553. * That's an error try to apply one of the fallback cases
  2554. */
  2555. ctxt->insert = insert;
  2556. if (!xsltApplyFallbacks(ctxt, contextNode, cur)) {
  2557. xsltGenericError(xsltGenericErrorContext,
  2558. "xsltApplySequenceConstructor: %s was not compiled\n",
  2559. cur->name);
  2560. }
  2561. ctxt->insert = oldInsert;
  2562. }
  2563. goto skip_children;
  2564. }
  2565. if (info->func != NULL) {
  2566. oldCurInst = ctxt->inst;
  2567. ctxt->inst = cur;
  2568. ctxt->insert = insert;
  2569. info->func(ctxt, contextNode, cur, (xsltElemPreCompPtr) info);
  2570. /*
  2571. * Cleanup temporary tree fragments.
  2572. */
  2573. if (oldLocalFragmentTop != ctxt->localRVT)
  2574. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  2575. ctxt->insert = oldInsert;
  2576. ctxt->inst = oldCurInst;
  2577. goto skip_children;
  2578. }
  2579. if (IS_XSLT_NAME(cur, "variable")) {
  2580. xsltStackElemPtr tmpvar = ctxt->vars;
  2581. oldCurInst = ctxt->inst;
  2582. ctxt->inst = cur;
  2583. xsltParseStylesheetVariable(ctxt, cur);
  2584. ctxt->inst = oldCurInst;
  2585. if (tmpvar != ctxt->vars) {
  2586. /*
  2587. * TODO: Using a @tmpvar is an annoying workaround, but
  2588. * the current mechanisms do not provide any other way
  2589. * of knowing if the var was really pushed onto the
  2590. * stack.
  2591. */
  2592. ctxt->vars->level = level;
  2593. }
  2594. } else if (IS_XSLT_NAME(cur, "message")) {
  2595. xsltMessage(ctxt, contextNode, cur);
  2596. } else {
  2597. xsltTransformError(ctxt, NULL, cur,
  2598. "Unexpected XSLT element '%s'.\n", cur->name);
  2599. }
  2600. goto skip_children;
  2601. } else if ((cur->type == XML_TEXT_NODE) ||
  2602. (cur->type == XML_CDATA_SECTION_NODE)) {
  2603. /*
  2604. * This text comes from the stylesheet
  2605. * For stylesheets, the set of whitespace-preserving
  2606. * element names consists of just xsl:text.
  2607. */
  2608. #ifdef WITH_XSLT_DEBUG_PROCESS
  2609. if (cur->type == XML_CDATA_SECTION_NODE) {
  2610. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2611. "xsltApplySequenceConstructor: copy CDATA text %s\n",
  2612. cur->content));
  2613. } else if (cur->name == xmlStringTextNoenc) {
  2614. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2615. "xsltApplySequenceConstructor: copy unescaped text %s\n",
  2616. cur->content));
  2617. } else {
  2618. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2619. "xsltApplySequenceConstructor: copy text %s\n",
  2620. cur->content));
  2621. }
  2622. #endif
  2623. if (xsltCopyText(ctxt, insert, cur, ctxt->internalized) == NULL)
  2624. goto error;
  2625. } else if ((cur->type == XML_ELEMENT_NODE) &&
  2626. (cur->ns != NULL) && (cur->psvi != NULL)) {
  2627. xsltTransformFunction function;
  2628. oldCurInst = ctxt->inst;
  2629. ctxt->inst = cur;
  2630. /*
  2631. * Flagged as an extension element
  2632. */
  2633. if (cur->psvi == xsltExtMarker)
  2634. function = xsltExtElementLookup(ctxt, cur->name,
  2635. cur->ns->href);
  2636. else
  2637. function = ((xsltElemPreCompPtr) cur->psvi)->func;
  2638. if (function == NULL) {
  2639. xmlNodePtr child;
  2640. int found = 0;
  2641. #ifdef WITH_XSLT_DEBUG_PROCESS
  2642. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2643. "xsltApplySequenceConstructor: unknown extension %s\n",
  2644. cur->name));
  2645. #endif
  2646. /*
  2647. * Search if there are fallbacks
  2648. */
  2649. ctxt->insert = insert;
  2650. child = cur->children;
  2651. while (child != NULL) {
  2652. if ((IS_XSLT_ELEM(child)) &&
  2653. (IS_XSLT_NAME(child, "fallback")))
  2654. {
  2655. found = 1;
  2656. xsltApplySequenceConstructor(ctxt, contextNode,
  2657. child->children, NULL);
  2658. }
  2659. child = child->next;
  2660. }
  2661. ctxt->insert = oldInsert;
  2662. if (!found) {
  2663. xsltTransformError(ctxt, NULL, cur,
  2664. "xsltApplySequenceConstructor: failed to find extension %s\n",
  2665. cur->name);
  2666. }
  2667. } else {
  2668. #ifdef WITH_XSLT_DEBUG_PROCESS
  2669. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2670. "xsltApplySequenceConstructor: extension construct %s\n",
  2671. cur->name));
  2672. #endif
  2673. /*
  2674. * Disable the xsltCopyTextString optimization for
  2675. * extension elements. Extensions could append text using
  2676. * xmlAddChild which will free the buffer pointed to by
  2677. * 'lasttext'. This buffer could later be reallocated with
  2678. * a different size than recorded in 'lasttsize'. See bug
  2679. * #777432.
  2680. */
  2681. if (cur->psvi == xsltExtMarker) {
  2682. ctxt->lasttext = NULL;
  2683. }
  2684. ctxt->insert = insert;
  2685. function(ctxt, contextNode, cur, cur->psvi);
  2686. /*
  2687. * Cleanup temporary tree fragments.
  2688. */
  2689. if (oldLocalFragmentTop != ctxt->localRVT)
  2690. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  2691. ctxt->insert = oldInsert;
  2692. }
  2693. ctxt->inst = oldCurInst;
  2694. goto skip_children;
  2695. } else if (cur->type == XML_ELEMENT_NODE) {
  2696. #ifdef WITH_XSLT_DEBUG_PROCESS
  2697. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2698. "xsltApplySequenceConstructor: copy node %s\n",
  2699. cur->name));
  2700. #endif
  2701. oldCurInst = ctxt->inst;
  2702. ctxt->inst = cur;
  2703. if ((copy = xsltShallowCopyElem(ctxt, cur, insert, 1)) == NULL)
  2704. goto error;
  2705. /*
  2706. * Add extra namespaces inherited from the current template
  2707. * if we are in the first level children and this is a
  2708. * "real" template.
  2709. */
  2710. if ((templ != NULL) && (oldInsert == insert) &&
  2711. (ctxt->templ != NULL) && (ctxt->templ->inheritedNs != NULL)) {
  2712. int i;
  2713. xmlNsPtr ns, ret;
  2714. for (i = 0; i < ctxt->templ->inheritedNsNr; i++) {
  2715. const xmlChar *URI = NULL;
  2716. xsltStylesheetPtr style;
  2717. ns = ctxt->templ->inheritedNs[i];
  2718. /* Note that the XSLT namespace was already excluded
  2719. * in xsltGetInheritedNsList().
  2720. */
  2721. #if 0
  2722. if (xmlStrEqual(ns->href, XSLT_NAMESPACE))
  2723. continue;
  2724. #endif
  2725. style = ctxt->style;
  2726. while (style != NULL) {
  2727. if (style->nsAliases != NULL)
  2728. URI = (const xmlChar *)
  2729. xmlHashLookup(style->nsAliases, ns->href);
  2730. if (URI != NULL)
  2731. break;
  2732. style = xsltNextImport(style);
  2733. }
  2734. if (URI == UNDEFINED_DEFAULT_NS)
  2735. continue;
  2736. if (URI == NULL)
  2737. URI = ns->href;
  2738. /*
  2739. * TODO: The following will still be buggy for the
  2740. * non-refactored code.
  2741. */
  2742. ret = xmlSearchNs(copy->doc, copy, ns->prefix);
  2743. if ((ret == NULL) || (!xmlStrEqual(ret->href, URI)))
  2744. {
  2745. xmlNewNs(copy, URI, ns->prefix);
  2746. }
  2747. }
  2748. if (copy->ns != NULL) {
  2749. /*
  2750. * Fix the node namespace if needed
  2751. */
  2752. copy->ns = xsltGetNamespace(ctxt, cur, copy->ns, copy);
  2753. }
  2754. }
  2755. /*
  2756. * all the attributes are directly inherited
  2757. */
  2758. if (cur->properties != NULL) {
  2759. xsltAttrListTemplateProcess(ctxt, copy, cur->properties);
  2760. }
  2761. ctxt->inst = oldCurInst;
  2762. }
  2763. #endif /* else of XSLT_REFACTORED */
  2764. /*
  2765. * Descend into content in document order.
  2766. */
  2767. if (cur->children != NULL) {
  2768. if (cur->children->type != XML_ENTITY_DECL) {
  2769. cur = cur->children;
  2770. level++;
  2771. if (copy != NULL)
  2772. insert = copy;
  2773. continue;
  2774. }
  2775. }
  2776. skip_children:
  2777. /*
  2778. * If xslt:message was just processed, we might have hit a
  2779. * terminate='yes'; if so, then break the loop and clean up.
  2780. * TODO: Do we need to check this also before trying to descend
  2781. * into the content?
  2782. */
  2783. if (ctxt->state == XSLT_STATE_STOPPED)
  2784. break;
  2785. if (cur->next != NULL) {
  2786. cur = cur->next;
  2787. continue;
  2788. }
  2789. do {
  2790. cur = cur->parent;
  2791. level--;
  2792. /*
  2793. * Pop variables/params (xsl:variable and xsl:param).
  2794. */
  2795. if ((ctxt->varsNr > oldVarsNr) && (ctxt->vars->level > level)) {
  2796. xsltLocalVariablePop(ctxt, oldVarsNr, level);
  2797. }
  2798. insert = insert->parent;
  2799. if (cur == NULL)
  2800. break;
  2801. if (cur == list->parent) {
  2802. cur = NULL;
  2803. break;
  2804. }
  2805. if (cur->next != NULL) {
  2806. cur = cur->next;
  2807. break;
  2808. }
  2809. } while (cur != NULL);
  2810. }
  2811. error:
  2812. /*
  2813. * In case of errors: pop remaining variables.
  2814. */
  2815. if (ctxt->varsNr > oldVarsNr)
  2816. xsltLocalVariablePop(ctxt, oldVarsNr, -1);
  2817. ctxt->node = oldContextNode;
  2818. ctxt->inst = oldInst;
  2819. ctxt->insert = oldInsert;
  2820. ctxt->depth--;
  2821. #ifdef WITH_DEBUGGER
  2822. if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (addCallResult)) {
  2823. xslDropCall();
  2824. }
  2825. #endif
  2826. }
  2827. /*
  2828. * xsltApplyXSLTTemplate:
  2829. * @ctxt: a XSLT transformation context
  2830. * @contextNode: the node in the source tree.
  2831. * @list: the nodes of a sequence constructor;
  2832. * (plus leading xsl:param elements)
  2833. * @templ: the compiled xsl:template declaration;
  2834. * NULL if a sequence constructor
  2835. * @withParams: a set of caller-parameters (xsl:with-param) or NULL
  2836. *
  2837. * Called by:
  2838. * - xsltApplyImports()
  2839. * - xsltCallTemplate()
  2840. * - xsltDefaultProcessOneNode()
  2841. * - xsltProcessOneNode()
  2842. */
  2843. static void
  2844. xsltApplyXSLTTemplate(xsltTransformContextPtr ctxt,
  2845. xmlNodePtr contextNode,
  2846. xmlNodePtr list,
  2847. xsltTemplatePtr templ,
  2848. xsltStackElemPtr withParams)
  2849. {
  2850. int oldVarsBase = 0;
  2851. xmlNodePtr cur;
  2852. xsltStackElemPtr tmpParam = NULL;
  2853. xmlDocPtr oldUserFragmentTop;
  2854. #ifdef WITH_PROFILER
  2855. long start = 0;
  2856. #endif
  2857. #ifdef XSLT_REFACTORED
  2858. xsltStyleItemParamPtr iparam;
  2859. #else
  2860. xsltStylePreCompPtr iparam;
  2861. #endif
  2862. #ifdef WITH_DEBUGGER
  2863. int addCallResult = 0;
  2864. #endif
  2865. if (ctxt == NULL)
  2866. return;
  2867. if (templ == NULL) {
  2868. xsltTransformError(ctxt, NULL, list,
  2869. "xsltApplyXSLTTemplate: Bad arguments; @templ is mandatory.\n");
  2870. return;
  2871. }
  2872. #ifdef WITH_DEBUGGER
  2873. if (ctxt->debugStatus != XSLT_DEBUG_NONE) {
  2874. if (xsltDebuggerStartSequenceConstructor(ctxt, contextNode,
  2875. list, templ, &addCallResult) == NULL)
  2876. return;
  2877. }
  2878. #endif
  2879. if (list == NULL)
  2880. return;
  2881. CHECK_STOPPED;
  2882. if (ctxt->varsNr >= ctxt->maxTemplateVars)
  2883. {
  2884. xsltTransformError(ctxt, NULL, list,
  2885. "xsltApplyXSLTTemplate: A potential infinite template recursion "
  2886. "was detected.\n"
  2887. "You can adjust maxTemplateVars (--maxvars) in order to "
  2888. "raise the maximum number of variables/params (currently set to %d).\n",
  2889. ctxt->maxTemplateVars);
  2890. xsltDebug(ctxt, contextNode, list, NULL);
  2891. ctxt->state = XSLT_STATE_STOPPED;
  2892. return;
  2893. }
  2894. oldUserFragmentTop = ctxt->tmpRVT;
  2895. ctxt->tmpRVT = NULL;
  2896. /*
  2897. * Initiate a distinct scope of local params/variables.
  2898. */
  2899. oldVarsBase = ctxt->varsBase;
  2900. ctxt->varsBase = ctxt->varsNr;
  2901. ctxt->node = contextNode;
  2902. #ifdef WITH_PROFILER
  2903. if (ctxt->profile) {
  2904. templ->nbCalls++;
  2905. start = xsltTimestamp();
  2906. profPush(ctxt, 0);
  2907. profCallgraphAdd(templ, ctxt->templ);
  2908. }
  2909. #endif
  2910. /*
  2911. * Push the xsl:template declaration onto the stack.
  2912. */
  2913. templPush(ctxt, templ);
  2914. #ifdef WITH_XSLT_DEBUG_PROCESS
  2915. if (templ->name != NULL)
  2916. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  2917. "applying xsl:template '%s'\n", templ->name));
  2918. #endif
  2919. /*
  2920. * Process xsl:param instructions and skip those elements for
  2921. * further processing.
  2922. */
  2923. cur = list;
  2924. do {
  2925. if (cur->type == XML_TEXT_NODE) {
  2926. cur = cur->next;
  2927. continue;
  2928. }
  2929. if ((cur->type != XML_ELEMENT_NODE) ||
  2930. (cur->name[0] != 'p') ||
  2931. (cur->psvi == NULL) ||
  2932. (! xmlStrEqual(cur->name, BAD_CAST "param")) ||
  2933. (! IS_XSLT_ELEM(cur)))
  2934. {
  2935. break;
  2936. }
  2937. list = cur->next;
  2938. #ifdef XSLT_REFACTORED
  2939. iparam = (xsltStyleItemParamPtr) cur->psvi;
  2940. #else
  2941. iparam = (xsltStylePreCompPtr) cur->psvi;
  2942. #endif
  2943. /*
  2944. * Substitute xsl:param for a given xsl:with-param.
  2945. * Since the XPath expression will reference the params/vars
  2946. * by index, we need to slot the xsl:with-params in the
  2947. * order of encountered xsl:params to keep the sequence of
  2948. * params/variables in the stack exactly as it was at
  2949. * compile time,
  2950. */
  2951. tmpParam = NULL;
  2952. if (withParams) {
  2953. tmpParam = withParams;
  2954. do {
  2955. if ((tmpParam->name == (iparam->name)) &&
  2956. (tmpParam->nameURI == (iparam->ns)))
  2957. {
  2958. /*
  2959. * Push the caller-parameter.
  2960. */
  2961. xsltLocalVariablePush(ctxt, tmpParam, -1);
  2962. break;
  2963. }
  2964. tmpParam = tmpParam->next;
  2965. } while (tmpParam != NULL);
  2966. }
  2967. /*
  2968. * Push the xsl:param.
  2969. */
  2970. if (tmpParam == NULL) {
  2971. /*
  2972. * Note that we must assume that the added parameter
  2973. * has a @depth of 0.
  2974. */
  2975. xsltParseStylesheetParam(ctxt, cur);
  2976. }
  2977. cur = cur->next;
  2978. } while (cur != NULL);
  2979. /*
  2980. * Process the sequence constructor.
  2981. */
  2982. xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
  2983. /*
  2984. * Remove remaining xsl:param and xsl:with-param items from
  2985. * the stack. Don't free xsl:with-param items.
  2986. */
  2987. if (ctxt->varsNr > ctxt->varsBase)
  2988. xsltTemplateParamsCleanup(ctxt);
  2989. ctxt->varsBase = oldVarsBase;
  2990. /*
  2991. * Release user-created fragments stored in the scope
  2992. * of xsl:template. Note that this mechanism is deprecated:
  2993. * user code should now use xsltRegisterLocalRVT() instead
  2994. * of the obsolete xsltRegisterTmpRVT().
  2995. */
  2996. if (ctxt->tmpRVT) {
  2997. xmlDocPtr curdoc = ctxt->tmpRVT, tmp;
  2998. while (curdoc != NULL) {
  2999. tmp = curdoc;
  3000. curdoc = (xmlDocPtr) curdoc->next;
  3001. xsltReleaseRVT(ctxt, tmp);
  3002. }
  3003. }
  3004. ctxt->tmpRVT = oldUserFragmentTop;
  3005. /*
  3006. * Pop the xsl:template declaration from the stack.
  3007. */
  3008. templPop(ctxt);
  3009. #ifdef WITH_PROFILER
  3010. if (ctxt->profile) {
  3011. long spent, child, total, end;
  3012. end = xsltTimestamp();
  3013. child = profPop(ctxt);
  3014. total = end - start;
  3015. spent = total - child;
  3016. if (spent <= 0) {
  3017. /*
  3018. * Not possible unless the original calibration failed
  3019. * we can try to correct it on the fly.
  3020. */
  3021. xsltCalibrateAdjust(spent);
  3022. spent = 0;
  3023. }
  3024. templ->time += spent;
  3025. if (ctxt->profNr > 0)
  3026. ctxt->profTab[ctxt->profNr - 1] += total;
  3027. }
  3028. #endif
  3029. #ifdef WITH_DEBUGGER
  3030. if ((ctxt->debugStatus != XSLT_DEBUG_NONE) && (addCallResult)) {
  3031. xslDropCall();
  3032. }
  3033. #endif
  3034. }
  3035. /**
  3036. * xsltApplyOneTemplate:
  3037. * @ctxt: a XSLT process context
  3038. * @contextNode: the node in the source tree.
  3039. * @list: the nodes of a sequence constructor
  3040. * @templ: not used
  3041. * @params: a set of parameters (xsl:param) or NULL
  3042. *
  3043. * Processes a sequence constructor on the current node in the source tree.
  3044. *
  3045. * @params are the already computed variable stack items; this function
  3046. * pushes them on the variable stack, and pops them before exiting; it's
  3047. * left to the caller to free or reuse @params afterwards. The initial
  3048. * states of the variable stack will always be restored before this
  3049. * function exits.
  3050. * NOTE that this does *not* initiate a new distinct variable scope; i.e.
  3051. * variables already on the stack are visible to the process. The caller's
  3052. * side needs to start a new variable scope if needed (e.g. in exsl:function).
  3053. *
  3054. * @templ is obsolete and not used anymore (e.g. <exslt:function> does not
  3055. * provide a @templ); a non-NULL @templ might raise an error in the future.
  3056. *
  3057. * BIG NOTE: This function is not intended to process the content of an
  3058. * xsl:template; it does not expect xsl:param instructions in @list and
  3059. * will report errors if found.
  3060. *
  3061. * Called by:
  3062. * - xsltEvalVariable() (variables.c)
  3063. * - exsltFuncFunctionFunction() (libexsl/functions.c)
  3064. */
  3065. void
  3066. xsltApplyOneTemplate(xsltTransformContextPtr ctxt,
  3067. xmlNodePtr contextNode,
  3068. xmlNodePtr list,
  3069. xsltTemplatePtr templ ATTRIBUTE_UNUSED,
  3070. xsltStackElemPtr params)
  3071. {
  3072. if ((ctxt == NULL) || (list == NULL))
  3073. return;
  3074. CHECK_STOPPED;
  3075. if (params) {
  3076. /*
  3077. * This code should be obsolete - was previously used
  3078. * by libexslt/functions.c, but due to bug 381319 the
  3079. * logic there was changed.
  3080. */
  3081. int oldVarsNr = ctxt->varsNr;
  3082. /*
  3083. * Push the given xsl:param(s) onto the variable stack.
  3084. */
  3085. while (params != NULL) {
  3086. xsltLocalVariablePush(ctxt, params, -1);
  3087. params = params->next;
  3088. }
  3089. xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
  3090. /*
  3091. * Pop the given xsl:param(s) from the stack but don't free them.
  3092. */
  3093. xsltLocalVariablePop(ctxt, oldVarsNr, -2);
  3094. } else
  3095. xsltApplySequenceConstructor(ctxt, contextNode, list, templ);
  3096. }
  3097. /************************************************************************
  3098. * *
  3099. * XSLT-1.1 extensions *
  3100. * *
  3101. ************************************************************************/
  3102. /**
  3103. * xsltDocumentElem:
  3104. * @ctxt: an XSLT processing context
  3105. * @node: The current node
  3106. * @inst: the instruction in the stylesheet
  3107. * @castedComp: precomputed information
  3108. *
  3109. * Process an EXSLT/XSLT-1.1 document element
  3110. */
  3111. void
  3112. xsltDocumentElem(xsltTransformContextPtr ctxt, xmlNodePtr node,
  3113. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  3114. {
  3115. #ifdef XSLT_REFACTORED
  3116. xsltStyleItemDocumentPtr comp = (xsltStyleItemDocumentPtr) castedComp;
  3117. #else
  3118. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  3119. #endif
  3120. xsltStylesheetPtr style = NULL;
  3121. int ret;
  3122. xmlChar *filename = NULL, *prop, *elements;
  3123. xmlChar *element, *end;
  3124. xmlDocPtr res = NULL;
  3125. xmlDocPtr oldOutput;
  3126. xmlNodePtr oldInsert, root;
  3127. const char *oldOutputFile;
  3128. xsltOutputType oldType;
  3129. xmlChar *URL = NULL;
  3130. const xmlChar *method;
  3131. const xmlChar *doctypePublic;
  3132. const xmlChar *doctypeSystem;
  3133. const xmlChar *version;
  3134. const xmlChar *encoding;
  3135. int redirect_write_append = 0;
  3136. if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
  3137. return;
  3138. if (comp->filename == NULL) {
  3139. if (xmlStrEqual(inst->name, (const xmlChar *) "output")) {
  3140. /*
  3141. * The element "output" is in the namespace XSLT_SAXON_NAMESPACE
  3142. * (http://icl.com/saxon)
  3143. * The @file is in no namespace.
  3144. */
  3145. #ifdef WITH_XSLT_DEBUG_EXTRA
  3146. xsltGenericDebug(xsltGenericDebugContext,
  3147. "Found saxon:output extension\n");
  3148. #endif
  3149. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3150. (const xmlChar *) "file",
  3151. XSLT_SAXON_NAMESPACE);
  3152. if (URL == NULL)
  3153. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3154. (const xmlChar *) "href",
  3155. XSLT_SAXON_NAMESPACE);
  3156. } else if (xmlStrEqual(inst->name, (const xmlChar *) "write")) {
  3157. #ifdef WITH_XSLT_DEBUG_EXTRA
  3158. xsltGenericDebug(xsltGenericDebugContext,
  3159. "Found xalan:write extension\n");
  3160. #endif
  3161. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3162. (const xmlChar *)
  3163. "select",
  3164. XSLT_XALAN_NAMESPACE);
  3165. if (URL != NULL) {
  3166. xmlXPathCompExprPtr cmp;
  3167. xmlChar *val;
  3168. /*
  3169. * Trying to handle bug #59212
  3170. * The value of the "select" attribute is an
  3171. * XPath expression.
  3172. * (see http://xml.apache.org/xalan-j/extensionslib.html#redirect)
  3173. */
  3174. cmp = xmlXPathCtxtCompile(ctxt->xpathCtxt, URL);
  3175. val = xsltEvalXPathString(ctxt, cmp);
  3176. xmlXPathFreeCompExpr(cmp);
  3177. xmlFree(URL);
  3178. URL = val;
  3179. }
  3180. if (URL == NULL)
  3181. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3182. (const xmlChar *)
  3183. "file",
  3184. XSLT_XALAN_NAMESPACE);
  3185. if (URL == NULL)
  3186. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3187. (const xmlChar *)
  3188. "href",
  3189. XSLT_XALAN_NAMESPACE);
  3190. } else if (xmlStrEqual(inst->name, (const xmlChar *) "document")) {
  3191. URL = xsltEvalAttrValueTemplate(ctxt, inst,
  3192. (const xmlChar *) "href",
  3193. NULL);
  3194. }
  3195. } else {
  3196. URL = xmlStrdup(comp->filename);
  3197. }
  3198. if (URL == NULL) {
  3199. xsltTransformError(ctxt, NULL, inst,
  3200. "xsltDocumentElem: href/URI-Reference not found\n");
  3201. return;
  3202. }
  3203. /*
  3204. * If the computation failed, it's likely that the URL wasn't escaped
  3205. */
  3206. filename = xmlBuildURI(URL, (const xmlChar *) ctxt->outputFile);
  3207. if (filename == NULL) {
  3208. xmlChar *escURL;
  3209. escURL=xmlURIEscapeStr(URL, BAD_CAST ":/.?,");
  3210. if (escURL != NULL) {
  3211. filename = xmlBuildURI(escURL, (const xmlChar *) ctxt->outputFile);
  3212. xmlFree(escURL);
  3213. }
  3214. }
  3215. if (filename == NULL) {
  3216. xsltTransformError(ctxt, NULL, inst,
  3217. "xsltDocumentElem: URL computation failed for %s\n",
  3218. URL);
  3219. xmlFree(URL);
  3220. return;
  3221. }
  3222. /*
  3223. * Security checking: can we write to this resource
  3224. */
  3225. if (ctxt->sec != NULL) {
  3226. ret = xsltCheckWrite(ctxt->sec, ctxt, filename);
  3227. if (ret <= 0) {
  3228. if (ret == 0)
  3229. xsltTransformError(ctxt, NULL, inst,
  3230. "xsltDocumentElem: write rights for %s denied\n",
  3231. filename);
  3232. xmlFree(URL);
  3233. xmlFree(filename);
  3234. return;
  3235. }
  3236. }
  3237. oldOutputFile = ctxt->outputFile;
  3238. oldOutput = ctxt->output;
  3239. oldInsert = ctxt->insert;
  3240. oldType = ctxt->type;
  3241. ctxt->outputFile = (const char *) filename;
  3242. style = xsltNewStylesheet();
  3243. if (style == NULL) {
  3244. xsltTransformError(ctxt, NULL, inst,
  3245. "xsltDocumentElem: out of memory\n");
  3246. goto error;
  3247. }
  3248. /*
  3249. * Version described in 1.1 draft allows full parameterization
  3250. * of the output.
  3251. */
  3252. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3253. (const xmlChar *) "version",
  3254. NULL);
  3255. if (prop != NULL) {
  3256. if (style->version != NULL)
  3257. xmlFree(style->version);
  3258. style->version = prop;
  3259. }
  3260. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3261. (const xmlChar *) "encoding",
  3262. NULL);
  3263. if (prop != NULL) {
  3264. if (style->encoding != NULL)
  3265. xmlFree(style->encoding);
  3266. style->encoding = prop;
  3267. }
  3268. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3269. (const xmlChar *) "method",
  3270. NULL);
  3271. if (prop != NULL) {
  3272. const xmlChar *URI;
  3273. if (style->method != NULL)
  3274. xmlFree(style->method);
  3275. style->method = NULL;
  3276. if (style->methodURI != NULL)
  3277. xmlFree(style->methodURI);
  3278. style->methodURI = NULL;
  3279. URI = xsltGetQNameURI(inst, &prop);
  3280. if (prop == NULL) {
  3281. if (style != NULL) style->errors++;
  3282. } else if (URI == NULL) {
  3283. if ((xmlStrEqual(prop, (const xmlChar *) "xml")) ||
  3284. (xmlStrEqual(prop, (const xmlChar *) "html")) ||
  3285. (xmlStrEqual(prop, (const xmlChar *) "text"))) {
  3286. style->method = prop;
  3287. } else {
  3288. xsltTransformError(ctxt, NULL, inst,
  3289. "invalid value for method: %s\n", prop);
  3290. if (style != NULL) style->warnings++;
  3291. }
  3292. } else {
  3293. style->method = prop;
  3294. style->methodURI = xmlStrdup(URI);
  3295. }
  3296. }
  3297. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3298. (const xmlChar *)
  3299. "doctype-system", NULL);
  3300. if (prop != NULL) {
  3301. if (style->doctypeSystem != NULL)
  3302. xmlFree(style->doctypeSystem);
  3303. style->doctypeSystem = prop;
  3304. }
  3305. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3306. (const xmlChar *)
  3307. "doctype-public", NULL);
  3308. if (prop != NULL) {
  3309. if (style->doctypePublic != NULL)
  3310. xmlFree(style->doctypePublic);
  3311. style->doctypePublic = prop;
  3312. }
  3313. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3314. (const xmlChar *) "standalone",
  3315. NULL);
  3316. if (prop != NULL) {
  3317. if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
  3318. style->standalone = 1;
  3319. } else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
  3320. style->standalone = 0;
  3321. } else {
  3322. xsltTransformError(ctxt, NULL, inst,
  3323. "invalid value for standalone: %s\n",
  3324. prop);
  3325. if (style != NULL) style->warnings++;
  3326. }
  3327. xmlFree(prop);
  3328. }
  3329. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3330. (const xmlChar *) "indent",
  3331. NULL);
  3332. if (prop != NULL) {
  3333. if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
  3334. style->indent = 1;
  3335. } else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
  3336. style->indent = 0;
  3337. } else {
  3338. xsltTransformError(ctxt, NULL, inst,
  3339. "invalid value for indent: %s\n", prop);
  3340. if (style != NULL) style->warnings++;
  3341. }
  3342. xmlFree(prop);
  3343. }
  3344. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3345. (const xmlChar *)
  3346. "omit-xml-declaration",
  3347. NULL);
  3348. if (prop != NULL) {
  3349. if (xmlStrEqual(prop, (const xmlChar *) "yes")) {
  3350. style->omitXmlDeclaration = 1;
  3351. } else if (xmlStrEqual(prop, (const xmlChar *) "no")) {
  3352. style->omitXmlDeclaration = 0;
  3353. } else {
  3354. xsltTransformError(ctxt, NULL, inst,
  3355. "invalid value for omit-xml-declaration: %s\n",
  3356. prop);
  3357. if (style != NULL) style->warnings++;
  3358. }
  3359. xmlFree(prop);
  3360. }
  3361. elements = xsltEvalAttrValueTemplate(ctxt, inst,
  3362. (const xmlChar *)
  3363. "cdata-section-elements",
  3364. NULL);
  3365. if (elements != NULL) {
  3366. if (style->stripSpaces == NULL)
  3367. style->stripSpaces = xmlHashCreate(10);
  3368. if (style->stripSpaces == NULL) {
  3369. xmlFree(elements);
  3370. return;
  3371. }
  3372. element = elements;
  3373. while (*element != 0) {
  3374. while (IS_BLANK_CH(*element))
  3375. element++;
  3376. if (*element == 0)
  3377. break;
  3378. end = element;
  3379. while ((*end != 0) && (!IS_BLANK_CH(*end)))
  3380. end++;
  3381. element = xmlStrndup(element, end - element);
  3382. if (element) {
  3383. const xmlChar *URI;
  3384. #ifdef WITH_XSLT_DEBUG_PARSING
  3385. xsltGenericDebug(xsltGenericDebugContext,
  3386. "add cdata section output element %s\n",
  3387. element);
  3388. #endif
  3389. URI = xsltGetQNameURI(inst, &element);
  3390. xmlHashAddEntry2(style->stripSpaces, element, URI,
  3391. (xmlChar *) "cdata");
  3392. xmlFree(element);
  3393. }
  3394. element = end;
  3395. }
  3396. xmlFree(elements);
  3397. }
  3398. /*
  3399. * Create a new document tree and process the element template
  3400. */
  3401. XSLT_GET_IMPORT_PTR(method, style, method)
  3402. XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
  3403. XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
  3404. XSLT_GET_IMPORT_PTR(version, style, version)
  3405. XSLT_GET_IMPORT_PTR(encoding, style, encoding)
  3406. if ((method != NULL) &&
  3407. (!xmlStrEqual(method, (const xmlChar *) "xml"))) {
  3408. if (xmlStrEqual(method, (const xmlChar *) "html")) {
  3409. ctxt->type = XSLT_OUTPUT_HTML;
  3410. if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
  3411. res = htmlNewDoc(doctypeSystem, doctypePublic);
  3412. else {
  3413. if (version != NULL) {
  3414. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  3415. xsltGetHTMLIDs(version, &doctypePublic, &doctypeSystem);
  3416. #endif
  3417. }
  3418. res = htmlNewDocNoDtD(doctypeSystem, doctypePublic);
  3419. }
  3420. if (res == NULL)
  3421. goto error;
  3422. res->dict = ctxt->dict;
  3423. xmlDictReference(res->dict);
  3424. } else if (xmlStrEqual(method, (const xmlChar *) "xhtml")) {
  3425. xsltTransformError(ctxt, NULL, inst,
  3426. "xsltDocumentElem: unsupported method xhtml\n");
  3427. ctxt->type = XSLT_OUTPUT_HTML;
  3428. res = htmlNewDocNoDtD(doctypeSystem, doctypePublic);
  3429. if (res == NULL)
  3430. goto error;
  3431. res->dict = ctxt->dict;
  3432. xmlDictReference(res->dict);
  3433. } else if (xmlStrEqual(method, (const xmlChar *) "text")) {
  3434. ctxt->type = XSLT_OUTPUT_TEXT;
  3435. res = xmlNewDoc(style->version);
  3436. if (res == NULL)
  3437. goto error;
  3438. res->dict = ctxt->dict;
  3439. xmlDictReference(res->dict);
  3440. #ifdef WITH_XSLT_DEBUG
  3441. xsltGenericDebug(xsltGenericDebugContext,
  3442. "reusing transformation dict for output\n");
  3443. #endif
  3444. } else {
  3445. xsltTransformError(ctxt, NULL, inst,
  3446. "xsltDocumentElem: unsupported method (%s)\n",
  3447. method);
  3448. goto error;
  3449. }
  3450. } else {
  3451. ctxt->type = XSLT_OUTPUT_XML;
  3452. res = xmlNewDoc(style->version);
  3453. if (res == NULL)
  3454. goto error;
  3455. res->dict = ctxt->dict;
  3456. xmlDictReference(res->dict);
  3457. #ifdef WITH_XSLT_DEBUG
  3458. xsltGenericDebug(xsltGenericDebugContext,
  3459. "reusing transformation dict for output\n");
  3460. #endif
  3461. }
  3462. res->charset = XML_CHAR_ENCODING_UTF8;
  3463. if (encoding != NULL)
  3464. res->encoding = xmlStrdup(encoding);
  3465. ctxt->output = res;
  3466. ctxt->insert = (xmlNodePtr) res;
  3467. xsltApplySequenceConstructor(ctxt, node, inst->children, NULL);
  3468. /*
  3469. * Do some post processing work depending on the generated output
  3470. */
  3471. root = xmlDocGetRootElement(res);
  3472. if (root != NULL) {
  3473. const xmlChar *doctype = NULL;
  3474. if ((root->ns != NULL) && (root->ns->prefix != NULL))
  3475. doctype = xmlDictQLookup(ctxt->dict, root->ns->prefix, root->name);
  3476. if (doctype == NULL)
  3477. doctype = root->name;
  3478. /*
  3479. * Apply the default selection of the method
  3480. */
  3481. if ((method == NULL) &&
  3482. (root->ns == NULL) &&
  3483. (!xmlStrcasecmp(root->name, (const xmlChar *) "html"))) {
  3484. xmlNodePtr tmp;
  3485. tmp = res->children;
  3486. while ((tmp != NULL) && (tmp != root)) {
  3487. if (tmp->type == XML_ELEMENT_NODE)
  3488. break;
  3489. if ((tmp->type == XML_TEXT_NODE) && (!xmlIsBlankNode(tmp)))
  3490. break;
  3491. tmp = tmp->next;
  3492. }
  3493. if (tmp == root) {
  3494. ctxt->type = XSLT_OUTPUT_HTML;
  3495. res->type = XML_HTML_DOCUMENT_NODE;
  3496. if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
  3497. res->intSubset = xmlCreateIntSubset(res, doctype,
  3498. doctypePublic,
  3499. doctypeSystem);
  3500. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  3501. } else if (version != NULL) {
  3502. xsltGetHTMLIDs(version, &doctypePublic,
  3503. &doctypeSystem);
  3504. if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
  3505. res->intSubset =
  3506. xmlCreateIntSubset(res, doctype,
  3507. doctypePublic,
  3508. doctypeSystem);
  3509. #endif
  3510. }
  3511. }
  3512. }
  3513. if (ctxt->type == XSLT_OUTPUT_XML) {
  3514. XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
  3515. XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
  3516. if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
  3517. res->intSubset = xmlCreateIntSubset(res, doctype,
  3518. doctypePublic,
  3519. doctypeSystem);
  3520. }
  3521. }
  3522. /*
  3523. * Calls to redirect:write also take an optional attribute append.
  3524. * Attribute append="true|yes" which will attempt to simply append
  3525. * to an existing file instead of always opening a new file. The
  3526. * default behavior of always overwriting the file still happens
  3527. * if we do not specify append.
  3528. * Note that append use will forbid use of remote URI target.
  3529. */
  3530. prop = xsltEvalAttrValueTemplate(ctxt, inst, (const xmlChar *)"append",
  3531. NULL);
  3532. if (prop != NULL) {
  3533. if (xmlStrEqual(prop, (const xmlChar *) "true") ||
  3534. xmlStrEqual(prop, (const xmlChar *) "yes")) {
  3535. style->omitXmlDeclaration = 1;
  3536. redirect_write_append = 1;
  3537. } else
  3538. style->omitXmlDeclaration = 0;
  3539. xmlFree(prop);
  3540. }
  3541. if (redirect_write_append) {
  3542. FILE *f;
  3543. f = fopen((const char *) filename, "ab");
  3544. if (f == NULL) {
  3545. ret = -1;
  3546. } else {
  3547. ret = xsltSaveResultToFile(f, res, style);
  3548. fclose(f);
  3549. }
  3550. } else {
  3551. ret = xsltSaveResultToFilename((const char *) filename, res, style, 0);
  3552. }
  3553. if (ret < 0) {
  3554. xsltTransformError(ctxt, NULL, inst,
  3555. "xsltDocumentElem: unable to save to %s\n",
  3556. filename);
  3557. #ifdef WITH_XSLT_DEBUG_EXTRA
  3558. } else {
  3559. xsltGenericDebug(xsltGenericDebugContext,
  3560. "Wrote %d bytes to %s\n", ret, filename);
  3561. #endif
  3562. }
  3563. error:
  3564. ctxt->output = oldOutput;
  3565. ctxt->insert = oldInsert;
  3566. ctxt->type = oldType;
  3567. ctxt->outputFile = oldOutputFile;
  3568. if (URL != NULL)
  3569. xmlFree(URL);
  3570. if (filename != NULL)
  3571. xmlFree(filename);
  3572. if (style != NULL)
  3573. xsltFreeStylesheet(style);
  3574. if (res != NULL)
  3575. xmlFreeDoc(res);
  3576. }
  3577. /************************************************************************
  3578. * *
  3579. * Most of the XSLT-1.0 transformations *
  3580. * *
  3581. ************************************************************************/
  3582. /**
  3583. * xsltSort:
  3584. * @ctxt: a XSLT process context
  3585. * @node: the node in the source tree.
  3586. * @inst: the xslt sort node
  3587. * @comp: precomputed information
  3588. *
  3589. * function attached to xsl:sort nodes, but this should not be
  3590. * called directly
  3591. */
  3592. void
  3593. xsltSort(xsltTransformContextPtr ctxt,
  3594. xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr inst,
  3595. xsltElemPreCompPtr comp) {
  3596. if (comp == NULL) {
  3597. xsltTransformError(ctxt, NULL, inst,
  3598. "xsl:sort : compilation failed\n");
  3599. return;
  3600. }
  3601. xsltTransformError(ctxt, NULL, inst,
  3602. "xsl:sort : improper use this should not be reached\n");
  3603. }
  3604. /**
  3605. * xsltCopy:
  3606. * @ctxt: an XSLT process context
  3607. * @node: the node in the source tree
  3608. * @inst: the element node of the XSLT-copy instruction
  3609. * @castedComp: computed information of the XSLT-copy instruction
  3610. *
  3611. * Execute the XSLT-copy instruction on the source node.
  3612. */
  3613. void
  3614. xsltCopy(xsltTransformContextPtr ctxt, xmlNodePtr node,
  3615. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  3616. {
  3617. #ifdef XSLT_REFACTORED
  3618. xsltStyleItemCopyPtr comp = (xsltStyleItemCopyPtr) castedComp;
  3619. #else
  3620. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  3621. #endif
  3622. xmlNodePtr copy, oldInsert;
  3623. oldInsert = ctxt->insert;
  3624. if (ctxt->insert != NULL) {
  3625. switch (node->type) {
  3626. case XML_TEXT_NODE:
  3627. case XML_CDATA_SECTION_NODE:
  3628. /*
  3629. * This text comes from the stylesheet
  3630. * For stylesheets, the set of whitespace-preserving
  3631. * element names consists of just xsl:text.
  3632. */
  3633. #ifdef WITH_XSLT_DEBUG_PROCESS
  3634. if (node->type == XML_CDATA_SECTION_NODE) {
  3635. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3636. "xsltCopy: CDATA text %s\n", node->content));
  3637. } else {
  3638. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3639. "xsltCopy: text %s\n", node->content));
  3640. }
  3641. #endif
  3642. xsltCopyText(ctxt, ctxt->insert, node, 0);
  3643. break;
  3644. case XML_DOCUMENT_NODE:
  3645. case XML_HTML_DOCUMENT_NODE:
  3646. break;
  3647. case XML_ELEMENT_NODE:
  3648. /*
  3649. * REVISIT NOTE: The "fake" is a doc-node, not an element node.
  3650. * REMOVED:
  3651. * if (xmlStrEqual(node->name, BAD_CAST " fake node libxslt"))
  3652. * return;
  3653. */
  3654. #ifdef WITH_XSLT_DEBUG_PROCESS
  3655. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3656. "xsltCopy: node %s\n", node->name));
  3657. #endif
  3658. copy = xsltShallowCopyElem(ctxt, node, ctxt->insert, 0);
  3659. ctxt->insert = copy;
  3660. if (comp->use != NULL) {
  3661. xsltApplyAttributeSet(ctxt, node, inst, comp->use);
  3662. }
  3663. break;
  3664. case XML_ATTRIBUTE_NODE: {
  3665. #ifdef WITH_XSLT_DEBUG_PROCESS
  3666. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3667. "xsltCopy: attribute %s\n", node->name));
  3668. #endif
  3669. /*
  3670. * REVISIT: We could also raise an error if the parent is not
  3671. * an element node.
  3672. * OPTIMIZE TODO: Can we set the value/children of the
  3673. * attribute without an intermediate copy of the string value?
  3674. */
  3675. xsltShallowCopyAttr(ctxt, inst, ctxt->insert, (xmlAttrPtr) node);
  3676. break;
  3677. }
  3678. case XML_PI_NODE:
  3679. #ifdef WITH_XSLT_DEBUG_PROCESS
  3680. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3681. "xsltCopy: PI %s\n", node->name));
  3682. #endif
  3683. copy = xmlNewDocPI(ctxt->insert->doc, node->name,
  3684. node->content);
  3685. copy = xsltAddChild(ctxt->insert, copy);
  3686. break;
  3687. case XML_COMMENT_NODE:
  3688. #ifdef WITH_XSLT_DEBUG_PROCESS
  3689. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3690. "xsltCopy: comment\n"));
  3691. #endif
  3692. copy = xmlNewComment(node->content);
  3693. copy = xsltAddChild(ctxt->insert, copy);
  3694. break;
  3695. case XML_NAMESPACE_DECL:
  3696. #ifdef WITH_XSLT_DEBUG_PROCESS
  3697. XSLT_TRACE(ctxt,XSLT_TRACE_COPY,xsltGenericDebug(xsltGenericDebugContext,
  3698. "xsltCopy: namespace declaration\n"));
  3699. #endif
  3700. xsltShallowCopyNsNode(ctxt, inst, ctxt->insert, (xmlNsPtr)node);
  3701. break;
  3702. default:
  3703. break;
  3704. }
  3705. }
  3706. switch (node->type) {
  3707. case XML_DOCUMENT_NODE:
  3708. case XML_HTML_DOCUMENT_NODE:
  3709. case XML_ELEMENT_NODE:
  3710. xsltApplySequenceConstructor(ctxt, ctxt->node, inst->children,
  3711. NULL);
  3712. break;
  3713. default:
  3714. break;
  3715. }
  3716. ctxt->insert = oldInsert;
  3717. }
  3718. /**
  3719. * xsltText:
  3720. * @ctxt: a XSLT process context
  3721. * @node: the node in the source tree.
  3722. * @inst: the xslt text node
  3723. * @comp: precomputed information
  3724. *
  3725. * Process the xslt text node on the source node
  3726. */
  3727. void
  3728. xsltText(xsltTransformContextPtr ctxt, xmlNodePtr node ATTRIBUTE_UNUSED,
  3729. xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED) {
  3730. if ((inst->children != NULL) && (comp != NULL)) {
  3731. xmlNodePtr text = inst->children;
  3732. xmlNodePtr copy;
  3733. while (text != NULL) {
  3734. if ((text->type != XML_TEXT_NODE) &&
  3735. (text->type != XML_CDATA_SECTION_NODE)) {
  3736. xsltTransformError(ctxt, NULL, inst,
  3737. "xsl:text content problem\n");
  3738. break;
  3739. }
  3740. copy = xmlNewDocText(ctxt->output, text->content);
  3741. if (text->type != XML_CDATA_SECTION_NODE) {
  3742. #ifdef WITH_XSLT_DEBUG_PARSING
  3743. xsltGenericDebug(xsltGenericDebugContext,
  3744. "Disable escaping: %s\n", text->content);
  3745. #endif
  3746. copy->name = xmlStringTextNoenc;
  3747. }
  3748. copy = xsltAddChild(ctxt->insert, copy);
  3749. text = text->next;
  3750. }
  3751. }
  3752. }
  3753. /**
  3754. * xsltElement:
  3755. * @ctxt: a XSLT process context
  3756. * @node: the node in the source tree.
  3757. * @inst: the xslt element node
  3758. * @castedComp: precomputed information
  3759. *
  3760. * Process the xslt element node on the source node
  3761. */
  3762. void
  3763. xsltElement(xsltTransformContextPtr ctxt, xmlNodePtr node,
  3764. xmlNodePtr inst, xsltElemPreCompPtr castedComp) {
  3765. #ifdef XSLT_REFACTORED
  3766. xsltStyleItemElementPtr comp = (xsltStyleItemElementPtr) castedComp;
  3767. #else
  3768. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  3769. #endif
  3770. xmlChar *prop = NULL;
  3771. const xmlChar *name, *prefix = NULL, *nsName = NULL;
  3772. xmlNodePtr copy;
  3773. xmlNodePtr oldInsert;
  3774. if (ctxt->insert == NULL)
  3775. return;
  3776. /*
  3777. * A comp->has_name == 0 indicates that we need to skip this instruction,
  3778. * since it was evaluated to be invalid already during compilation.
  3779. */
  3780. if (!comp->has_name)
  3781. return;
  3782. /*
  3783. * stack and saves
  3784. */
  3785. oldInsert = ctxt->insert;
  3786. if (comp->name == NULL) {
  3787. /* TODO: fix attr acquisition wrt to the XSLT namespace */
  3788. prop = xsltEvalAttrValueTemplate(ctxt, inst,
  3789. (const xmlChar *) "name", XSLT_NAMESPACE);
  3790. if (prop == NULL) {
  3791. xsltTransformError(ctxt, NULL, inst,
  3792. "xsl:element: The attribute 'name' is missing.\n");
  3793. goto error;
  3794. }
  3795. if (xmlValidateQName(prop, 0)) {
  3796. xsltTransformError(ctxt, NULL, inst,
  3797. "xsl:element: The effective name '%s' is not a "
  3798. "valid QName.\n", prop);
  3799. /* we fall through to catch any further errors, if possible */
  3800. }
  3801. name = xsltSplitQName(ctxt->dict, prop, &prefix);
  3802. xmlFree(prop);
  3803. } else {
  3804. /*
  3805. * The "name" value was static.
  3806. */
  3807. #ifdef XSLT_REFACTORED
  3808. prefix = comp->nsPrefix;
  3809. name = comp->name;
  3810. #else
  3811. name = xsltSplitQName(ctxt->dict, comp->name, &prefix);
  3812. #endif
  3813. }
  3814. /*
  3815. * Create the new element
  3816. */
  3817. if (ctxt->output->dict == ctxt->dict) {
  3818. copy = xmlNewDocNodeEatName(ctxt->output, NULL, (xmlChar *)name, NULL);
  3819. } else {
  3820. copy = xmlNewDocNode(ctxt->output, NULL, (xmlChar *)name, NULL);
  3821. }
  3822. if (copy == NULL) {
  3823. xsltTransformError(ctxt, NULL, inst,
  3824. "xsl:element : creation of %s failed\n", name);
  3825. return;
  3826. }
  3827. copy = xsltAddChild(ctxt->insert, copy);
  3828. if (copy == NULL) {
  3829. xsltTransformError(ctxt, NULL, inst,
  3830. "xsl:element : xsltAddChild failed\n");
  3831. return;
  3832. }
  3833. /*
  3834. * Namespace
  3835. * ---------
  3836. */
  3837. if (comp->has_ns) {
  3838. if (comp->ns != NULL) {
  3839. /*
  3840. * No AVT; just plain text for the namespace name.
  3841. */
  3842. if (comp->ns[0] != 0)
  3843. nsName = comp->ns;
  3844. } else {
  3845. xmlChar *tmpNsName;
  3846. /*
  3847. * Eval the AVT.
  3848. */
  3849. /* TODO: check attr acquisition wrt to the XSLT namespace */
  3850. tmpNsName = xsltEvalAttrValueTemplate(ctxt, inst,
  3851. (const xmlChar *) "namespace", XSLT_NAMESPACE);
  3852. /*
  3853. * SPEC XSLT 1.0:
  3854. * "If the string is empty, then the expanded-name of the
  3855. * attribute has a null namespace URI."
  3856. */
  3857. if ((tmpNsName != NULL) && (tmpNsName[0] != 0))
  3858. nsName = xmlDictLookup(ctxt->dict, BAD_CAST tmpNsName, -1);
  3859. xmlFree(tmpNsName);
  3860. }
  3861. if (xmlStrEqual(nsName, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
  3862. xsltTransformError(ctxt, NULL, inst,
  3863. "xsl:attribute: Namespace http://www.w3.org/2000/xmlns/ "
  3864. "forbidden.\n");
  3865. goto error;
  3866. }
  3867. if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) {
  3868. prefix = BAD_CAST "xml";
  3869. } else if (xmlStrEqual(prefix, BAD_CAST "xml")) {
  3870. prefix = NULL;
  3871. }
  3872. } else {
  3873. xmlNsPtr ns;
  3874. /*
  3875. * SPEC XSLT 1.0:
  3876. * "If the namespace attribute is not present, then the QName is
  3877. * expanded into an expanded-name using the namespace declarations
  3878. * in effect for the xsl:element element, including any default
  3879. * namespace declaration.
  3880. */
  3881. ns = xmlSearchNs(inst->doc, inst, prefix);
  3882. if (ns == NULL) {
  3883. /*
  3884. * TODO: Check this in the compilation layer in case it's a
  3885. * static value.
  3886. */
  3887. if (prefix != NULL) {
  3888. xsltTransformError(ctxt, NULL, inst,
  3889. "xsl:element: The QName '%s:%s' has no "
  3890. "namespace binding in scope in the stylesheet; "
  3891. "this is an error, since the namespace was not "
  3892. "specified by the instruction itself.\n", prefix, name);
  3893. }
  3894. } else
  3895. nsName = ns->href;
  3896. }
  3897. /*
  3898. * Find/create a matching ns-decl in the result tree.
  3899. */
  3900. if (nsName != NULL) {
  3901. if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
  3902. /* Don't use a prefix of "xmlns" */
  3903. xmlChar *pref = xmlStrdup(BAD_CAST "ns_1");
  3904. copy->ns = xsltGetSpecialNamespace(ctxt, inst, nsName, pref, copy);
  3905. xmlFree(pref);
  3906. } else {
  3907. copy->ns = xsltGetSpecialNamespace(ctxt, inst, nsName, prefix,
  3908. copy);
  3909. }
  3910. } else if ((copy->parent != NULL) &&
  3911. (copy->parent->type == XML_ELEMENT_NODE) &&
  3912. (copy->parent->ns != NULL))
  3913. {
  3914. /*
  3915. * "Undeclare" the default namespace.
  3916. */
  3917. xsltGetSpecialNamespace(ctxt, inst, NULL, NULL, copy);
  3918. }
  3919. ctxt->insert = copy;
  3920. if (comp->has_use) {
  3921. if (comp->use != NULL) {
  3922. xsltApplyAttributeSet(ctxt, node, inst, comp->use);
  3923. } else {
  3924. xmlChar *attrSets = NULL;
  3925. /*
  3926. * BUG TODO: use-attribute-sets is not a value template.
  3927. * use-attribute-sets = qnames
  3928. */
  3929. attrSets = xsltEvalAttrValueTemplate(ctxt, inst,
  3930. (const xmlChar *)"use-attribute-sets", NULL);
  3931. if (attrSets != NULL) {
  3932. xsltApplyAttributeSet(ctxt, node, inst, attrSets);
  3933. xmlFree(attrSets);
  3934. }
  3935. }
  3936. }
  3937. /*
  3938. * Instantiate the sequence constructor.
  3939. */
  3940. if (inst->children != NULL)
  3941. xsltApplySequenceConstructor(ctxt, ctxt->node, inst->children,
  3942. NULL);
  3943. error:
  3944. ctxt->insert = oldInsert;
  3945. return;
  3946. }
  3947. /**
  3948. * xsltComment:
  3949. * @ctxt: a XSLT process context
  3950. * @node: the node in the source tree.
  3951. * @inst: the xslt comment node
  3952. * @comp: precomputed information
  3953. *
  3954. * Process the xslt comment node on the source node
  3955. */
  3956. void
  3957. xsltComment(xsltTransformContextPtr ctxt, xmlNodePtr node,
  3958. xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED) {
  3959. xmlChar *value = NULL;
  3960. xmlNodePtr commentNode;
  3961. int len;
  3962. value = xsltEvalTemplateString(ctxt, node, inst);
  3963. /* TODO: use or generate the compiled form */
  3964. len = xmlStrlen(value);
  3965. if (len > 0) {
  3966. if ((value[len-1] == '-') ||
  3967. (xmlStrstr(value, BAD_CAST "--"))) {
  3968. xsltTransformError(ctxt, NULL, inst,
  3969. "xsl:comment : '--' or ending '-' not allowed in comment\n");
  3970. /* fall through to try to catch further errors */
  3971. }
  3972. }
  3973. #ifdef WITH_XSLT_DEBUG_PROCESS
  3974. if (value == NULL) {
  3975. XSLT_TRACE(ctxt,XSLT_TRACE_COMMENT,xsltGenericDebug(xsltGenericDebugContext,
  3976. "xsltComment: empty\n"));
  3977. } else {
  3978. XSLT_TRACE(ctxt,XSLT_TRACE_COMMENT,xsltGenericDebug(xsltGenericDebugContext,
  3979. "xsltComment: content %s\n", value));
  3980. }
  3981. #endif
  3982. commentNode = xmlNewComment(value);
  3983. commentNode = xsltAddChild(ctxt->insert, commentNode);
  3984. if (value != NULL)
  3985. xmlFree(value);
  3986. }
  3987. /**
  3988. * xsltProcessingInstruction:
  3989. * @ctxt: a XSLT process context
  3990. * @node: the node in the source tree.
  3991. * @inst: the xslt processing-instruction node
  3992. * @castedComp: precomputed information
  3993. *
  3994. * Process the xslt processing-instruction node on the source node
  3995. */
  3996. void
  3997. xsltProcessingInstruction(xsltTransformContextPtr ctxt, xmlNodePtr node,
  3998. xmlNodePtr inst, xsltElemPreCompPtr castedComp) {
  3999. #ifdef XSLT_REFACTORED
  4000. xsltStyleItemPIPtr comp = (xsltStyleItemPIPtr) castedComp;
  4001. #else
  4002. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4003. #endif
  4004. const xmlChar *name;
  4005. xmlChar *value = NULL;
  4006. xmlNodePtr pi;
  4007. if (ctxt->insert == NULL)
  4008. return;
  4009. if (comp->has_name == 0)
  4010. return;
  4011. if (comp->name == NULL) {
  4012. name = xsltEvalAttrValueTemplate(ctxt, inst,
  4013. (const xmlChar *)"name", NULL);
  4014. if (name == NULL) {
  4015. xsltTransformError(ctxt, NULL, inst,
  4016. "xsl:processing-instruction : name is missing\n");
  4017. goto error;
  4018. }
  4019. } else {
  4020. name = comp->name;
  4021. }
  4022. /* TODO: check that it's both an an NCName and a PITarget. */
  4023. value = xsltEvalTemplateString(ctxt, node, inst);
  4024. if (xmlStrstr(value, BAD_CAST "?>") != NULL) {
  4025. xsltTransformError(ctxt, NULL, inst,
  4026. "xsl:processing-instruction: '?>' not allowed within PI content\n");
  4027. goto error;
  4028. }
  4029. #ifdef WITH_XSLT_DEBUG_PROCESS
  4030. if (value == NULL) {
  4031. XSLT_TRACE(ctxt,XSLT_TRACE_PI,xsltGenericDebug(xsltGenericDebugContext,
  4032. "xsltProcessingInstruction: %s empty\n", name));
  4033. } else {
  4034. XSLT_TRACE(ctxt,XSLT_TRACE_PI,xsltGenericDebug(xsltGenericDebugContext,
  4035. "xsltProcessingInstruction: %s content %s\n", name, value));
  4036. }
  4037. #endif
  4038. pi = xmlNewDocPI(ctxt->insert->doc, name, value);
  4039. pi = xsltAddChild(ctxt->insert, pi);
  4040. error:
  4041. if ((name != NULL) && (name != comp->name))
  4042. xmlFree((xmlChar *) name);
  4043. if (value != NULL)
  4044. xmlFree(value);
  4045. }
  4046. /**
  4047. * xsltCopyOf:
  4048. * @ctxt: an XSLT transformation context
  4049. * @node: the current node in the source tree
  4050. * @inst: the element node of the XSLT copy-of instruction
  4051. * @castedComp: precomputed information of the XSLT copy-of instruction
  4052. *
  4053. * Process the XSLT copy-of instruction.
  4054. */
  4055. void
  4056. xsltCopyOf(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4057. xmlNodePtr inst, xsltElemPreCompPtr castedComp) {
  4058. #ifdef XSLT_REFACTORED
  4059. xsltStyleItemCopyOfPtr comp = (xsltStyleItemCopyOfPtr) castedComp;
  4060. #else
  4061. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4062. #endif
  4063. xmlXPathObjectPtr res = NULL;
  4064. xmlNodeSetPtr list = NULL;
  4065. int i;
  4066. if ((ctxt == NULL) || (node == NULL) || (inst == NULL))
  4067. return;
  4068. if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) {
  4069. xsltTransformError(ctxt, NULL, inst,
  4070. "xsl:copy-of : compilation failed\n");
  4071. return;
  4072. }
  4073. /*
  4074. * SPEC XSLT 1.0:
  4075. * "The xsl:copy-of element can be used to insert a result tree
  4076. * fragment into the result tree, without first converting it to
  4077. * a string as xsl:value-of does (see [7.6.1 Generating Text with
  4078. * xsl:value-of]). The required select attribute contains an
  4079. * expression. When the result of evaluating the expression is a
  4080. * result tree fragment, the complete fragment is copied into the
  4081. * result tree. When the result is a node-set, all the nodes in the
  4082. * set are copied in document order into the result tree; copying
  4083. * an element node copies the attribute nodes, namespace nodes and
  4084. * children of the element node as well as the element node itself;
  4085. * a root node is copied by copying its children. When the result
  4086. * is neither a node-set nor a result tree fragment, the result is
  4087. * converted to a string and then inserted into the result tree,
  4088. * as with xsl:value-of.
  4089. */
  4090. #ifdef WITH_XSLT_DEBUG_PROCESS
  4091. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
  4092. "xsltCopyOf: select %s\n", comp->select));
  4093. #endif
  4094. /*
  4095. * Evaluate the "select" expression.
  4096. */
  4097. res = xsltPreCompEval(ctxt, node, comp);
  4098. if (res != NULL) {
  4099. if (res->type == XPATH_NODESET) {
  4100. /*
  4101. * Node-set
  4102. * --------
  4103. */
  4104. #ifdef WITH_XSLT_DEBUG_PROCESS
  4105. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
  4106. "xsltCopyOf: result is a node set\n"));
  4107. #endif
  4108. list = res->nodesetval;
  4109. if (list != NULL) {
  4110. xmlNodePtr cur;
  4111. /*
  4112. * The list is already sorted in document order by XPath.
  4113. * Append everything in this order under ctxt->insert.
  4114. */
  4115. for (i = 0;i < list->nodeNr;i++) {
  4116. cur = list->nodeTab[i];
  4117. if (cur == NULL)
  4118. continue;
  4119. if ((cur->type == XML_DOCUMENT_NODE) ||
  4120. (cur->type == XML_HTML_DOCUMENT_NODE))
  4121. {
  4122. xsltCopyTreeList(ctxt, inst,
  4123. cur->children, ctxt->insert, 0, 0);
  4124. } else if (cur->type == XML_ATTRIBUTE_NODE) {
  4125. xsltShallowCopyAttr(ctxt, inst,
  4126. ctxt->insert, (xmlAttrPtr) cur);
  4127. } else {
  4128. xsltCopyTree(ctxt, inst, cur, ctxt->insert, 0, 0);
  4129. }
  4130. }
  4131. }
  4132. } else if (res->type == XPATH_XSLT_TREE) {
  4133. /*
  4134. * Result tree fragment
  4135. * --------------------
  4136. * E.g. via <xsl:variable ...><foo/></xsl:variable>
  4137. * Note that the root node of such trees is an xmlDocPtr in Libxslt.
  4138. */
  4139. #ifdef WITH_XSLT_DEBUG_PROCESS
  4140. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
  4141. "xsltCopyOf: result is a result tree fragment\n"));
  4142. #endif
  4143. list = res->nodesetval;
  4144. if ((list != NULL) && (list->nodeTab != NULL) &&
  4145. (list->nodeTab[0] != NULL) &&
  4146. (IS_XSLT_REAL_NODE(list->nodeTab[0])))
  4147. {
  4148. xsltCopyTreeList(ctxt, inst,
  4149. list->nodeTab[0]->children, ctxt->insert, 0, 0);
  4150. }
  4151. } else {
  4152. xmlChar *value = NULL;
  4153. /*
  4154. * Convert to a string.
  4155. */
  4156. value = xmlXPathCastToString(res);
  4157. if (value == NULL) {
  4158. xsltTransformError(ctxt, NULL, inst,
  4159. "Internal error in xsltCopyOf(): "
  4160. "failed to cast an XPath object to string.\n");
  4161. ctxt->state = XSLT_STATE_STOPPED;
  4162. } else {
  4163. if (value[0] != 0) {
  4164. /*
  4165. * Append content as text node.
  4166. */
  4167. xsltCopyTextString(ctxt, ctxt->insert, value, 0);
  4168. }
  4169. xmlFree(value);
  4170. #ifdef WITH_XSLT_DEBUG_PROCESS
  4171. XSLT_TRACE(ctxt,XSLT_TRACE_COPY_OF,xsltGenericDebug(xsltGenericDebugContext,
  4172. "xsltCopyOf: result %s\n", res->stringval));
  4173. #endif
  4174. }
  4175. }
  4176. } else {
  4177. ctxt->state = XSLT_STATE_STOPPED;
  4178. }
  4179. if (res != NULL)
  4180. xmlXPathFreeObject(res);
  4181. }
  4182. /**
  4183. * xsltValueOf:
  4184. * @ctxt: a XSLT process context
  4185. * @node: the node in the source tree.
  4186. * @inst: the xslt value-of node
  4187. * @castedComp: precomputed information
  4188. *
  4189. * Process the xslt value-of node on the source node
  4190. */
  4191. void
  4192. xsltValueOf(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4193. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4194. {
  4195. #ifdef XSLT_REFACTORED
  4196. xsltStyleItemValueOfPtr comp = (xsltStyleItemValueOfPtr) castedComp;
  4197. #else
  4198. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4199. #endif
  4200. xmlXPathObjectPtr res = NULL;
  4201. xmlChar *value = NULL;
  4202. if ((ctxt == NULL) || (node == NULL) || (inst == NULL))
  4203. return;
  4204. if ((comp == NULL) || (comp->select == NULL) || (comp->comp == NULL)) {
  4205. xsltTransformError(ctxt, NULL, inst,
  4206. "Internal error in xsltValueOf(): "
  4207. "The XSLT 'value-of' instruction was not compiled.\n");
  4208. return;
  4209. }
  4210. #ifdef WITH_XSLT_DEBUG_PROCESS
  4211. XSLT_TRACE(ctxt,XSLT_TRACE_VALUE_OF,xsltGenericDebug(xsltGenericDebugContext,
  4212. "xsltValueOf: select %s\n", comp->select));
  4213. #endif
  4214. res = xsltPreCompEval(ctxt, node, comp);
  4215. /*
  4216. * Cast the XPath object to string.
  4217. */
  4218. if (res != NULL) {
  4219. value = xmlXPathCastToString(res);
  4220. if (value == NULL) {
  4221. xsltTransformError(ctxt, NULL, inst,
  4222. "Internal error in xsltValueOf(): "
  4223. "failed to cast an XPath object to string.\n");
  4224. ctxt->state = XSLT_STATE_STOPPED;
  4225. goto error;
  4226. }
  4227. if (value[0] != 0) {
  4228. xsltCopyTextString(ctxt, ctxt->insert, value, comp->noescape);
  4229. }
  4230. } else {
  4231. xsltTransformError(ctxt, NULL, inst,
  4232. "XPath evaluation returned no result.\n");
  4233. ctxt->state = XSLT_STATE_STOPPED;
  4234. goto error;
  4235. }
  4236. #ifdef WITH_XSLT_DEBUG_PROCESS
  4237. if (value) {
  4238. XSLT_TRACE(ctxt,XSLT_TRACE_VALUE_OF,xsltGenericDebug(xsltGenericDebugContext,
  4239. "xsltValueOf: result '%s'\n", value));
  4240. }
  4241. #endif
  4242. error:
  4243. if (value != NULL)
  4244. xmlFree(value);
  4245. if (res != NULL)
  4246. xmlXPathFreeObject(res);
  4247. }
  4248. /**
  4249. * xsltNumber:
  4250. * @ctxt: a XSLT process context
  4251. * @node: the node in the source tree.
  4252. * @inst: the xslt number node
  4253. * @castedComp: precomputed information
  4254. *
  4255. * Process the xslt number node on the source node
  4256. */
  4257. void
  4258. xsltNumber(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4259. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4260. {
  4261. #ifdef XSLT_REFACTORED
  4262. xsltStyleItemNumberPtr comp = (xsltStyleItemNumberPtr) castedComp;
  4263. #else
  4264. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4265. #endif
  4266. xmlXPathContextPtr xpctxt;
  4267. xmlNsPtr *oldXPNamespaces;
  4268. int oldXPNsNr;
  4269. if (comp == NULL) {
  4270. xsltTransformError(ctxt, NULL, inst,
  4271. "xsl:number : compilation failed\n");
  4272. return;
  4273. }
  4274. if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
  4275. return;
  4276. comp->numdata.doc = inst->doc;
  4277. comp->numdata.node = inst;
  4278. xpctxt = ctxt->xpathCtxt;
  4279. oldXPNsNr = xpctxt->nsNr;
  4280. oldXPNamespaces = xpctxt->namespaces;
  4281. #ifdef XSLT_REFACTORED
  4282. if (comp->inScopeNs != NULL) {
  4283. xpctxt->namespaces = comp->inScopeNs->list;
  4284. xpctxt->nsNr = comp->inScopeNs->xpathNumber;
  4285. } else {
  4286. xpctxt->namespaces = NULL;
  4287. xpctxt->nsNr = 0;
  4288. }
  4289. #else
  4290. xpctxt->namespaces = comp->nsList;
  4291. xpctxt->nsNr = comp->nsNr;
  4292. #endif
  4293. xsltNumberFormat(ctxt, &comp->numdata, node);
  4294. xpctxt->nsNr = oldXPNsNr;
  4295. xpctxt->namespaces = oldXPNamespaces;
  4296. }
  4297. /**
  4298. * xsltApplyImports:
  4299. * @ctxt: an XSLT transformation context
  4300. * @contextNode: the current node in the source tree.
  4301. * @inst: the element node of the XSLT 'apply-imports' instruction
  4302. * @comp: the compiled instruction
  4303. *
  4304. * Process the XSLT apply-imports element.
  4305. */
  4306. void
  4307. xsltApplyImports(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  4308. xmlNodePtr inst,
  4309. xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
  4310. {
  4311. xsltTemplatePtr templ;
  4312. if ((ctxt == NULL) || (inst == NULL))
  4313. return;
  4314. if (comp == NULL) {
  4315. xsltTransformError(ctxt, NULL, inst,
  4316. "Internal error in xsltApplyImports(): "
  4317. "The XSLT 'apply-imports' instruction was not compiled.\n");
  4318. return;
  4319. }
  4320. /*
  4321. * NOTE that ctxt->currentTemplateRule and ctxt->templ is not the
  4322. * same; the former is the "Current Template Rule" as defined by the
  4323. * XSLT spec, the latter is simply the template struct being
  4324. * currently processed.
  4325. */
  4326. if (ctxt->currentTemplateRule == NULL) {
  4327. /*
  4328. * SPEC XSLT 2.0:
  4329. * "[ERR XTDE0560] It is a non-recoverable dynamic error if
  4330. * xsl:apply-imports or xsl:next-match is evaluated when the
  4331. * current template rule is null."
  4332. */
  4333. xsltTransformError(ctxt, NULL, inst,
  4334. "It is an error to call 'apply-imports' "
  4335. "when there's no current template rule.\n");
  4336. return;
  4337. }
  4338. /*
  4339. * TODO: Check if this is correct.
  4340. */
  4341. templ = xsltGetTemplate(ctxt, contextNode,
  4342. ctxt->currentTemplateRule->style);
  4343. if (templ != NULL) {
  4344. xsltTemplatePtr oldCurTemplRule = ctxt->currentTemplateRule;
  4345. /*
  4346. * Set the current template rule.
  4347. */
  4348. ctxt->currentTemplateRule = templ;
  4349. /*
  4350. * URGENT TODO: Need xsl:with-param be handled somehow here?
  4351. */
  4352. xsltApplyXSLTTemplate(ctxt, contextNode, templ->content,
  4353. templ, NULL);
  4354. ctxt->currentTemplateRule = oldCurTemplRule;
  4355. }
  4356. else {
  4357. /* Use built-in templates. */
  4358. xsltDefaultProcessOneNode(ctxt, contextNode, NULL);
  4359. }
  4360. }
  4361. /**
  4362. * xsltCallTemplate:
  4363. * @ctxt: a XSLT transformation context
  4364. * @node: the "current node" in the source tree
  4365. * @inst: the XSLT 'call-template' instruction
  4366. * @castedComp: the compiled information of the instruction
  4367. *
  4368. * Processes the XSLT call-template instruction on the source node.
  4369. */
  4370. void
  4371. xsltCallTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4372. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4373. {
  4374. #ifdef XSLT_REFACTORED
  4375. xsltStyleItemCallTemplatePtr comp =
  4376. (xsltStyleItemCallTemplatePtr) castedComp;
  4377. #else
  4378. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4379. #endif
  4380. xsltStackElemPtr withParams = NULL;
  4381. if (ctxt->insert == NULL)
  4382. return;
  4383. if (comp == NULL) {
  4384. xsltTransformError(ctxt, NULL, inst,
  4385. "The XSLT 'call-template' instruction was not compiled.\n");
  4386. return;
  4387. }
  4388. /*
  4389. * The template must have been precomputed
  4390. */
  4391. if (comp->templ == NULL) {
  4392. comp->templ = xsltFindTemplate(ctxt, comp->name, comp->ns);
  4393. if (comp->templ == NULL) {
  4394. if (comp->ns != NULL) {
  4395. xsltTransformError(ctxt, NULL, inst,
  4396. "The called template '{%s}%s' was not found.\n",
  4397. comp->ns, comp->name);
  4398. } else {
  4399. xsltTransformError(ctxt, NULL, inst,
  4400. "The called template '%s' was not found.\n",
  4401. comp->name);
  4402. }
  4403. return;
  4404. }
  4405. }
  4406. #ifdef WITH_XSLT_DEBUG_PROCESS
  4407. if ((comp != NULL) && (comp->name != NULL))
  4408. XSLT_TRACE(ctxt,XSLT_TRACE_CALL_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  4409. "call-template: name %s\n", comp->name));
  4410. #endif
  4411. if (inst->children) {
  4412. xmlNodePtr cur;
  4413. xsltStackElemPtr param;
  4414. cur = inst->children;
  4415. while (cur != NULL) {
  4416. #ifdef WITH_DEBUGGER
  4417. if (ctxt->debugStatus != XSLT_DEBUG_NONE)
  4418. xslHandleDebugger(cur, node, comp->templ, ctxt);
  4419. #endif
  4420. if (ctxt->state == XSLT_STATE_STOPPED) break;
  4421. /*
  4422. * TODO: The "with-param"s could be part of the "call-template"
  4423. * structure. Avoid to "search" for params dynamically
  4424. * in the XML tree every time.
  4425. */
  4426. if (IS_XSLT_ELEM(cur)) {
  4427. if (IS_XSLT_NAME(cur, "with-param")) {
  4428. param = xsltParseStylesheetCallerParam(ctxt, cur);
  4429. if (param != NULL) {
  4430. param->next = withParams;
  4431. withParams = param;
  4432. }
  4433. } else {
  4434. xsltGenericError(xsltGenericErrorContext,
  4435. "xsl:call-template: misplaced xsl:%s\n", cur->name);
  4436. }
  4437. } else {
  4438. xsltGenericError(xsltGenericErrorContext,
  4439. "xsl:call-template: misplaced %s element\n", cur->name);
  4440. }
  4441. cur = cur->next;
  4442. }
  4443. }
  4444. /*
  4445. * Create a new frame using the params first
  4446. */
  4447. xsltApplyXSLTTemplate(ctxt, node, comp->templ->content, comp->templ,
  4448. withParams);
  4449. if (withParams != NULL)
  4450. xsltFreeStackElemList(withParams);
  4451. #ifdef WITH_XSLT_DEBUG_PROCESS
  4452. if ((comp != NULL) && (comp->name != NULL))
  4453. XSLT_TRACE(ctxt,XSLT_TRACE_CALL_TEMPLATE,xsltGenericDebug(xsltGenericDebugContext,
  4454. "call-template returned: name %s\n", comp->name));
  4455. #endif
  4456. }
  4457. /**
  4458. * xsltApplyTemplates:
  4459. * @ctxt: a XSLT transformation context
  4460. * @node: the 'current node' in the source tree
  4461. * @inst: the element node of an XSLT 'apply-templates' instruction
  4462. * @castedComp: the compiled instruction
  4463. *
  4464. * Processes the XSLT 'apply-templates' instruction on the current node.
  4465. */
  4466. void
  4467. xsltApplyTemplates(xsltTransformContextPtr ctxt, xmlNodePtr node,
  4468. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4469. {
  4470. #ifdef XSLT_REFACTORED
  4471. xsltStyleItemApplyTemplatesPtr comp =
  4472. (xsltStyleItemApplyTemplatesPtr) castedComp;
  4473. #else
  4474. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4475. #endif
  4476. int i;
  4477. xmlNodePtr cur, oldContextNode;
  4478. xmlNodeSetPtr list = NULL, oldList;
  4479. xsltStackElemPtr withParams = NULL;
  4480. int oldXPProximityPosition, oldXPContextSize;
  4481. const xmlChar *oldMode, *oldModeURI;
  4482. xmlDocPtr oldXPDoc;
  4483. xsltDocumentPtr oldDocInfo;
  4484. xmlXPathContextPtr xpctxt;
  4485. if (comp == NULL) {
  4486. xsltTransformError(ctxt, NULL, inst,
  4487. "xsl:apply-templates : compilation failed\n");
  4488. return;
  4489. }
  4490. if ((ctxt == NULL) || (node == NULL) || (inst == NULL) || (comp == NULL))
  4491. return;
  4492. #ifdef WITH_XSLT_DEBUG_PROCESS
  4493. if ((node != NULL) && (node->name != NULL))
  4494. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4495. "xsltApplyTemplates: node: '%s'\n", node->name));
  4496. #endif
  4497. xpctxt = ctxt->xpathCtxt;
  4498. /*
  4499. * Save context states.
  4500. */
  4501. oldContextNode = ctxt->node;
  4502. oldMode = ctxt->mode;
  4503. oldModeURI = ctxt->modeURI;
  4504. oldDocInfo = ctxt->document;
  4505. oldList = ctxt->nodeList;
  4506. /*
  4507. * The xpath context size and proximity position, as
  4508. * well as the xpath and context documents, may be changed
  4509. * so we save their initial state and will restore on exit
  4510. */
  4511. oldXPContextSize = xpctxt->contextSize;
  4512. oldXPProximityPosition = xpctxt->proximityPosition;
  4513. oldXPDoc = xpctxt->doc;
  4514. /*
  4515. * Set up contexts.
  4516. */
  4517. ctxt->mode = comp->mode;
  4518. ctxt->modeURI = comp->modeURI;
  4519. if (comp->select != NULL) {
  4520. xmlXPathObjectPtr res = NULL;
  4521. if (comp->comp == NULL) {
  4522. xsltTransformError(ctxt, NULL, inst,
  4523. "xsl:apply-templates : compilation failed\n");
  4524. goto error;
  4525. }
  4526. #ifdef WITH_XSLT_DEBUG_PROCESS
  4527. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4528. "xsltApplyTemplates: select %s\n", comp->select));
  4529. #endif
  4530. res = xsltPreCompEval(ctxt, node, comp);
  4531. if (res != NULL) {
  4532. if (res->type == XPATH_NODESET) {
  4533. list = res->nodesetval; /* consume the node set */
  4534. res->nodesetval = NULL;
  4535. } else {
  4536. xsltTransformError(ctxt, NULL, inst,
  4537. "The 'select' expression did not evaluate to a "
  4538. "node set.\n");
  4539. ctxt->state = XSLT_STATE_STOPPED;
  4540. xmlXPathFreeObject(res);
  4541. goto error;
  4542. }
  4543. xmlXPathFreeObject(res);
  4544. /*
  4545. * Note: An xsl:apply-templates with a 'select' attribute,
  4546. * can change the current source doc.
  4547. */
  4548. } else {
  4549. xsltTransformError(ctxt, NULL, inst,
  4550. "Failed to evaluate the 'select' expression.\n");
  4551. ctxt->state = XSLT_STATE_STOPPED;
  4552. goto error;
  4553. }
  4554. if (list == NULL) {
  4555. #ifdef WITH_XSLT_DEBUG_PROCESS
  4556. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4557. "xsltApplyTemplates: select didn't evaluate to a node list\n"));
  4558. #endif
  4559. goto exit;
  4560. }
  4561. /*
  4562. *
  4563. * NOTE: Previously a document info (xsltDocument) was
  4564. * created and attached to the Result Tree Fragment.
  4565. * But such a document info is created on demand in
  4566. * xsltKeyFunction() (functions.c), so we need to create
  4567. * it here beforehand.
  4568. * In order to take care of potential keys we need to
  4569. * do some extra work for the case when a Result Tree Fragment
  4570. * is converted into a nodeset (e.g. exslt:node-set()) :
  4571. * We attach a "pseudo-doc" (xsltDocument) to _private.
  4572. * This xsltDocument, together with the keyset, will be freed
  4573. * when the Result Tree Fragment is freed.
  4574. *
  4575. */
  4576. #if 0
  4577. if ((ctxt->nbKeys > 0) &&
  4578. (list->nodeNr != 0) &&
  4579. (list->nodeTab[0]->doc != NULL) &&
  4580. XSLT_IS_RES_TREE_FRAG(list->nodeTab[0]->doc))
  4581. {
  4582. /*
  4583. * NOTE that it's also OK if @effectiveDocInfo will be
  4584. * set to NULL.
  4585. */
  4586. isRTF = 1;
  4587. effectiveDocInfo = list->nodeTab[0]->doc->_private;
  4588. }
  4589. #endif
  4590. } else {
  4591. /*
  4592. * Build an XPath node set with the children
  4593. */
  4594. list = xmlXPathNodeSetCreate(NULL);
  4595. if (list == NULL)
  4596. goto error;
  4597. if (node->type != XML_NAMESPACE_DECL)
  4598. cur = node->children;
  4599. else
  4600. cur = NULL;
  4601. while (cur != NULL) {
  4602. if (IS_XSLT_REAL_NODE(cur))
  4603. xmlXPathNodeSetAddUnique(list, cur);
  4604. cur = cur->next;
  4605. }
  4606. }
  4607. #ifdef WITH_XSLT_DEBUG_PROCESS
  4608. if (list != NULL)
  4609. XSLT_TRACE(ctxt,XSLT_TRACE_APPLY_TEMPLATES,xsltGenericDebug(xsltGenericDebugContext,
  4610. "xsltApplyTemplates: list of %d nodes\n", list->nodeNr));
  4611. #endif
  4612. if ((list == NULL) || (list->nodeNr == 0))
  4613. goto exit;
  4614. /*
  4615. * Set the context's node set and size; this is also needed for
  4616. * for xsltDoSortFunction().
  4617. */
  4618. ctxt->nodeList = list;
  4619. /*
  4620. * Process xsl:with-param and xsl:sort instructions.
  4621. * (The code became so verbose just to avoid the
  4622. * xmlNodePtr sorts[XSLT_MAX_SORT] if there's no xsl:sort)
  4623. * BUG TODO: We are not using namespaced potentially defined on the
  4624. * xsl:sort or xsl:with-param elements; XPath expression might fail.
  4625. */
  4626. if (inst->children) {
  4627. xsltStackElemPtr param;
  4628. cur = inst->children;
  4629. while (cur) {
  4630. #ifdef WITH_DEBUGGER
  4631. if (ctxt->debugStatus != XSLT_DEBUG_NONE)
  4632. xslHandleDebugger(cur, node, NULL, ctxt);
  4633. #endif
  4634. if (ctxt->state == XSLT_STATE_STOPPED)
  4635. break;
  4636. if (cur->type == XML_TEXT_NODE) {
  4637. cur = cur->next;
  4638. continue;
  4639. }
  4640. if (! IS_XSLT_ELEM(cur))
  4641. break;
  4642. if (IS_XSLT_NAME(cur, "with-param")) {
  4643. param = xsltParseStylesheetCallerParam(ctxt, cur);
  4644. if (param != NULL) {
  4645. param->next = withParams;
  4646. withParams = param;
  4647. }
  4648. }
  4649. if (IS_XSLT_NAME(cur, "sort")) {
  4650. xsltTemplatePtr oldCurTempRule =
  4651. ctxt->currentTemplateRule;
  4652. int nbsorts = 0;
  4653. xmlNodePtr sorts[XSLT_MAX_SORT];
  4654. sorts[nbsorts++] = cur;
  4655. cur = cur->next;
  4656. while (cur) {
  4657. #ifdef WITH_DEBUGGER
  4658. if (ctxt->debugStatus != XSLT_DEBUG_NONE)
  4659. xslHandleDebugger(cur, node, NULL, ctxt);
  4660. #endif
  4661. if (ctxt->state == XSLT_STATE_STOPPED)
  4662. break;
  4663. if (cur->type == XML_TEXT_NODE) {
  4664. cur = cur->next;
  4665. continue;
  4666. }
  4667. if (! IS_XSLT_ELEM(cur))
  4668. break;
  4669. if (IS_XSLT_NAME(cur, "with-param")) {
  4670. param = xsltParseStylesheetCallerParam(ctxt, cur);
  4671. if (param != NULL) {
  4672. param->next = withParams;
  4673. withParams = param;
  4674. }
  4675. }
  4676. if (IS_XSLT_NAME(cur, "sort")) {
  4677. if (nbsorts >= XSLT_MAX_SORT) {
  4678. xsltTransformError(ctxt, NULL, cur,
  4679. "The number (%d) of xsl:sort instructions exceeds the "
  4680. "maximum allowed by this processor's settings.\n",
  4681. nbsorts);
  4682. ctxt->state = XSLT_STATE_STOPPED;
  4683. break;
  4684. } else {
  4685. sorts[nbsorts++] = cur;
  4686. }
  4687. }
  4688. cur = cur->next;
  4689. }
  4690. /*
  4691. * The "current template rule" is cleared for xsl:sort.
  4692. */
  4693. ctxt->currentTemplateRule = NULL;
  4694. /*
  4695. * Sort.
  4696. */
  4697. xsltDoSortFunction(ctxt, sorts, nbsorts);
  4698. ctxt->currentTemplateRule = oldCurTempRule;
  4699. break;
  4700. }
  4701. cur = cur->next;
  4702. }
  4703. }
  4704. xpctxt->contextSize = list->nodeNr;
  4705. /*
  4706. * Apply templates for all selected source nodes.
  4707. */
  4708. for (i = 0; i < list->nodeNr; i++) {
  4709. cur = list->nodeTab[i];
  4710. /*
  4711. * The node becomes the "current node".
  4712. */
  4713. ctxt->node = cur;
  4714. /*
  4715. * An xsl:apply-templates can change the current context doc.
  4716. * OPTIMIZE TODO: Get rid of the need to set the context doc.
  4717. */
  4718. if ((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL))
  4719. xpctxt->doc = cur->doc;
  4720. xpctxt->proximityPosition = i + 1;
  4721. /*
  4722. * Find and apply a template for this node.
  4723. */
  4724. xsltProcessOneNode(ctxt, cur, withParams);
  4725. }
  4726. exit:
  4727. error:
  4728. /*
  4729. * Free the parameter list.
  4730. */
  4731. if (withParams != NULL)
  4732. xsltFreeStackElemList(withParams);
  4733. if (list != NULL)
  4734. xmlXPathFreeNodeSet(list);
  4735. /*
  4736. * Restore context states.
  4737. */
  4738. xpctxt->doc = oldXPDoc;
  4739. xpctxt->contextSize = oldXPContextSize;
  4740. xpctxt->proximityPosition = oldXPProximityPosition;
  4741. ctxt->document = oldDocInfo;
  4742. ctxt->nodeList = oldList;
  4743. ctxt->node = oldContextNode;
  4744. ctxt->mode = oldMode;
  4745. ctxt->modeURI = oldModeURI;
  4746. }
  4747. /**
  4748. * xsltChoose:
  4749. * @ctxt: a XSLT process context
  4750. * @contextNode: the current node in the source tree
  4751. * @inst: the xsl:choose instruction
  4752. * @comp: compiled information of the instruction
  4753. *
  4754. * Processes the xsl:choose instruction on the source node.
  4755. */
  4756. void
  4757. xsltChoose(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  4758. xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
  4759. {
  4760. xmlNodePtr cur;
  4761. if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL))
  4762. return;
  4763. /*
  4764. * TODO: Content model checks should be done only at compilation
  4765. * time.
  4766. */
  4767. cur = inst->children;
  4768. if (cur == NULL) {
  4769. xsltTransformError(ctxt, NULL, inst,
  4770. "xsl:choose: The instruction has no content.\n");
  4771. return;
  4772. }
  4773. #ifdef XSLT_REFACTORED
  4774. /*
  4775. * We don't check the content model during transformation.
  4776. */
  4777. #else
  4778. if ((! IS_XSLT_ELEM(cur)) || (! IS_XSLT_NAME(cur, "when"))) {
  4779. xsltTransformError(ctxt, NULL, inst,
  4780. "xsl:choose: xsl:when expected first\n");
  4781. return;
  4782. }
  4783. #endif
  4784. {
  4785. int testRes = 0, res = 0;
  4786. #ifdef XSLT_REFACTORED
  4787. xsltStyleItemWhenPtr wcomp = NULL;
  4788. #else
  4789. xsltStylePreCompPtr wcomp = NULL;
  4790. #endif
  4791. /*
  4792. * Process xsl:when ---------------------------------------------------
  4793. */
  4794. while (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "when")) {
  4795. wcomp = cur->psvi;
  4796. if ((wcomp == NULL) || (wcomp->test == NULL) ||
  4797. (wcomp->comp == NULL))
  4798. {
  4799. xsltTransformError(ctxt, NULL, cur,
  4800. "Internal error in xsltChoose(): "
  4801. "The XSLT 'when' instruction was not compiled.\n");
  4802. goto error;
  4803. }
  4804. #ifdef WITH_DEBUGGER
  4805. if (xslDebugStatus != XSLT_DEBUG_NONE) {
  4806. /*
  4807. * TODO: Isn't comp->templ always NULL for xsl:choose?
  4808. */
  4809. xslHandleDebugger(cur, contextNode, NULL, ctxt);
  4810. }
  4811. #endif
  4812. #ifdef WITH_XSLT_DEBUG_PROCESS
  4813. XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
  4814. "xsltChoose: test %s\n", wcomp->test));
  4815. #endif
  4816. #ifdef XSLT_FAST_IF
  4817. res = xsltPreCompEvalToBoolean(ctxt, contextNode, wcomp);
  4818. if (res == -1) {
  4819. ctxt->state = XSLT_STATE_STOPPED;
  4820. goto error;
  4821. }
  4822. testRes = (res == 1) ? 1 : 0;
  4823. #else /* XSLT_FAST_IF */
  4824. res = xsltPreCompEval(ctxt, cotextNode, wcomp);
  4825. if (res != NULL) {
  4826. if (res->type != XPATH_BOOLEAN)
  4827. res = xmlXPathConvertBoolean(res);
  4828. if (res->type == XPATH_BOOLEAN)
  4829. testRes = res->boolval;
  4830. else {
  4831. #ifdef WITH_XSLT_DEBUG_PROCESS
  4832. XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
  4833. "xsltChoose: test didn't evaluate to a boolean\n"));
  4834. #endif
  4835. goto error;
  4836. }
  4837. xmlXPathFreeObject(res);
  4838. res = NULL;
  4839. } else {
  4840. ctxt->state = XSLT_STATE_STOPPED;
  4841. goto error;
  4842. }
  4843. #endif /* else of XSLT_FAST_IF */
  4844. #ifdef WITH_XSLT_DEBUG_PROCESS
  4845. XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
  4846. "xsltChoose: test evaluate to %d\n", testRes));
  4847. #endif
  4848. if (testRes)
  4849. goto test_is_true;
  4850. cur = cur->next;
  4851. }
  4852. /*
  4853. * Process xsl:otherwise ----------------------------------------------
  4854. */
  4855. if (IS_XSLT_ELEM(cur) && IS_XSLT_NAME(cur, "otherwise")) {
  4856. #ifdef WITH_DEBUGGER
  4857. if (xslDebugStatus != XSLT_DEBUG_NONE)
  4858. xslHandleDebugger(cur, contextNode, NULL, ctxt);
  4859. #endif
  4860. #ifdef WITH_XSLT_DEBUG_PROCESS
  4861. XSLT_TRACE(ctxt,XSLT_TRACE_CHOOSE,xsltGenericDebug(xsltGenericDebugContext,
  4862. "evaluating xsl:otherwise\n"));
  4863. #endif
  4864. goto test_is_true;
  4865. }
  4866. goto exit;
  4867. test_is_true:
  4868. goto process_sequence;
  4869. }
  4870. process_sequence:
  4871. /*
  4872. * Instantiate the sequence constructor.
  4873. */
  4874. xsltApplySequenceConstructor(ctxt, ctxt->node, cur->children,
  4875. NULL);
  4876. exit:
  4877. error:
  4878. return;
  4879. }
  4880. /**
  4881. * xsltIf:
  4882. * @ctxt: a XSLT process context
  4883. * @contextNode: the current node in the source tree
  4884. * @inst: the xsl:if instruction
  4885. * @castedComp: compiled information of the instruction
  4886. *
  4887. * Processes the xsl:if instruction on the source node.
  4888. */
  4889. void
  4890. xsltIf(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  4891. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4892. {
  4893. int res = 0;
  4894. #ifdef XSLT_REFACTORED
  4895. xsltStyleItemIfPtr comp = (xsltStyleItemIfPtr) castedComp;
  4896. #else
  4897. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4898. #endif
  4899. if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL))
  4900. return;
  4901. if ((comp == NULL) || (comp->test == NULL) || (comp->comp == NULL)) {
  4902. xsltTransformError(ctxt, NULL, inst,
  4903. "Internal error in xsltIf(): "
  4904. "The XSLT 'if' instruction was not compiled.\n");
  4905. return;
  4906. }
  4907. #ifdef WITH_XSLT_DEBUG_PROCESS
  4908. XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
  4909. "xsltIf: test %s\n", comp->test));
  4910. #endif
  4911. #ifdef XSLT_FAST_IF
  4912. {
  4913. xmlDocPtr oldLocalFragmentTop = ctxt->localRVT;
  4914. res = xsltPreCompEvalToBoolean(ctxt, contextNode, comp);
  4915. /*
  4916. * Cleanup fragments created during evaluation of the
  4917. * "select" expression.
  4918. */
  4919. if (oldLocalFragmentTop != ctxt->localRVT)
  4920. xsltReleaseLocalRVTs(ctxt, oldLocalFragmentTop);
  4921. }
  4922. #ifdef WITH_XSLT_DEBUG_PROCESS
  4923. XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
  4924. "xsltIf: test evaluate to %d\n", res));
  4925. #endif
  4926. if (res == -1) {
  4927. ctxt->state = XSLT_STATE_STOPPED;
  4928. goto error;
  4929. }
  4930. if (res == 1) {
  4931. /*
  4932. * Instantiate the sequence constructor of xsl:if.
  4933. */
  4934. xsltApplySequenceConstructor(ctxt,
  4935. contextNode, inst->children, NULL);
  4936. }
  4937. #else /* XSLT_FAST_IF */
  4938. {
  4939. /*
  4940. * OLD CODE:
  4941. */
  4942. xmlXPathObjectPtr xpobj = xsltPreCompEval(ctxt, contextNode, comp);
  4943. if (xpobj != NULL) {
  4944. if (xpobj->type != XPATH_BOOLEAN)
  4945. xpobj = xmlXPathConvertBoolean(xpobj);
  4946. if (xpobj->type == XPATH_BOOLEAN) {
  4947. res = xpobj->boolval;
  4948. #ifdef WITH_XSLT_DEBUG_PROCESS
  4949. XSLT_TRACE(ctxt,XSLT_TRACE_IF,xsltGenericDebug(xsltGenericDebugContext,
  4950. "xsltIf: test evaluate to %d\n", res));
  4951. #endif
  4952. if (res) {
  4953. xsltApplySequenceConstructor(ctxt,
  4954. contextNode, inst->children, NULL);
  4955. }
  4956. } else {
  4957. #ifdef WITH_XSLT_DEBUG_PROCESS
  4958. XSLT_TRACE(ctxt, XSLT_TRACE_IF,
  4959. xsltGenericDebug(xsltGenericDebugContext,
  4960. "xsltIf: test didn't evaluate to a boolean\n"));
  4961. #endif
  4962. ctxt->state = XSLT_STATE_STOPPED;
  4963. }
  4964. xmlXPathFreeObject(xpobj);
  4965. } else {
  4966. ctxt->state = XSLT_STATE_STOPPED;
  4967. }
  4968. }
  4969. #endif /* else of XSLT_FAST_IF */
  4970. error:
  4971. return;
  4972. }
  4973. /**
  4974. * xsltForEach:
  4975. * @ctxt: an XSLT transformation context
  4976. * @contextNode: the "current node" in the source tree
  4977. * @inst: the element node of the xsl:for-each instruction
  4978. * @castedComp: the compiled information of the instruction
  4979. *
  4980. * Process the xslt for-each node on the source node
  4981. */
  4982. void
  4983. xsltForEach(xsltTransformContextPtr ctxt, xmlNodePtr contextNode,
  4984. xmlNodePtr inst, xsltElemPreCompPtr castedComp)
  4985. {
  4986. #ifdef XSLT_REFACTORED
  4987. xsltStyleItemForEachPtr comp = (xsltStyleItemForEachPtr) castedComp;
  4988. #else
  4989. xsltStylePreCompPtr comp = (xsltStylePreCompPtr) castedComp;
  4990. #endif
  4991. int i;
  4992. xmlXPathObjectPtr res = NULL;
  4993. xmlNodePtr cur, curInst;
  4994. xmlNodeSetPtr list = NULL;
  4995. xmlNodeSetPtr oldList;
  4996. int oldXPProximityPosition, oldXPContextSize;
  4997. xmlNodePtr oldContextNode;
  4998. xsltTemplatePtr oldCurTemplRule;
  4999. xmlDocPtr oldXPDoc;
  5000. xsltDocumentPtr oldDocInfo;
  5001. xmlXPathContextPtr xpctxt;
  5002. if ((ctxt == NULL) || (contextNode == NULL) || (inst == NULL)) {
  5003. xsltGenericError(xsltGenericErrorContext,
  5004. "xsltForEach(): Bad arguments.\n");
  5005. return;
  5006. }
  5007. if (comp == NULL) {
  5008. xsltTransformError(ctxt, NULL, inst,
  5009. "Internal error in xsltForEach(): "
  5010. "The XSLT 'for-each' instruction was not compiled.\n");
  5011. return;
  5012. }
  5013. if ((comp->select == NULL) || (comp->comp == NULL)) {
  5014. xsltTransformError(ctxt, NULL, inst,
  5015. "Internal error in xsltForEach(): "
  5016. "The selecting expression of the XSLT 'for-each' "
  5017. "instruction was not compiled correctly.\n");
  5018. return;
  5019. }
  5020. xpctxt = ctxt->xpathCtxt;
  5021. #ifdef WITH_XSLT_DEBUG_PROCESS
  5022. XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
  5023. "xsltForEach: select %s\n", comp->select));
  5024. #endif
  5025. /*
  5026. * Save context states.
  5027. */
  5028. oldDocInfo = ctxt->document;
  5029. oldList = ctxt->nodeList;
  5030. oldContextNode = ctxt->node;
  5031. /*
  5032. * The "current template rule" is cleared for the instantiation of
  5033. * xsl:for-each.
  5034. */
  5035. oldCurTemplRule = ctxt->currentTemplateRule;
  5036. ctxt->currentTemplateRule = NULL;
  5037. oldXPDoc = xpctxt->doc;
  5038. oldXPProximityPosition = xpctxt->proximityPosition;
  5039. oldXPContextSize = xpctxt->contextSize;
  5040. /*
  5041. * Evaluate the 'select' expression.
  5042. */
  5043. res = xsltPreCompEval(ctxt, contextNode, comp);
  5044. if (res != NULL) {
  5045. if (res->type == XPATH_NODESET)
  5046. list = res->nodesetval;
  5047. else {
  5048. xsltTransformError(ctxt, NULL, inst,
  5049. "The 'select' expression does not evaluate to a node set.\n");
  5050. #ifdef WITH_XSLT_DEBUG_PROCESS
  5051. XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
  5052. "xsltForEach: select didn't evaluate to a node list\n"));
  5053. #endif
  5054. goto error;
  5055. }
  5056. } else {
  5057. xsltTransformError(ctxt, NULL, inst,
  5058. "Failed to evaluate the 'select' expression.\n");
  5059. ctxt->state = XSLT_STATE_STOPPED;
  5060. goto error;
  5061. }
  5062. if ((list == NULL) || (list->nodeNr <= 0))
  5063. goto exit;
  5064. #ifdef WITH_XSLT_DEBUG_PROCESS
  5065. XSLT_TRACE(ctxt,XSLT_TRACE_FOR_EACH,xsltGenericDebug(xsltGenericDebugContext,
  5066. "xsltForEach: select evaluates to %d nodes\n", list->nodeNr));
  5067. #endif
  5068. /*
  5069. * Set the list; this has to be done already here for xsltDoSortFunction().
  5070. */
  5071. ctxt->nodeList = list;
  5072. /*
  5073. * Handle xsl:sort instructions and skip them for further processing.
  5074. * BUG TODO: We are not using namespaced potentially defined on the
  5075. * xsl:sort element; XPath expression might fail.
  5076. */
  5077. curInst = inst->children;
  5078. if (IS_XSLT_ELEM(curInst) && IS_XSLT_NAME(curInst, "sort")) {
  5079. int nbsorts = 0;
  5080. xmlNodePtr sorts[XSLT_MAX_SORT];
  5081. sorts[nbsorts++] = curInst;
  5082. #ifdef WITH_DEBUGGER
  5083. if (xslDebugStatus != XSLT_DEBUG_NONE)
  5084. xslHandleDebugger(curInst, contextNode, NULL, ctxt);
  5085. #endif
  5086. curInst = curInst->next;
  5087. while (IS_XSLT_ELEM(curInst) && IS_XSLT_NAME(curInst, "sort")) {
  5088. if (nbsorts >= XSLT_MAX_SORT) {
  5089. xsltTransformError(ctxt, NULL, curInst,
  5090. "The number of xsl:sort instructions exceeds the "
  5091. "maximum (%d) allowed by this processor.\n",
  5092. XSLT_MAX_SORT);
  5093. goto error;
  5094. } else {
  5095. sorts[nbsorts++] = curInst;
  5096. }
  5097. #ifdef WITH_DEBUGGER
  5098. if (xslDebugStatus != XSLT_DEBUG_NONE)
  5099. xslHandleDebugger(curInst, contextNode, NULL, ctxt);
  5100. #endif
  5101. curInst = curInst->next;
  5102. }
  5103. xsltDoSortFunction(ctxt, sorts, nbsorts);
  5104. }
  5105. xpctxt->contextSize = list->nodeNr;
  5106. /*
  5107. * Instantiate the sequence constructor for each selected node.
  5108. */
  5109. for (i = 0; i < list->nodeNr; i++) {
  5110. cur = list->nodeTab[i];
  5111. /*
  5112. * The selected node becomes the "current node".
  5113. */
  5114. ctxt->node = cur;
  5115. /*
  5116. * An xsl:for-each can change the current context doc.
  5117. * OPTIMIZE TODO: Get rid of the need to set the context doc.
  5118. */
  5119. if ((cur->type != XML_NAMESPACE_DECL) && (cur->doc != NULL))
  5120. xpctxt->doc = cur->doc;
  5121. xpctxt->proximityPosition = i + 1;
  5122. xsltApplySequenceConstructor(ctxt, cur, curInst, NULL);
  5123. }
  5124. exit:
  5125. error:
  5126. if (res != NULL)
  5127. xmlXPathFreeObject(res);
  5128. /*
  5129. * Restore old states.
  5130. */
  5131. ctxt->document = oldDocInfo;
  5132. ctxt->nodeList = oldList;
  5133. ctxt->node = oldContextNode;
  5134. ctxt->currentTemplateRule = oldCurTemplRule;
  5135. xpctxt->doc = oldXPDoc;
  5136. xpctxt->contextSize = oldXPContextSize;
  5137. xpctxt->proximityPosition = oldXPProximityPosition;
  5138. }
  5139. /************************************************************************
  5140. * *
  5141. * Generic interface *
  5142. * *
  5143. ************************************************************************/
  5144. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  5145. typedef struct xsltHTMLVersion {
  5146. const char *version;
  5147. const char *public;
  5148. const char *system;
  5149. } xsltHTMLVersion;
  5150. static xsltHTMLVersion xsltHTMLVersions[] = {
  5151. { "5", NULL, "about:legacy-compat" },
  5152. { "4.01frame", "-//W3C//DTD HTML 4.01 Frameset//EN",
  5153. "http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd"},
  5154. { "4.01strict", "-//W3C//DTD HTML 4.01//EN",
  5155. "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd"},
  5156. { "4.01trans", "-//W3C//DTD HTML 4.01 Transitional//EN",
  5157. "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"},
  5158. { "4.01", "-//W3C//DTD HTML 4.01 Transitional//EN",
  5159. "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"},
  5160. { "4.0strict", "-//W3C//DTD HTML 4.01//EN",
  5161. "http://www.w3.org/TR/html4/strict.dtd"},
  5162. { "4.0trans", "-//W3C//DTD HTML 4.01 Transitional//EN",
  5163. "http://www.w3.org/TR/html4/loose.dtd"},
  5164. { "4.0frame", "-//W3C//DTD HTML 4.01 Frameset//EN",
  5165. "http://www.w3.org/TR/html4/frameset.dtd"},
  5166. { "4.0", "-//W3C//DTD HTML 4.01 Transitional//EN",
  5167. "http://www.w3.org/TR/html4/loose.dtd"},
  5168. { "3.2", "-//W3C//DTD HTML 3.2//EN", NULL }
  5169. };
  5170. /**
  5171. * xsltGetHTMLIDs:
  5172. * @version: the version string
  5173. * @publicID: used to return the public ID
  5174. * @systemID: used to return the system ID
  5175. *
  5176. * Returns -1 if not found, 0 otherwise and the system and public
  5177. * Identifier for this given verion of HTML
  5178. */
  5179. static int
  5180. xsltGetHTMLIDs(const xmlChar *version, const xmlChar **publicID,
  5181. const xmlChar **systemID) {
  5182. unsigned int i;
  5183. if (version == NULL)
  5184. return(-1);
  5185. for (i = 0;i < (sizeof(xsltHTMLVersions)/sizeof(xsltHTMLVersions[1]));
  5186. i++) {
  5187. if (!xmlStrcasecmp(version,
  5188. (const xmlChar *) xsltHTMLVersions[i].version)) {
  5189. if (publicID != NULL)
  5190. *publicID = (const xmlChar *) xsltHTMLVersions[i].public;
  5191. if (systemID != NULL)
  5192. *systemID = (const xmlChar *) xsltHTMLVersions[i].system;
  5193. return(0);
  5194. }
  5195. }
  5196. return(-1);
  5197. }
  5198. #endif
  5199. /**
  5200. * xsltApplyStripSpaces:
  5201. * @ctxt: a XSLT process context
  5202. * @node: the root of the XML tree
  5203. *
  5204. * Strip the unwanted ignorable spaces from the input tree
  5205. */
  5206. void
  5207. xsltApplyStripSpaces(xsltTransformContextPtr ctxt, xmlNodePtr node) {
  5208. xmlNodePtr current;
  5209. #ifdef WITH_XSLT_DEBUG_PROCESS
  5210. int nb = 0;
  5211. #endif
  5212. current = node;
  5213. while (current != NULL) {
  5214. /*
  5215. * Cleanup children empty nodes if asked for
  5216. */
  5217. if ((IS_XSLT_REAL_NODE(current)) &&
  5218. (current->children != NULL) &&
  5219. (xsltFindElemSpaceHandling(ctxt, current))) {
  5220. xmlNodePtr delete = NULL, cur = current->children;
  5221. while (cur != NULL) {
  5222. if (IS_BLANK_NODE(cur))
  5223. delete = cur;
  5224. cur = cur->next;
  5225. if (delete != NULL) {
  5226. xmlUnlinkNode(delete);
  5227. xmlFreeNode(delete);
  5228. delete = NULL;
  5229. #ifdef WITH_XSLT_DEBUG_PROCESS
  5230. nb++;
  5231. #endif
  5232. }
  5233. }
  5234. }
  5235. /*
  5236. * Skip to next node in document order.
  5237. */
  5238. if (node->type == XML_ENTITY_REF_NODE) {
  5239. /* process deep in entities */
  5240. xsltApplyStripSpaces(ctxt, node->children);
  5241. }
  5242. if ((current->children != NULL) &&
  5243. (current->type != XML_ENTITY_REF_NODE)) {
  5244. current = current->children;
  5245. } else if (current->next != NULL) {
  5246. current = current->next;
  5247. } else {
  5248. do {
  5249. current = current->parent;
  5250. if (current == NULL)
  5251. break;
  5252. if (current == node)
  5253. goto done;
  5254. if (current->next != NULL) {
  5255. current = current->next;
  5256. break;
  5257. }
  5258. } while (current != NULL);
  5259. }
  5260. }
  5261. done:
  5262. #ifdef WITH_XSLT_DEBUG_PROCESS
  5263. XSLT_TRACE(ctxt,XSLT_TRACE_STRIP_SPACES,xsltGenericDebug(xsltGenericDebugContext,
  5264. "xsltApplyStripSpaces: removed %d ignorable blank node\n", nb));
  5265. #endif
  5266. return;
  5267. }
  5268. static int
  5269. xsltCountKeys(xsltTransformContextPtr ctxt)
  5270. {
  5271. xsltStylesheetPtr style;
  5272. xsltKeyDefPtr keyd;
  5273. if (ctxt == NULL)
  5274. return(-1);
  5275. /*
  5276. * Do we have those nastly templates with a key() in the match pattern?
  5277. */
  5278. ctxt->hasTemplKeyPatterns = 0;
  5279. style = ctxt->style;
  5280. while (style != NULL) {
  5281. if (style->keyMatch != NULL) {
  5282. ctxt->hasTemplKeyPatterns = 1;
  5283. break;
  5284. }
  5285. style = xsltNextImport(style);
  5286. }
  5287. /*
  5288. * Count number of key declarations.
  5289. */
  5290. ctxt->nbKeys = 0;
  5291. style = ctxt->style;
  5292. while (style != NULL) {
  5293. keyd = style->keys;
  5294. while (keyd) {
  5295. ctxt->nbKeys++;
  5296. keyd = keyd->next;
  5297. }
  5298. style = xsltNextImport(style);
  5299. }
  5300. return(ctxt->nbKeys);
  5301. }
  5302. /**
  5303. * xsltApplyStylesheetInternal:
  5304. * @style: a parsed XSLT stylesheet
  5305. * @doc: a parsed XML document
  5306. * @params: a NULL terminated array of parameters names/values tuples
  5307. * @output: the targetted output
  5308. * @profile: profile FILE * output or NULL
  5309. * @user: user provided parameter
  5310. *
  5311. * Apply the stylesheet to the document
  5312. * NOTE: This may lead to a non-wellformed output XML wise !
  5313. *
  5314. * Returns the result document or NULL in case of error
  5315. */
  5316. static xmlDocPtr
  5317. xsltApplyStylesheetInternal(xsltStylesheetPtr style, xmlDocPtr doc,
  5318. const char **params, const char *output,
  5319. FILE * profile, xsltTransformContextPtr userCtxt)
  5320. {
  5321. xmlDocPtr res = NULL;
  5322. xsltTransformContextPtr ctxt = NULL;
  5323. xmlNodePtr root, node;
  5324. const xmlChar *method;
  5325. const xmlChar *doctypePublic;
  5326. const xmlChar *doctypeSystem;
  5327. const xmlChar *version;
  5328. const xmlChar *encoding;
  5329. xsltStackElemPtr variables;
  5330. xsltStackElemPtr vptr;
  5331. xsltInitGlobals();
  5332. if ((style == NULL) || (doc == NULL))
  5333. return (NULL);
  5334. if (style->internalized == 0) {
  5335. #ifdef WITH_XSLT_DEBUG
  5336. xsltGenericDebug(xsltGenericDebugContext,
  5337. "Stylesheet was not fully internalized !\n");
  5338. #endif
  5339. }
  5340. if (doc->intSubset != NULL) {
  5341. /*
  5342. * Avoid hitting the DTD when scanning nodes
  5343. * but keep it linked as doc->intSubset
  5344. */
  5345. xmlNodePtr cur = (xmlNodePtr) doc->intSubset;
  5346. if (cur->next != NULL)
  5347. cur->next->prev = cur->prev;
  5348. if (cur->prev != NULL)
  5349. cur->prev->next = cur->next;
  5350. if (doc->children == cur)
  5351. doc->children = cur->next;
  5352. if (doc->last == cur)
  5353. doc->last = cur->prev;
  5354. cur->prev = cur->next = NULL;
  5355. }
  5356. /*
  5357. * Check for XPath document order availability
  5358. */
  5359. root = xmlDocGetRootElement(doc);
  5360. if (root != NULL) {
  5361. if (((ptrdiff_t) root->content >= 0) &&
  5362. (xslDebugStatus == XSLT_DEBUG_NONE))
  5363. xmlXPathOrderDocElems(doc);
  5364. }
  5365. if (userCtxt != NULL)
  5366. ctxt = userCtxt;
  5367. else
  5368. ctxt = xsltNewTransformContext(style, doc);
  5369. if (ctxt == NULL)
  5370. return (NULL);
  5371. ctxt->initialContextDoc = doc;
  5372. ctxt->initialContextNode = (xmlNodePtr) doc;
  5373. if (profile != NULL) {
  5374. #ifdef WITH_PROFILER
  5375. ctxt->profile = 1;
  5376. #else
  5377. xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
  5378. "xsltApplyStylesheetInternal: "
  5379. "libxslt compiled without profiler\n");
  5380. goto error;
  5381. #endif
  5382. }
  5383. if (output != NULL)
  5384. ctxt->outputFile = output;
  5385. else
  5386. ctxt->outputFile = NULL;
  5387. /*
  5388. * internalize the modes if needed
  5389. */
  5390. if (ctxt->dict != NULL) {
  5391. if (ctxt->mode != NULL)
  5392. ctxt->mode = xmlDictLookup(ctxt->dict, ctxt->mode, -1);
  5393. if (ctxt->modeURI != NULL)
  5394. ctxt->modeURI = xmlDictLookup(ctxt->dict, ctxt->modeURI, -1);
  5395. }
  5396. XSLT_GET_IMPORT_PTR(method, style, method)
  5397. XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
  5398. XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
  5399. XSLT_GET_IMPORT_PTR(version, style, version)
  5400. XSLT_GET_IMPORT_PTR(encoding, style, encoding)
  5401. if ((method != NULL) &&
  5402. (!xmlStrEqual(method, (const xmlChar *) "xml")))
  5403. {
  5404. if (xmlStrEqual(method, (const xmlChar *) "html")) {
  5405. ctxt->type = XSLT_OUTPUT_HTML;
  5406. if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
  5407. res = htmlNewDoc(doctypeSystem, doctypePublic);
  5408. } else {
  5409. if (version == NULL) {
  5410. xmlDtdPtr dtd;
  5411. res = htmlNewDoc(NULL, NULL);
  5412. /*
  5413. * Make sure no DTD node is generated in this case
  5414. */
  5415. if (res != NULL) {
  5416. dtd = xmlGetIntSubset(res);
  5417. if (dtd != NULL) {
  5418. xmlUnlinkNode((xmlNodePtr) dtd);
  5419. xmlFreeDtd(dtd);
  5420. }
  5421. res->intSubset = NULL;
  5422. res->extSubset = NULL;
  5423. }
  5424. } else {
  5425. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  5426. xsltGetHTMLIDs(version, &doctypePublic, &doctypeSystem);
  5427. #endif
  5428. res = htmlNewDoc(doctypeSystem, doctypePublic);
  5429. }
  5430. }
  5431. if (res == NULL)
  5432. goto error;
  5433. res->dict = ctxt->dict;
  5434. xmlDictReference(res->dict);
  5435. #ifdef WITH_XSLT_DEBUG
  5436. xsltGenericDebug(xsltGenericDebugContext,
  5437. "reusing transformation dict for output\n");
  5438. #endif
  5439. } else if (xmlStrEqual(method, (const xmlChar *) "xhtml")) {
  5440. xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
  5441. "xsltApplyStylesheetInternal: unsupported method xhtml, using html\n");
  5442. ctxt->type = XSLT_OUTPUT_HTML;
  5443. res = htmlNewDoc(doctypeSystem, doctypePublic);
  5444. if (res == NULL)
  5445. goto error;
  5446. res->dict = ctxt->dict;
  5447. xmlDictReference(res->dict);
  5448. #ifdef WITH_XSLT_DEBUG
  5449. xsltGenericDebug(xsltGenericDebugContext,
  5450. "reusing transformation dict for output\n");
  5451. #endif
  5452. } else if (xmlStrEqual(method, (const xmlChar *) "text")) {
  5453. ctxt->type = XSLT_OUTPUT_TEXT;
  5454. res = xmlNewDoc(style->version);
  5455. if (res == NULL)
  5456. goto error;
  5457. res->dict = ctxt->dict;
  5458. xmlDictReference(res->dict);
  5459. #ifdef WITH_XSLT_DEBUG
  5460. xsltGenericDebug(xsltGenericDebugContext,
  5461. "reusing transformation dict for output\n");
  5462. #endif
  5463. } else {
  5464. xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
  5465. "xsltApplyStylesheetInternal: unsupported method (%s)\n",
  5466. method);
  5467. goto error;
  5468. }
  5469. } else {
  5470. ctxt->type = XSLT_OUTPUT_XML;
  5471. res = xmlNewDoc(style->version);
  5472. if (res == NULL)
  5473. goto error;
  5474. res->dict = ctxt->dict;
  5475. xmlDictReference(ctxt->dict);
  5476. #ifdef WITH_XSLT_DEBUG
  5477. xsltGenericDebug(xsltGenericDebugContext,
  5478. "reusing transformation dict for output\n");
  5479. #endif
  5480. }
  5481. res->charset = XML_CHAR_ENCODING_UTF8;
  5482. if (encoding != NULL)
  5483. res->encoding = xmlStrdup(encoding);
  5484. variables = style->variables;
  5485. ctxt->node = (xmlNodePtr) doc;
  5486. ctxt->output = res;
  5487. ctxt->xpathCtxt->contextSize = 1;
  5488. ctxt->xpathCtxt->proximityPosition = 1;
  5489. ctxt->xpathCtxt->node = NULL; /* TODO: Set the context node here? */
  5490. /*
  5491. * Start the evaluation, evaluate the params, the stylesheets globals
  5492. * and start by processing the top node.
  5493. */
  5494. if (xsltNeedElemSpaceHandling(ctxt))
  5495. xsltApplyStripSpaces(ctxt, xmlDocGetRootElement(doc));
  5496. /*
  5497. * Evaluate global params and user-provided params.
  5498. */
  5499. if (ctxt->globalVars == NULL)
  5500. ctxt->globalVars = xmlHashCreate(20);
  5501. if (params != NULL) {
  5502. xsltEvalUserParams(ctxt, params);
  5503. }
  5504. /* need to be called before evaluating global variables */
  5505. xsltCountKeys(ctxt);
  5506. xsltEvalGlobalVariables(ctxt);
  5507. /* Clean up any unused RVTs. */
  5508. xsltReleaseLocalRVTs(ctxt, NULL);
  5509. ctxt->insert = (xmlNodePtr) res;
  5510. ctxt->varsBase = ctxt->varsNr - 1;
  5511. /*
  5512. * Start processing the source tree -----------------------------------
  5513. */
  5514. xsltProcessOneNode(ctxt, ctxt->node, NULL);
  5515. /*
  5516. * Remove all remaining vars from the stack.
  5517. */
  5518. xsltLocalVariablePop(ctxt, 0, -2);
  5519. xsltShutdownCtxtExts(ctxt);
  5520. xsltCleanupTemplates(style); /* TODO: <- style should be read only */
  5521. /*
  5522. * Now cleanup our variables so stylesheet can be re-used
  5523. *
  5524. * TODO: this is not needed anymore global variables are copied
  5525. * and not evaluated directly anymore, keep this as a check
  5526. */
  5527. if (style->variables != variables) {
  5528. vptr = style->variables;
  5529. while (vptr->next != variables)
  5530. vptr = vptr->next;
  5531. vptr->next = NULL;
  5532. xsltFreeStackElemList(style->variables);
  5533. style->variables = variables;
  5534. }
  5535. vptr = style->variables;
  5536. while (vptr != NULL) {
  5537. if (vptr->computed) {
  5538. if (vptr->value != NULL) {
  5539. xmlXPathFreeObject(vptr->value);
  5540. vptr->value = NULL;
  5541. vptr->computed = 0;
  5542. }
  5543. }
  5544. vptr = vptr->next;
  5545. }
  5546. #if 0
  5547. /*
  5548. * code disabled by wmb; awaiting kb's review
  5549. * problem is that global variable(s) may contain xpath objects
  5550. * from doc associated with RVT, so can't be freed at this point.
  5551. * xsltFreeTransformContext includes a call to xsltFreeRVTs, so
  5552. * I assume this shouldn't be required at this point.
  5553. */
  5554. /*
  5555. * Free all remaining tree fragments.
  5556. */
  5557. xsltFreeRVTs(ctxt);
  5558. #endif
  5559. /*
  5560. * Do some post processing work depending on the generated output
  5561. */
  5562. root = xmlDocGetRootElement(res);
  5563. if (root != NULL) {
  5564. const xmlChar *doctype = NULL;
  5565. if ((root->ns != NULL) && (root->ns->prefix != NULL))
  5566. doctype = xmlDictQLookup(ctxt->dict, root->ns->prefix, root->name);
  5567. if (doctype == NULL)
  5568. doctype = root->name;
  5569. /*
  5570. * Apply the default selection of the method
  5571. */
  5572. if ((method == NULL) &&
  5573. (root->ns == NULL) &&
  5574. (!xmlStrcasecmp(root->name, (const xmlChar *) "html"))) {
  5575. xmlNodePtr tmp;
  5576. tmp = res->children;
  5577. while ((tmp != NULL) && (tmp != root)) {
  5578. if (tmp->type == XML_ELEMENT_NODE)
  5579. break;
  5580. if ((tmp->type == XML_TEXT_NODE) && (!xmlIsBlankNode(tmp)))
  5581. break;
  5582. tmp = tmp->next;
  5583. }
  5584. if (tmp == root) {
  5585. ctxt->type = XSLT_OUTPUT_HTML;
  5586. /*
  5587. * REVISIT TODO: XML_HTML_DOCUMENT_NODE is set after the
  5588. * transformation on the doc, but functions like
  5589. */
  5590. res->type = XML_HTML_DOCUMENT_NODE;
  5591. if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
  5592. res->intSubset = xmlCreateIntSubset(res, doctype,
  5593. doctypePublic,
  5594. doctypeSystem);
  5595. #ifdef XSLT_GENERATE_HTML_DOCTYPE
  5596. } else if (version != NULL) {
  5597. xsltGetHTMLIDs(version, &doctypePublic,
  5598. &doctypeSystem);
  5599. if (((doctypePublic != NULL) || (doctypeSystem != NULL)))
  5600. res->intSubset =
  5601. xmlCreateIntSubset(res, doctype,
  5602. doctypePublic,
  5603. doctypeSystem);
  5604. #endif
  5605. }
  5606. }
  5607. }
  5608. if (ctxt->type == XSLT_OUTPUT_XML) {
  5609. XSLT_GET_IMPORT_PTR(doctypePublic, style, doctypePublic)
  5610. XSLT_GET_IMPORT_PTR(doctypeSystem, style, doctypeSystem)
  5611. if (((doctypePublic != NULL) || (doctypeSystem != NULL))) {
  5612. xmlNodePtr last;
  5613. /* Need a small "hack" here to assure DTD comes before
  5614. possible comment nodes */
  5615. node = res->children;
  5616. last = res->last;
  5617. res->children = NULL;
  5618. res->last = NULL;
  5619. res->intSubset = xmlCreateIntSubset(res, doctype,
  5620. doctypePublic,
  5621. doctypeSystem);
  5622. if (res->children != NULL) {
  5623. res->children->next = node;
  5624. node->prev = res->children;
  5625. res->last = last;
  5626. } else {
  5627. res->children = node;
  5628. res->last = last;
  5629. }
  5630. }
  5631. }
  5632. }
  5633. xmlXPathFreeNodeSet(ctxt->nodeList);
  5634. #ifdef WITH_PROFILER
  5635. if (profile != NULL) {
  5636. xsltSaveProfiling(ctxt, profile);
  5637. }
  5638. #endif
  5639. /*
  5640. * Be pedantic.
  5641. */
  5642. if ((ctxt != NULL) && (ctxt->state != XSLT_STATE_OK)) {
  5643. xmlFreeDoc(res);
  5644. res = NULL;
  5645. }
  5646. if ((res != NULL) && (ctxt != NULL) && (output != NULL)) {
  5647. int ret;
  5648. ret = xsltCheckWrite(ctxt->sec, ctxt, (const xmlChar *) output);
  5649. if (ret == 0) {
  5650. xsltTransformError(ctxt, NULL, NULL,
  5651. "xsltApplyStylesheet: forbidden to save to %s\n",
  5652. output);
  5653. } else if (ret < 0) {
  5654. xsltTransformError(ctxt, NULL, NULL,
  5655. "xsltApplyStylesheet: saving to %s may not be possible\n",
  5656. output);
  5657. }
  5658. }
  5659. #ifdef XSLT_DEBUG_PROFILE_CACHE
  5660. printf("# Cache:\n");
  5661. printf("# Reused tree fragments: %d\n", ctxt->cache->dbgReusedRVTs);
  5662. printf("# Reused variables : %d\n", ctxt->cache->dbgReusedVars);
  5663. #endif
  5664. if ((ctxt != NULL) && (userCtxt == NULL))
  5665. xsltFreeTransformContext(ctxt);
  5666. return (res);
  5667. error:
  5668. if (res != NULL)
  5669. xmlFreeDoc(res);
  5670. #ifdef XSLT_DEBUG_PROFILE_CACHE
  5671. printf("# Cache:\n");
  5672. printf("# Reused tree fragments: %d\n", ctxt->cache->dbgReusedRVTs);
  5673. printf("# Reused variables : %d\n", ctxt->cache->dbgReusedVars);
  5674. #endif
  5675. if ((ctxt != NULL) && (userCtxt == NULL))
  5676. xsltFreeTransformContext(ctxt);
  5677. return (NULL);
  5678. }
  5679. /**
  5680. * xsltApplyStylesheet:
  5681. * @style: a parsed XSLT stylesheet
  5682. * @doc: a parsed XML document
  5683. * @params: a NULL terminated arry of parameters names/values tuples
  5684. *
  5685. * Apply the stylesheet to the document
  5686. * NOTE: This may lead to a non-wellformed output XML wise !
  5687. *
  5688. * Returns the result document or NULL in case of error
  5689. */
  5690. xmlDocPtr
  5691. xsltApplyStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
  5692. const char **params)
  5693. {
  5694. return (xsltApplyStylesheetInternal(style, doc, params, NULL, NULL, NULL));
  5695. }
  5696. /**
  5697. * xsltProfileStylesheet:
  5698. * @style: a parsed XSLT stylesheet
  5699. * @doc: a parsed XML document
  5700. * @params: a NULL terminated arry of parameters names/values tuples
  5701. * @output: a FILE * for the profiling output
  5702. *
  5703. * Apply the stylesheet to the document and dump the profiling to
  5704. * the given output.
  5705. *
  5706. * Returns the result document or NULL in case of error
  5707. */
  5708. xmlDocPtr
  5709. xsltProfileStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
  5710. const char **params, FILE * output)
  5711. {
  5712. xmlDocPtr res;
  5713. res = xsltApplyStylesheetInternal(style, doc, params, NULL, output, NULL);
  5714. return (res);
  5715. }
  5716. /**
  5717. * xsltApplyStylesheetUser:
  5718. * @style: a parsed XSLT stylesheet
  5719. * @doc: a parsed XML document
  5720. * @params: a NULL terminated array of parameters names/values tuples
  5721. * @output: the targetted output
  5722. * @profile: profile FILE * output or NULL
  5723. * @userCtxt: user provided transform context
  5724. *
  5725. * Apply the stylesheet to the document and allow the user to provide
  5726. * its own transformation context.
  5727. *
  5728. * Returns the result document or NULL in case of error
  5729. */
  5730. xmlDocPtr
  5731. xsltApplyStylesheetUser(xsltStylesheetPtr style, xmlDocPtr doc,
  5732. const char **params, const char *output,
  5733. FILE * profile, xsltTransformContextPtr userCtxt)
  5734. {
  5735. xmlDocPtr res;
  5736. res = xsltApplyStylesheetInternal(style, doc, params, output,
  5737. profile, userCtxt);
  5738. return (res);
  5739. }
  5740. /**
  5741. * xsltRunStylesheetUser:
  5742. * @style: a parsed XSLT stylesheet
  5743. * @doc: a parsed XML document
  5744. * @params: a NULL terminated array of parameters names/values tuples
  5745. * @output: the URL/filename ot the generated resource if available
  5746. * @SAX: a SAX handler for progressive callback output (not implemented yet)
  5747. * @IObuf: an output buffer for progressive output (not implemented yet)
  5748. * @profile: profile FILE * output or NULL
  5749. * @userCtxt: user provided transform context
  5750. *
  5751. * Apply the stylesheet to the document and generate the output according
  5752. * to @output @SAX and @IObuf. It's an error to specify both @SAX and @IObuf.
  5753. *
  5754. * NOTE: This may lead to a non-wellformed output XML wise !
  5755. * NOTE: This may also result in multiple files being generated
  5756. * NOTE: using IObuf, the result encoding used will be the one used for
  5757. * creating the output buffer, use the following macro to read it
  5758. * from the stylesheet
  5759. * XSLT_GET_IMPORT_PTR(encoding, style, encoding)
  5760. * NOTE: using SAX, any encoding specified in the stylesheet will be lost
  5761. * since the interface uses only UTF8
  5762. *
  5763. * Returns the number of by written to the main resource or -1 in case of
  5764. * error.
  5765. */
  5766. int
  5767. xsltRunStylesheetUser(xsltStylesheetPtr style, xmlDocPtr doc,
  5768. const char **params, const char *output,
  5769. xmlSAXHandlerPtr SAX, xmlOutputBufferPtr IObuf,
  5770. FILE * profile, xsltTransformContextPtr userCtxt)
  5771. {
  5772. xmlDocPtr tmp;
  5773. int ret;
  5774. if ((output == NULL) && (SAX == NULL) && (IObuf == NULL))
  5775. return (-1);
  5776. if ((SAX != NULL) && (IObuf != NULL))
  5777. return (-1);
  5778. /* unsupported yet */
  5779. if (SAX != NULL) {
  5780. XSLT_TODO /* xsltRunStylesheet xmlSAXHandlerPtr SAX */
  5781. return (-1);
  5782. }
  5783. tmp = xsltApplyStylesheetInternal(style, doc, params, output, profile,
  5784. userCtxt);
  5785. if (tmp == NULL) {
  5786. xsltTransformError(NULL, NULL, (xmlNodePtr) doc,
  5787. "xsltRunStylesheet : run failed\n");
  5788. return (-1);
  5789. }
  5790. if (IObuf != NULL) {
  5791. /* TODO: incomplete, IObuf output not progressive */
  5792. ret = xsltSaveResultTo(IObuf, tmp, style);
  5793. } else {
  5794. ret = xsltSaveResultToFilename(output, tmp, style, 0);
  5795. }
  5796. xmlFreeDoc(tmp);
  5797. return (ret);
  5798. }
  5799. /**
  5800. * xsltRunStylesheet:
  5801. * @style: a parsed XSLT stylesheet
  5802. * @doc: a parsed XML document
  5803. * @params: a NULL terminated array of parameters names/values tuples
  5804. * @output: the URL/filename ot the generated resource if available
  5805. * @SAX: a SAX handler for progressive callback output (not implemented yet)
  5806. * @IObuf: an output buffer for progressive output (not implemented yet)
  5807. *
  5808. * Apply the stylesheet to the document and generate the output according
  5809. * to @output @SAX and @IObuf. It's an error to specify both @SAX and @IObuf.
  5810. *
  5811. * NOTE: This may lead to a non-wellformed output XML wise !
  5812. * NOTE: This may also result in multiple files being generated
  5813. * NOTE: using IObuf, the result encoding used will be the one used for
  5814. * creating the output buffer, use the following macro to read it
  5815. * from the stylesheet
  5816. * XSLT_GET_IMPORT_PTR(encoding, style, encoding)
  5817. * NOTE: using SAX, any encoding specified in the stylesheet will be lost
  5818. * since the interface uses only UTF8
  5819. *
  5820. * Returns the number of bytes written to the main resource or -1 in case of
  5821. * error.
  5822. */
  5823. int
  5824. xsltRunStylesheet(xsltStylesheetPtr style, xmlDocPtr doc,
  5825. const char **params, const char *output,
  5826. xmlSAXHandlerPtr SAX, xmlOutputBufferPtr IObuf)
  5827. {
  5828. return(xsltRunStylesheetUser(style, doc, params, output, SAX, IObuf,
  5829. NULL, NULL));
  5830. }
  5831. static void
  5832. xsltMessageWrapper(xsltTransformContextPtr ctxt, xmlNodePtr node,
  5833. xmlNodePtr inst, xsltElemPreCompPtr comp ATTRIBUTE_UNUSED) {
  5834. xsltMessage(ctxt, node, inst);
  5835. }
  5836. /**
  5837. * xsltRegisterAllElement:
  5838. * @ctxt: the XPath context
  5839. *
  5840. * Registers all default XSLT elements in this context
  5841. */
  5842. void
  5843. xsltRegisterAllElement(xsltTransformContextPtr ctxt)
  5844. {
  5845. xsltRegisterExtElement(ctxt, (const xmlChar *) "apply-templates",
  5846. XSLT_NAMESPACE,
  5847. xsltApplyTemplates);
  5848. xsltRegisterExtElement(ctxt, (const xmlChar *) "apply-imports",
  5849. XSLT_NAMESPACE,
  5850. xsltApplyImports);
  5851. xsltRegisterExtElement(ctxt, (const xmlChar *) "call-template",
  5852. XSLT_NAMESPACE,
  5853. xsltCallTemplate);
  5854. xsltRegisterExtElement(ctxt, (const xmlChar *) "element",
  5855. XSLT_NAMESPACE,
  5856. xsltElement);
  5857. xsltRegisterExtElement(ctxt, (const xmlChar *) "attribute",
  5858. XSLT_NAMESPACE,
  5859. xsltAttribute);
  5860. xsltRegisterExtElement(ctxt, (const xmlChar *) "text",
  5861. XSLT_NAMESPACE,
  5862. xsltText);
  5863. xsltRegisterExtElement(ctxt, (const xmlChar *) "processing-instruction",
  5864. XSLT_NAMESPACE,
  5865. xsltProcessingInstruction);
  5866. xsltRegisterExtElement(ctxt, (const xmlChar *) "comment",
  5867. XSLT_NAMESPACE,
  5868. xsltComment);
  5869. xsltRegisterExtElement(ctxt, (const xmlChar *) "copy",
  5870. XSLT_NAMESPACE,
  5871. xsltCopy);
  5872. xsltRegisterExtElement(ctxt, (const xmlChar *) "value-of",
  5873. XSLT_NAMESPACE,
  5874. xsltValueOf);
  5875. xsltRegisterExtElement(ctxt, (const xmlChar *) "number",
  5876. XSLT_NAMESPACE,
  5877. xsltNumber);
  5878. xsltRegisterExtElement(ctxt, (const xmlChar *) "for-each",
  5879. XSLT_NAMESPACE,
  5880. xsltForEach);
  5881. xsltRegisterExtElement(ctxt, (const xmlChar *) "if",
  5882. XSLT_NAMESPACE,
  5883. xsltIf);
  5884. xsltRegisterExtElement(ctxt, (const xmlChar *) "choose",
  5885. XSLT_NAMESPACE,
  5886. xsltChoose);
  5887. xsltRegisterExtElement(ctxt, (const xmlChar *) "sort",
  5888. XSLT_NAMESPACE,
  5889. xsltSort);
  5890. xsltRegisterExtElement(ctxt, (const xmlChar *) "copy-of",
  5891. XSLT_NAMESPACE,
  5892. xsltCopyOf);
  5893. xsltRegisterExtElement(ctxt, (const xmlChar *) "message",
  5894. XSLT_NAMESPACE,
  5895. xsltMessageWrapper);
  5896. /*
  5897. * Those don't have callable entry points but are registered anyway
  5898. */
  5899. xsltRegisterExtElement(ctxt, (const xmlChar *) "variable",
  5900. XSLT_NAMESPACE,
  5901. xsltDebug);
  5902. xsltRegisterExtElement(ctxt, (const xmlChar *) "param",
  5903. XSLT_NAMESPACE,
  5904. xsltDebug);
  5905. xsltRegisterExtElement(ctxt, (const xmlChar *) "with-param",
  5906. XSLT_NAMESPACE,
  5907. xsltDebug);
  5908. xsltRegisterExtElement(ctxt, (const xmlChar *) "decimal-format",
  5909. XSLT_NAMESPACE,
  5910. xsltDebug);
  5911. xsltRegisterExtElement(ctxt, (const xmlChar *) "when",
  5912. XSLT_NAMESPACE,
  5913. xsltDebug);
  5914. xsltRegisterExtElement(ctxt, (const xmlChar *) "otherwise",
  5915. XSLT_NAMESPACE,
  5916. xsltDebug);
  5917. xsltRegisterExtElement(ctxt, (const xmlChar *) "fallback",
  5918. XSLT_NAMESPACE,
  5919. xsltDebug);
  5920. }