The key issue as stated is that you need a comma, as per XQuery specs. This is true for ANY constructed XML in the context of surrounding computed constructor.
You can see this in detail in the XQuery specifications http://www.w3.org/TR/xquery-30/#id-otherConstructors Neglecting PI's for the moment. This works: element name { <child1/>, <child2/> } This is a syntax error element name { <child1/><child2/> } But this works <name> <child1/><child2/> </name> Notice the difference. The context of first expression is XQuery evaluation context. The second expression is inside a "direct constructor" ... The syntax differs. Heres a more complex example (indentation and line breaks to make it clearer) let $text := "Mixed Text" return element name { <child1/>, <child2> { <child3>{ $text , <child4>{ $text}<child5>{$text}</child5></child4>, <child6>More Text</child6> } </child3>, <child7>{$text}</child7> }</child2> } Produces: <name> <child1/> <child2> <child3>Mixed Text<child4>Mixed Text<child5>Mixed Text</child5></child4><child6>More Text</child6></child3> <child7>Mixed Text</child7> </child2> </name> XQuery starts out in "XQuery Evaluation" context, in that context everything is a sequence (, separated) If you enter a direct constructor (looks like literal XML) it enters XML evaluation context *but only for that node* Then it returns to XQuery context. Inside a direct constructor you can re-enter XQuery mode by the use of {} This occurs recursively. Your code used a "computed constructor" which enters XQuery evaluation context: document { <?hi there?><Root><?hi there?></Root> } XQuery sees 2 expressions , in the body of document, interpreted in XQuery evaluation context, NOT separated by ","s so not valid XQuery 1) <?hi there?> 2) <Root><?hi there?></Root> The 2nd PI is in direct constructed node context so could well be <Root><?hi there?><?hi again?></Root> This code will work correctly: document { <?format role="output"?> , <body> <?PI2?> </body> } Note the necessary comma. -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of "neil bradley" Sent: Wednesday, July 30, 2014 5:49 AM To: Jason Hunter Cc: [email protected] Subject: Re: [MarkLogic Dev General] Problems with Processing Instructions I am not saying that the ZIP solution does not work - I expect that it does. I am saying that the solution to construct an XML document in memory does not work. This does not work... document { <?hi there?><Root><?hi there?></Root> } Neil. on 30/7/14 10:32 AM, Jason Hunter <[email protected]<mailto:[email protected]>> wrote: > I ran the exact code I sent you just now on my laptop and it worked fine. > The file was saved and on unzipping the file and viewing it in a text editor > I see the PI before the root node as well as inside it. > > You're saying it "does not work". I'll need you to be specific on > what's happening because I'm seeing it work. And definitely to make a > valid expression where there's a PI before the root you need the > document node wrapper to make it one expression not two. > > -jh- > > On Jul 30, 2014, at 5:26 PM, neil bradley > <[email protected]<mailto:[email protected]>> wrote: > >> Jason, >> >> Thanks for your answers, and I expect that your solution to the ZIP >> problem will help me. >> >> But I dont think you noticed that I was saying that the document node does >> NOT WORK: >> >> document { <?hi there?><Root><?hi there?></Root> } >> >> Now that is totally valid XML so I cant see why it fails. >> >> Neil. >> >> >> >> on 30/7/14 10:17 AM, Jason Hunter >> <[email protected]<mailto:[email protected]>> wrote: >> >>>> Just one more point. I had thought I would solved the first, more >>>> minor issue, by wrapping the XML in a document node. But that still >>>> does not work... >>>> >>>> document { <?hi there?><Root><?hi there?></Root> } >>> >>> Correct, because without that you were providing two independent >> expressions >>> without a comma between them. You need the document wrapper to make >>> it a >>> single expression. >>> >>> I think there may be a bug in xdmp:zip-create() where only the root >>> node >> is >>> used. I was able to fix it by making the document into a string first: >>> >>> let $doc := document { >>> <?hi there?>, >>> <Root><?hi there?></Root> >>> } >>> >>> let $parts := <parts xmlns="xdmp:zip"><part>MyDoc.xml</part></parts> >>> let $zip := xdmp:zip-create($parts, text { xdmp:quote($doc) }) >> (: >>> quoted! :) >>> let $save := xdmp:save("/tmp/test.zip", $zip, <options >>> xmlns="xdmp:save"><encoding>utf8</encoding></options>) >>> return "fixed" >>> >>> -jh- >>> >>>> >>>> >>>> Neil. >>>> >>>> >>>> >>>> on 30/7/14 9:51 AM, neil bradley >>>> <[email protected]<mailto:[email protected]>> wrote: >>>> >>>>> Hi, >>>>> >>>>> I am having two problems with processing instructions. >>>>> >>>>> First, in general it seems I cannot create one that is before the >>>>> root element. >>>>> >>>>> I can type this into QC and run it and the result is as expected: >>>>> >>>>> <?hi there?> >>>>> >>>>> I can also have one embedded in an element and again it is >>>>> preserved in the output: >>>>> >>>>> <Root><?hi there?></Root> >>>>> >>>>> But it seems I cannot have a PI before a root element. This does >>>>> not work when I enter it into QC, and I get “unexpected token >>>>> syntax >>>>> error”: >>>>> >>>>> <?hi there?> >>>>> <Root><?hi there?></Root> >>>>> >>>>> However, that is a side issue to my main concern… >>>>> >>>>> I can import an XML document that has a leading PI, store it in >>>>> ML, and it is still there when I query the document. I can even >>>>> save it using xdmp:save() and the PI is still there, as I would >>>>> expect. But when I store the XML file in a ZIP instead, it is removed! >>>>> >>>>> Here is sample code that saves a ZIP file that removes the leading >>>>> PI from the document: >>>>> >>>>> let $Doc := doc("/MyDoc.xml") >>>>> let $Parts := <parts >>>>> xmlns="xdmp:zip"><part>MyDoc.xml</part></parts> >>>>> let $ZIP := xdmp:zip-create($Parts, $Doc) return >>>>> xdmp:save("c:/TEST/test.zip", $ZIP, <options >>>>> xmlns="xdmp:save"><encoding>utf8</encoding></options>) >>>>> >>>>> Does anyone have experience of this, and know any workaround. It >>>>> is really important that I preserve the PIs at the top of >>>>> documents and place them in a ZIP. >>>>> >>>>> Neil. >>>>> _______________________________________________ >>>>> General mailing list >>>>> [email protected]<mailto:[email protected]> >>>>> http://developer.marklogic.com/mailman/listinfo/general >>>>> >>>>> >>>>> >>>> _______________________________________________ >>>> General mailing list >>>> [email protected]<mailto:[email protected]> >>>> http://developer.marklogic.com/mailman/listinfo/general >>> >>> >>> >>> > > > > _______________________________________________ General mailing list [email protected]<mailto:[email protected]> http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________ General mailing list [email protected] http://developer.marklogic.com/mailman/listinfo/general
