Help for parsing http:send-request response

Wednesday, 6 June 2012 · 8 messages · last reply 7 Jun 2012
PR
Philippe Rathé6 Jun 2012 06:39 #
Hello folks, I'm using the HTTP Module[1] for sending request. The response has he http:response element which is XML. Then at the end of that element is the body of the response. If your ask for text/plain then you will have the text just after the closing </http:response>.
    <http:response status="200" message="OK">
        ...
    </http:response>Hello World!
In my case the media-type of the response is XML. I have something like the following:
    <http:response status="200" message="OK">
        ...
    </http:response><service>
        <hello/>
    </service>
How am I suppose to parse this output? That is the EXPpath spec, I have to deal with that. But parsing it with BaseX give the following error: No elements allowed after closed root element. True, that is not valid XML document. What is the best solution to parse separately the http:response and the body? Thanks, -- Philippe [1]: http://docs.basex.org/wiki/HTTP_Module
CG
Christian Grün6 Jun 2012 12:20 #
Quoted text · Philippe Rathé · 3 lines
> I'm using the HTTP Module[1] for sending request. The response has he > http:response element which is XML. Then at the end of that element is > the body of the response.
http:send-request() will return two items. You can access the second one (the actual result) with a positional predicate:
  http:send-request(...)[2]
CG
Christian Grünin reply to Philippe Rathé, 7 Jun 2012, 19:067 Jun 2012 17:11 #
Quoted text · Philippe Rathé · 2 lines
> Okay, I thought that this error came from XQuery. I did not notice > before that XQuery can operate on invalid XML document.
A minor note: XQuery definitely requires well-formed XML as input, but the http:send-request() function generates two results: the first one represents the XML response element, the second one represents the actual result – which is the reason why you need to explicitly choose the second item via "[2]".
CG
Christian Grünin reply to Philippe Rathé, 7 Jun 2012, 19:487 Jun 2012 17:57 #
Quoted text · Philippe Rathé · 1 line
> Does well-formed XML can be invalid XML?
The Wikipedia gives you some more information on "well-formed" and "valid" XML documents [1]. Documents are "valid" if they are successfully matched against a schema (usually DTD, XML Schema or RelaxNG).
Quoted text · Philippe Rathé · 2 lines
> I am confused. If XQuery requires well-formed (or valid) XML, how can > it deals with the invalid XML result of http:send-request?
http:send-request() returns a sequence of two results, each of which is completely well-formed (provided that your result is XML). What you see on your screen is just a textual "serialized" representation of all results; there is no unique bidirectional mapping (Text ↔ XML) for that. If you want to know more about XQuery and XML, and get more familiar with the wording, you are invited to check out one the available online tutorials, such as [2]. Christian [1] http://en.wikipedia.org/wiki/XML [2] http://www.w3schools.com/xquery/default.asp
PR
Philippe Rathéin reply to Christian Grün, 6 Jun 2012, 12:207 Jun 2012 18:26 #
Christian, thanks. In fact this path expression work in the context of XQuery code.
   http:send-request(...)[2]
But using the CLI with an xml with two elements give me an error $ echo "<a></a><b></b>" | java -cp basex.jar org.basex.BaseX -i - -q".[2]" "1339078766196.xml" (Line 1): No elements allowed after closed root element. I have the error even when I use the -i option with an xml file having two elements. My intention was to save the http:send-request output to a file and process it later with a CLI. Is it the intented behavior of the CLI?
Quoted text · Christian Grün · 10 lines
On Wed, Jun 6, 2012 at 6:20 AM, Christian Grün <christian.gruen at gmail.com> wrote: >> I'm using the HTTP Module[1] for sending request. The response has he >> http:response element which is XML. Then at the end of that element is >> the body of the response. > > http:send-request() will return two items. You can access the second > one (the actual result) with a positional predicate: > > http:send-request(...)[2]
PR
Philippe Rathé7 Jun 2012 19:06 #
Okay, I thought that this error came from XQuery. I did not notice before that XQuery can operate on invalid XML document. Thanks. In fact I can save the response and the body in two different files.
Quoted text · Christian Grün · 44 lines
On Thu, Jun 7, 2012 at 10:50 AM, Christian Grün <christian.gruen at gmail.com> wrote: >> But using the CLI with an xml with two elements give me an error >> >> $ echo "<a></a><b></b>" | java -cp basex.jar org.basex.BaseX -i - -q".[2]" >> "1339078766196.xml" (Line 1): No elements allowed after closed root element. > > This is acatually not the fault of the CLI; the -i flag expects a > valid XML document as input, and "<a></a><b></b>" does not meet these > requirements. It would probably be difficult to extend -i to accept > any kind of input, because there are numerous cases in which XQuery > results are ambiguous. > > What about storing only the actual result of http:send-request in your file? > Christian On Thu, Jun 7, 2012 at 10:26 AM, Philippe Rathé <prathe at gmail.com> wrote: > Christian, > > thanks. In fact this path expression work in the context of XQuery code. > > http:send-request(...)[2] > > But using the CLI with an xml with two elements give me an error > > $ echo "<a></a><b></b>" | java -cp basex.jar org.basex.BaseX -i - -q".[2]" > "1339078766196.xml" (Line 1): No elements allowed after closed root element. > > I have the error even when I use the -i option with an xml file having > two elements. > My intention was to save the http:send-request output to a file and > process it later with a CLI. > > Is it the intented behavior of the CLI? > > On Wed, Jun 6, 2012 at 6:20 AM, Christian Grün > <christian.gruen at gmail.com> wrote: >>> I'm using the HTTP Module[1] for sending request. The response has he >>> http:response element which is XML. Then at the end of that element is >>> the body of the response. >> >> http:send-request() will return two items. You can access the second >> one (the actual result) with a positional predicate: >> >> http:send-request(...)[2]
PR
Philippe Rathéin reply to Christian Grün, 7 Jun 2012, 17:117 Jun 2012 19:48 #
Quoted text · Christian Grün · 10 lines
On Thu, Jun 7, 2012 at 11:11 AM, Christian Grün <christian.gruen at gmail.com> wrote: >> Okay, I thought that this error came from XQuery. I did not notice >> before that XQuery can operate on invalid XML document. > > A minor note: XQuery definitely requires well-formed XML as input, but > the http:send-request() function generates two results: the first one > represents the XML response element, the second one represents the > actual result – which is the reason why you need to explicitly choose > the second item via "[2]".
Does well-formed XML can be invalid XML? I am confused. If XQuery requires well-formed (or valid) XML, how can it deals with the invalid XML result of http:send-request?
PR
Philippe Rathéin reply to Christian Grün, 7 Jun 2012, 17:577 Jun 2012 20:19 #
Christian, thanks for taking your time for some conversations not related to BaseX. I'm hoping to be able to help too in the future.
Quoted text · Christian Grün · 26 lines
On Thu, Jun 7, 2012 at 11:57 AM, Christian Grün <christian.gruen at gmail.com> wrote: >> Does well-formed XML can be invalid XML? > > The Wikipedia gives you some more information on "well-formed" and > "valid" XML documents [1]. Documents are "valid" if they are > successfully matched against a schema (usually DTD, XML Schema or > RelaxNG). > >> I am confused. If XQuery requires well-formed (or valid) XML, how can >> it deals with the invalid XML result of http:send-request? > > http:send-request() returns a sequence of two results, each of which > is completely well-formed (provided that your result is XML). What you > see on your screen is just a textual "serialized" representation of > all results; there is no unique bidirectional mapping (Text ↔ XML) for > that. > > If you want to know more about XQuery and XML, and get more familiar > with the wording, you are invited to check out one the available > online tutorials, such as [2]. > > Christian > > [1] http://en.wikipedia.org/wiki/XML > [2] http://www.w3schools.com/xquery/default.asp

⚡Generated with XQuery