It looks like MarkLogic 4.2 now prepends the
XML version/encoding header for you when serializing
a document. On 4.1 and older, that is not the case
it just serializes the nodes in the document.
Note that an XML "processing instruction" is
not part of the XML document (it's not even an
actual PI), it's meta-information about how to
process the serialized XML document that follows.
Here are two examples of how to generate
serialized XML output with an XML header.
4.2:
xquery version "1.0-ml";
declare function local:my-node() as element(foo)
{
<foo bar="baz">
<blah>flerp</blah>
</foo>
};
let $foo := local:my-node()
return
document { $foo }
4.1:
xquery version "1.0-ml";
declare function local:my-node() as element(foo)
{
<foo bar="bax">
<blah>flerp</blah>
</foo>
};
let $xml-pi := '<?xml version="1.0" encoding="UTF-8"?>'
let $foo := local:my-node()
return
fn:concat ($xml-pi, "
", xdmp:quote ($foo))
On Feb 28, 2011, at 10:37 AM, Nuno Job wrote:
> This should do the trick:
> document {
> <result>
> <lst name="first">
> <elem1>1</elem1>
> <elem2>2</elem2>
> </lst>
> </result>
> }
>
> Case you are interested about creating other PIs (or comments):
>
> document {
> comment {'now this is a comment'},
> processing-instruction pi { 'textual content' },
> element rootNode {
> attribute id {'5'},
> text { 'The End!' }
> }
> }
>
> Nuno
>
> On Mon, Feb 28, 2011 at 10:20 AM, Geert Josten <[email protected]>
> wrote:
> Hi Rojan,
>
>
> Personally, I wouldn’t add the xml-pi to the value returned from the
> function, but in the main body, since then you know for sure where the result
> is being sent to. But you could do it this way:
>
>
> xquery version "1.0-ml";
>
> (: buffer 1 :)
>
>
> declare function local:getSearchResult() as element(){
>
> <result>
>
> <lst name="first">
>
> <elem1>1</elem1>
>
> <elem2>2</elem2>
>
> </lst>
>
> </result>
>
> };
>
>
> declare function local:getResult() as item()+
>
> {
>
> '<?xml version="1.0" encoding="utf-8"?>',
>
> local:getSearchResult()
>
> };
>
>
> xdmp:set-response-content-type("application/xml"),
>
> xdmp:set-response-encoding("utf-8"),
>
> local:getResult()
>
>
> Kind regards,
>
> Geert
>
>
> Van: Rojan K K [mailto:[email protected]]
> Verzonden: maandag 28 februari 2011 9:53
> Aan: General MarkLogic Developer Discussion
> CC: Geert Josten
> Onderwerp: Re: [MarkLogic Dev General] prepend the xmlversion identifier with
> the resulted xml
>
>
> Hi Geert,
>
>
> Thanks for your support.
>
>
> But my actual problem is, i couldnt able to add processing instruction to the
> element.
>
>
> I am using a search functionality and has to append PI with resulted xml
>
> sample code structure is given below
>
>
> declare function ns:getResult(--- params--) as element()*
>
> {
>
> let $resultXml=ns:getSearchResult(--params--)
>
> return ($resultXml)
>
> }
>
> so that i am getting $resultXml as
>
>
> <result>
>
> <lst name="first">
>
> <elm1>1<elem1>
>
> <elm2>2<elem2>
>
> </lst>
>
> --
>
> --
>
> </result>
>
> and output xml will be the same
>
> but expected output is as given below
>
> <?xml version="1.0" encoding="UTF-8>
>
> <result>
>
> <lst name="first">
>
> <elm1>1<elem1>
>
> <elm2>2<elem2>
>
> </lst>
>
> --
>
> --
>
> </result>
>
> Please advice me how i can achieve this expected result by adding processing
> instruction to the element.
>
>
> regards,
>
> Rojan
>
>
>
> On Mon, Feb 28, 2011 at 12:55 PM, Geert Josten <[email protected]>
> wrote:
>
> Hi Rojan,
>
>
> If you precede your root element with a processing-instruction, comment or
> string, you are returning a multi-item sequence. You will have to at least
> add a + or * behind the return type. Secondly, a pi or string is not an
> element. Use item() instead.
>
>
> By the way, are you sure that MarkLogic Server doesn’t prepend the xml pi
> itself upon returning results? You can set the encoding of the response using
> xdmp:set-response-encoding
> (http://developer.marklogic.com/pubs/4.2/apidocs/AppServerBuiltins.html#xdmp:set-response-encoding)..
>
>
> Kind regards,
>
> Geert
>
>
> Van: [email protected]
> [mailto:[email protected]] Namens Rojan K K
> Verzonden: maandag 28 februari 2011 7:52
> Aan: [email protected]
> Onderwerp: [MarkLogic Dev General] prepend the xmlversion identifier with the
> resulted xml
>
>
> Hi,
>
>
> I would like to prepend the xml version identifier and encoding(<?xml
> version="1.0" encoding="UTF-8"?>) with the ouput xml which i am giving as the
> result of a method having element() return type.I tried to append it as
> string then its showing" Invalid coercion: "<?xml version="1.0"
> encoding="UTF-8"?>" as element() " since its not node type. Please
> help me to resolve this.
>
>
>
> Regards,
>
> Rojan
>
>
>
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
>
>
>
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
>
>
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
---
Ron Hitchens {mailto:[email protected]} Ronsoft Technologies
+44 7879 358 212 (voice) http://www.ronsoft.com
+1 707 924 3878 (fax) Bit Twiddling At Its Finest
"No amount of belief establishes any fact." -Unknown
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general