Christian,
Thanks for the help. This works, but as with the other method. It affects the entire script which is not desired. I just need the call to fn:serialize to create json. No where else.
Once I set this I have to adjust a lot of the methods i'm using to accept json instead of xml since thats what 90% of our stuff works with. Can you change the output mid script? The output is xml, i just need the json for mapping an http request. For example:
declare option db:parser "html";
declare option db:htmlopt "html=true";
for $product in doc("http://www.telvent-gis.com/support/versions.shtml")//div[@id='contenedor']/div[@id] return
let $request :=
<http:request href='http://localhost:8080/api/core/v3/contents'
method='post' username='admin' password='admin' send-authorization='true'>
<http:body media-type="application/json">
{
json:serialize(
<json objects="json">
<subject>Test Subject 22</subject>
<content type="object">
<type>text/html</type>
<text>{fn:serialize($product/*)}</text>
</content>
<type>document</type>
</json>) }
</http:body>
</http:request> return
try { http:send-request($request) }
catch HC0001 { 'Not Found' }
Thanks again for spending time on this. I might need to look into some other method. :(
- James
> From: christian.gruen@gmail.com
> Date: Fri, 2 Aug 2013 18:01:13 +0200
> Subject: Re: [basex-talk] Question around JSON Serialization
> To: james.jw@hotmail.com
> CC: basex-talk@mailman.uni-konstanz.de
>
> I have attached a possible solution; don’t hesitate to tell us if you
> expect a different solution.
>
> Best,
> Christian
> ___________________________
>
> declare option output:method 'json';
>
> let $htmlContent := <div><h2>Issue:</h2></div>
> return
> <json objects="json">
> <subject>Test Subject 10</subject>
> <content type="object">
> <type>text/html</type>
> <text>{ fn:serialize($htmlContent) }</text>
> </content>
> <type>document</type>
> </json>
> ___________________________
>
> 2013/8/2 James Wright <james.jw@hotmail.com>:
> > Christian,
> >
> > Not a problem. Here is a script that perfectly demonstrates my issue and
> > should run on your end:
> >
> > declare variable $serializationParameters :=
> > <output:serialization-parameters
> > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization"
> > xmlns="http://example.org/ext">
> > <output:method value="text"/>
> > </output:serialization-parameters>;
> >
> > let $htmlContent := <div><h2>Issue:</h2></div>
> > return json:serialize(
> > <json objects="json">
> > <subject>Test Subject 10</subject>
> > <content type="object">
> > <type>text/html</type>
> > <text>{fn:serialize($htmlContent, $serializationParameters
> > )}</text>
> > </content>
> > <type>document</type>
> > </json> )
> >
> > This returns:
> >
> > {
> > "subject": "Test Subject 10",
> > "content": {
> > "type": "text\/html",
> > "text": "Issue:"
> > },
> > "type": "document"
> > }
> >
> > However we need it to return:
> >
> > {
> > "subject": "Test Subject 10",
> > "content": {
> > "type": "text\/html",
> > "text": "<div><h2>Issue:<\/h2><\/div>"
> > },
> > "type": "document"
> > }
> >
> > Thanks,
> > James
> >
> >> From: christian.gruen@gmail.com
> >> Date: Fri, 2 Aug 2013 17:32:11 +0200
> >
> >> Subject: Re: [basex-talk] Question around JSON Serialization
> >> To: james.jw@hotmail.com
> >> CC: basex-talk@mailman.uni-konstanz.de
> >>
> >> Hi James,
> >>
> >> could you please have another look at your example and provide us with
> >> some code that runs out of the box?
> >>
> >> Thanks in advance,
> >> Christian
> >> ___________________________
> >>
> >> 2013/8/2 James Wright <james.jw@hotmail.com>:
> >> > Hey Christian,
> >> >
> >> > Thank definitely did the trick however it affects the entire query. The
> >> > query should output xml of the api call requests. I tried adding
> >> > serialization parameters to the single fn:serialize method and set the
> >> > output to 'text' however now it only returns the 'text' inside of the
> >> > elements and not the xml. The script as a whole is supposed to return
> >> > xml
> >> > with results. The only json part is for the API calls. Here is a more
> >> > full
> >> > script:
> >> >
> >> > declare option db:parser "html";
> >> > declare option db:htmlopt "html=true";
> >> > declare variable $js := '{';
> >> > declare variable $je := '}';
> >> > let $serializationParameters :=
> >> > <output:serialization-parameters
> >> >
> >> > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization"
> >> > xmlns="http://example.org/ext">
> >> > <output:method value="text"/>
> >> >
> >> > <results>
> >> > for $productInfo in
> >> >
> >> > doc("http://www.someUrl.com/support/versions.shtml")//div[@id='contenedor']/div[@id]
> >> > return
> >> > let $request :=
> >> > <http:request
> >> > href='http://www.someExternalService.com/api/core/v3/contents'
> >> > method='post' username='admin' password='admin'
> >> > send-authorization='true'>
> >> > <http:body media-type="application/json">
> >> > {json:serialize(
> >> > <json objects="json">
> >> > <subject>$productInfo/name</subject>
> >> > <content type="object">
> >> > <type>text/html</type>
> >> > <text>{fn:serialize($productInfo/* ,
> >> > $serializationParameters )}</text>
> >> > </content>
> >> > <type>document</type>
> >> > </json> )}
> >> > </http:body>
> >> > </http:request> return
> >> > <result>
> >> > try {http:send-request($request)[2]}
> >> > catch HC0001 {'Not Found'}
> >> > </result>
> >> > </results>
> >> >
> >> > I guess I would expect this to operate as you described since I set the
> >> > output:method to 'text'. Let me know if you have any ideas? I have read
> >> > that
> >> > serialization page several times and I guess I'm just not understanding
> >> > this
> >> > piece.
> >> >
> >> > Thanks again.
> >> > - James
> >> >
> >> >> From: christian.gruen@gmail.com
> >> >> Date: Fri, 2 Aug 2013 09:28:48 +0200
> >> >> Subject: Re: [basex-talk] Question around JSON Serialization
> >> >> To: james.jw@hotmail.com
> >> >> CC: basex-talk@mailman.uni-konstanz.de
> >> >
> >> >>
> >> >> Hi James,
> >> >>
> >> >> by default, all string results are XML-encoded. You’ll get the
> >> >> expected result by declaring "text" as output option in the prolog
> >> >> (header) of your query:
> >> >>
> >> >> declare option output:method 'text';
> >> >> let $someItem := ...
> >> >>
> >> >> Our Wiki page on serialization may give you more hints [1].
> >> >>
> >> >> Hope this helps,
> >> >> Christian
> >> >>
> >> >> [1] http://docs.basex.org/wiki/Serialization
> >> >> ___________________________
> >> >>
> >> >> 2013/8/2 James Wright <james.jw@hotmail.com>:
> >> >> > Hey BaseX Team,
> >> >> >
> >> >> > Im trying to serialize some JSON in order to make a few calls to an
> >> >> > external
> >> >> > api. In the json I need to include 'html' for example:
> >> >> >
> >> >> > {
> >> >> > "subject": "Test Subject",
> >> >> > "content": {
> >> >> > type: "text/html",
> >> >> > text: "<div><h2>Issue:</h2></div>
> >> >> > },
> >> >> > "type": "document"
> >> >> > }
> >> >> >
> >> >> > When I call the following script however I get:
> >> >> >
> >> >> > {
> >> >> > "subject": "Test Subject 10",
> >> >> > "content": {
> >> >> > "type": "text\/html",
> >> >> > "text": "<html>\n <div>\n <h2>Issue:<\/h2>\n
> >> >> > <\/div>\n<\/html>"
> >> >> > },
> >> >> > "type": "document"
> >> >> > }
> >> >> >
> >> >> > I tried messing with the serialization parameters however with no
> >> >> > success. I
> >> >> > use the resulting string as the content for a http post request. Any
> >> >> > help
> >> >> > would be appreciated.
> >> >> >
> >> >> > Script:
> >> >> > let $htmlContent := <div><h2>Issue:</h2></div>
> >> >> > return json:serialize(
> >> >> > <json objects="json">
> >> >> > <subject>Test Subject 10</subject>
> >> >> > <content type="object">
> >> >> > <type>text/html</type>
> >> >> > <text>{fn:serialize(<html>{ $someItem }</html>)}</text>
> >> >> > </content>
> >> >> > <type>document</type>
> >> >> > </json> )
> >> >> >
> >> >> > Thanks,
> >> >> > James
> >> >> >
> >> >> > _______________________________________________
> >> >> > BaseX-Talk mailing list
> >> >> > BaseX-Talk@mailman.uni-konstanz.de
> >> >> > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
> >> >> >