Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Parsing a generic data file

Reply
Thread Tools

Parsing a generic data file

 
 
dnovatchev@gmail.com
Guest
Posts: n/a
 
      12-17-2007
> The question arises as to whether the output XML should represent the data
> that would be available in the set of generated objects had the JSON been
> eval'd?
>
> Perhaps the Grades section should look like this:-
>
> <Grades>
> <Test>
> <grade>C</grade>
> <points>2</points>
> </Test>
> <Test>
> <grade>A</grade>
> <points>72</points>
> </Test>
> <Test>
> <grade>A</grade>
> <points>65</points>
> </Test>
> </Grades>
>
> since only this data would appear in the an eval of the JSON?
>


The answer is clearly: No.

It is the definition of JSON (and the convertors from XML to JSON use
this) that a sequence of repeating xml elements with the same name are
represented as an ARRAY in JSON.

We don't care what an JScript interpreter would do with the data, but
we must implement a truthful and lossless conversion. Not producing
all <test /> and <grade /> elements results in data loss.


Cheers,
Dimitre Novatchev



On Dec 16, 8:10 am, "Anthony Jones" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
>
>
> > The FXSL library has a json-document() function (written entirely in
> > XSLT
> > 2.0 and using the FXSL's LR parsing framework (also written entirely
> > in XSLT
> > 2.0) ).

>
> > When this transformation:

>
> > <xsl:stylesheet version="2.0"
> > xmlnssl="http://www.w3.org/1999/XSL/Transform"
> > xmlnss="http://www.w3.org/2001/XMLSchema"
> > xmlns:f="http://fxsl.sf.net/"
> > exclude-result-prefixes="f xs"

>
> > <xsl:import href="../f/func-json-document.xsl"/>

>
> > <xslutput omit-xml-declaration="yes" indent="yes"/>

>
> > <xsl:variable name="vstrParam" as="xs:string">
> > {

>
> > "teacher":{
> > "name":
> > "Mr Borat",
> > "age":
> > "35",
> > "Nationality":
> > "Kazakhstan"
> > },

>
> > "Class":{
> > "Semester":
> > "Summer",
> > "Room":
> > "null",
> > "Subject":
> > "Politics",
> > "Notes":
> > "We're happy, you happy?"
> > },

>
> > "Students":
> > [
> > {
> > "Smith":
> > [{"First_Name":"Mary","sex":"Female"}],
> > "Brown":
> > [{"First_Name":"John","sex":"Male"}],
> > "Jackson":
> > [{"First_Name":"Jackie","sex":"Female"}]
> > }
> > ],

>
> > "Grades":
> > [
> > {
> > "Test":
> > [{"grade":"A","points":68},{"grade":"B","points":25 },
> > {"grade":"C","points":15}],
> > "Test":
> > [{"grade":"C","points":2},{"grade":"B","points":29} ,
> > {"grade":"A","points":55}],
> > "Test":
> > [{"grade":"C","points":2},{"grade":"A","points":72} ,
> > {"grade":"A","points":65}]
> > }
> > ]

>
> > }
> > </xsl:variable>

>
> > <xsl:template match="/">
> > <xsl:sequence select="f:json-document($vstrParam)"/>
> > </xsl:template>
> > </xsl:stylesheet>

>
> > is applied (containing essentially your original data, with "First
> > Name"
> > changed to "First_Name", and null changed to "null

>
> > the following result is produced:

>
> > <teacher>
> > <name>Mr Borat</name>
> > <age>35</age>
> > <Nationality>Kazakhstan</Nationality>
> > </teacher>
> > <Class>
> > <Semester>Summer</Semester>
> > <Room>null</Room>
> > <Subject>Politics</Subject>
> > <Notes>We're happy, you happy?</Notes>
> > </Class>
> > <Students>
> > <Smith>
> > <First_Name>Mary</First_Name>
> > <sex>Female</sex>
> > </Smith>
> > <Brown>
> > <First_Name>John</First_Name>
> > <sex>Male</sex>
> > </Brown>
> > <Jackson>
> > <First_Name>Jackie</First_Name>
> > <sex>Female</sex>
> > </Jackson>
> > </Students>
> > <Grades>
> > <Test>
> > <grade>A</grade>
> > <points>68</points>
> > </Test>
> > <Test>
> > <grade>B</grade>
> > <points>25</points>
> > </Test>
> > <Test>
> > <grade>C</grade>
> > <points>15</points>
> > </Test>
> > <Test>
> > <grade>C</grade>
> > <points>2</points>
> > </Test>
> > <Test>
> > <grade>B</grade>
> > <points>29</points>
> > </Test>
> > <Test>
> > <grade>A</grade>
> > <points>55</points>
> > </Test>
> > <Test>
> > <grade>C</grade>
> > <points>2</points>
> > </Test>
> > <Test>
> > <grade>A</grade>
> > <points>72</points>
> > </Test>
> > <Test>
> > <grade>A</grade>
> > <points>65</points>
> > </Test>
> > </Grades>

>
> > One can use json-document() in any XPath expressions, for example,
> > getting
> > all female students is as easy as:

>
> > f:json-document($vstrParam)/Students/*[sex = 'Female']

>
> > and produces:

>
> > <Smith>
> > <First_Name>Mary</First_Name>
> > <sex>Female</sex>
> > </Smith>
> > <Jackson>
> > <First_Name>Jackie</First_Name>
> > <sex>Female</sex>
> > </Jackson>

>
> > I will fix the implementation of json-document() to replace whitespace
> > in
> > element names with underscores and to process the unquoted string
> > null.

>
> --
> Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -
>
> - Show quoted text -


 
Reply With Quote
 
 
 
 
dnovatchev@gmail.com
Guest
Posts: n/a
 
      12-17-2007
I also think that a more appropriate JSON representation than:

"Grades":
[
{
"Test":
[{"grade":"A","points":68},{"grade":"B","points":25 },
{"grade":"C","points":15}],
"Test":
[{"grade":"C","points":2},{"grade":"B","points":29} ,
{"grade":"A","points":55}],
"Test":
[{"grade":"C","points":2},{"grade":"A","points":72} ,
{"grade":"A","points":65}]
}
]


should have been:

"Grades":

{
"Test":
[
{"grade":"A","points":68,"grade":"B","points":
25,"grade":"C","points":15},

{"grade":"C","points":2, "grade":"B","points":29,
"grade":"A","points":55},

{"grade":"C","points":2, "grade":"A","points":72,
"grade":"A","points":65}
]
}

Also, instead of:

"Students":
[
{
"Smith":
[{"First Name":"Mary","sex":"Female"}],
"Brown":
[{"1First Name":"John","sex":"Male"}],
"Jackson":
[{"2First Name":"Jackie","sex":"Female"}]
}
],

it is better to have just:

"Students":
{
"Smith":
{"First Name":"Mary","sex":"Female"},
"Brown":
{"1First Name":"John","sex":"Male"},
"Jackson":
{"2First Name":"Jackie","sex":"Female"}
}
,


Maybe, the original data was produced by a faulty XML --> JSON
convertor.

BTW, I have updated the FXSL CVS with the newest f:json-document(),
which correctly produces XML element names from any JSON string.

The correct treatment of null will follow shortly.


Cheers,
Dimitre Novatchev


On Dec 16, 8:10 am, "Anthony Jones" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
>
>
> > The FXSL library has a json-document() function (written entirely in
> > XSLT
> > 2.0 and using the FXSL's LR parsing framework (also written entirely
> > in XSLT
> > 2.0) ).

>
> > When this transformation:

>
> > <xsl:stylesheet version="2.0"
> > xmlnssl="http://www.w3.org/1999/XSL/Transform"
> > xmlnss="http://www.w3.org/2001/XMLSchema"
> > xmlns:f="http://fxsl.sf.net/"
> > exclude-result-prefixes="f xs"

>
> > <xsl:import href="../f/func-json-document.xsl"/>

>
> > <xslutput omit-xml-declaration="yes" indent="yes"/>

>
> > <xsl:variable name="vstrParam" as="xs:string">
> > {

>
> > "teacher":{
> > "name":
> > "Mr Borat",
> > "age":
> > "35",
> > "Nationality":
> > "Kazakhstan"
> > },

>
> > "Class":{
> > "Semester":
> > "Summer",
> > "Room":
> > "null",
> > "Subject":
> > "Politics",
> > "Notes":
> > "We're happy, you happy?"
> > },

>
> > "Students":
> > [
> > {
> > "Smith":
> > [{"First_Name":"Mary","sex":"Female"}],
> > "Brown":
> > [{"First_Name":"John","sex":"Male"}],
> > "Jackson":
> > [{"First_Name":"Jackie","sex":"Female"}]
> > }
> > ],

>
> > "Grades":
> > [
> > {
> > "Test":
> > [{"grade":"A","points":68},{"grade":"B","points":25 },
> > {"grade":"C","points":15}],
> > "Test":
> > [{"grade":"C","points":2},{"grade":"B","points":29} ,
> > {"grade":"A","points":55}],
> > "Test":
> > [{"grade":"C","points":2},{"grade":"A","points":72} ,
> > {"grade":"A","points":65}]
> > }
> > ]

>
> > }
> > </xsl:variable>

>
> > <xsl:template match="/">
> > <xsl:sequence select="f:json-document($vstrParam)"/>
> > </xsl:template>
> > </xsl:stylesheet>

>
> > is applied (containing essentially your original data, with "First
> > Name"
> > changed to "First_Name", and null changed to "null

>
> > the following result is produced:

>
> > <teacher>
> > <name>Mr Borat</name>
> > <age>35</age>
> > <Nationality>Kazakhstan</Nationality>
> > </teacher>
> > <Class>
> > <Semester>Summer</Semester>
> > <Room>null</Room>
> > <Subject>Politics</Subject>
> > <Notes>We're happy, you happy?</Notes>
> > </Class>
> > <Students>
> > <Smith>
> > <First_Name>Mary</First_Name>
> > <sex>Female</sex>
> > </Smith>
> > <Brown>
> > <First_Name>John</First_Name>
> > <sex>Male</sex>
> > </Brown>
> > <Jackson>
> > <First_Name>Jackie</First_Name>
> > <sex>Female</sex>
> > </Jackson>
> > </Students>
> > <Grades>
> > <Test>
> > <grade>A</grade>
> > <points>68</points>
> > </Test>
> > <Test>
> > <grade>B</grade>
> > <points>25</points>
> > </Test>
> > <Test>
> > <grade>C</grade>
> > <points>15</points>
> > </Test>
> > <Test>
> > <grade>C</grade>
> > <points>2</points>
> > </Test>
> > <Test>
> > <grade>B</grade>
> > <points>29</points>
> > </Test>
> > <Test>
> > <grade>A</grade>
> > <points>55</points>
> > </Test>
> > <Test>
> > <grade>C</grade>
> > <points>2</points>
> > </Test>
> > <Test>
> > <grade>A</grade>
> > <points>72</points>
> > </Test>
> > <Test>
> > <grade>A</grade>
> > <points>65</points>
> > </Test>
> > </Grades>

>
> > One can use json-document() in any XPath expressions, for example,
> > getting
> > all female students is as easy as:

>
> > f:json-document($vstrParam)/Students/*[sex = 'Female']

>
> > and produces:

>
> > <Smith>
> > <First_Name>Mary</First_Name>
> > <sex>Female</sex>
> > </Smith>
> > <Jackson>
> > <First_Name>Jackie</First_Name>
> > <sex>Female</sex>
> > </Jackson>

>
> > I will fix the implementation of json-document() to replace whitespace
> > in
> > element names with underscores and to process the unquoted string
> > null.

>
> The question arises as to whether the output XML should represent the data
> that would be available in the set of generated objects had the JSON been
> eval'd?
>
> Perhaps the Grades section should look like this:-
>
> <Grades>
> <Test>
> <grade>C</grade>
> <points>2</points>
> </Test>
> <Test>
> <grade>A</grade>
> <points>72</points>
> </Test>
> <Test>
> <grade>A</grade>
> <points>65</points>
> </Test>
> </Grades>
>
> since only this data would appear in the an eval of the JSON?
>
> --
> Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -
>
> - Show quoted text -


 
Reply With Quote
 
 
 
 
Anthony Jones
Guest
Posts: n/a
 
      12-22-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> > The question arises as to whether the output XML should represent the

data
> > that would be available in the set of generated objects had the JSON

been
> > eval'd?
> >
> > Perhaps the Grades section should look like this:-
> >
> > <Grades>
> > <Test>
> > <grade>C</grade>
> > <points>2</points>
> > </Test>
> > <Test>
> > <grade>A</grade>
> > <points>72</points>
> > </Test>
> > <Test>
> > <grade>A</grade>
> > <points>65</points>
> > </Test>
> > </Grades>
> >
> > since only this data would appear in the an eval of the JSON?
> >

>
> The answer is clearly: No.
>


Oh, I thought the raison d'Ítre behind JSON was that a data structure could
be serialised to a string that could be passed to Javascript and
re-assembled easily by using the Eval statement.

> It is the definition of JSON (and the convertors from XML to JSON use
> this) that a sequence of repeating xml elements with the same name are
> represented as an ARRAY in JSON.


Is there a spec? Where does it say that?

>
> We don't care what an JScript interpreter would do with the data, but
> we must implement a truthful and lossless conversion. Not producing
> all <test /> and <grade /> elements results in data loss.
>


Agreed. I'm willing to be shown wrong on this but if you're right than JSON
is bust and pointless.

--
Anthony Jones - MVP ASP/ASP.NET


 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      12-22-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I also think that a more appropriate JSON representation than:
>
> "Grades":
> [
> {
> "Test":
> [{"grade":"A","points":68},{"grade":"B","points":25 },
> {"grade":"C","points":15}],
> "Test":
> [{"grade":"C","points":2},{"grade":"B","points":29} ,
> {"grade":"A","points":55}],
> "Test":
> [{"grade":"C","points":2},{"grade":"A","points":72} ,
> {"grade":"A","points":65}]
> }
> ]
>
>
> should have been:
>
> "Grades":
>
> {
> "Test":
> [
> {"grade":"A","points":68,"grade":"B","points":
> 25,"grade":"C","points":15},
>
> {"grade":"C","points":2, "grade":"B","points":29,
> "grade":"A","points":55},
>
> {"grade":"C","points":2, "grade":"A","points":72,
> "grade":"A","points":65}
> ]
> }
>


We're just guessing at the intent but that appears to be an object called
Grades that contains just one member an array called Test containing what
appears to be grades required to pass each test. Seems a little convoluted
and how is each test identified? Ordinal position?

> Also, instead of:
>
> "Students":
> [
> {
> "Smith":
> [{"First Name":"Mary","sex":"Female"}],
> "Brown":
> [{"1First Name":"John","sex":"Male"}],
> "Jackson":
> [{"2First Name":"Jackie","sex":"Female"}]
> }
> ],
>
> it is better to have just:
>
> "Students":
> {
> "Smith":
> {"First Name":"Mary","sex":"Female"},
> "Brown":
> {"1First Name":"John","sex":"Male"},
> "Jackson":
> {"2First Name":"Jackie","sex":"Female"}
> }
> ,


And if you have two students with the last name Smith? Smith magically
becomes an array?

>Maybe, the original data was produced by a faulty XML --> JSON
>convertor.


Its difficult to make sense of what appears to be faulty both as JSON and as
a logical structure.

--
Anthony Jones - MVP ASP/ASP.NET


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Parsing generic XML Roedy Green Java 1 06-11-2008 03:28 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
Generic Parsing Library Adam Sanderson Ruby 8 08-19-2005 05:47 PM



Advertisments