Database Reference
In-Depth Information
• The root element is absorbed:
<root>A</root>
becomes
"A"
.
• Attributes are serialized as properties, with the attribute name and its value.
• An element with a single text child becomes a property whose value is the text
child:
<e>text</e>
becomes
{"e": "text"}
.
• Sibling elements with the same name within a parent element are added to an
array:
<A><b>1</b><b>2</b></A>
becomes
{ "b" : ["1", "2"] }
.
• In mixed-content nodes, text nodes are dropped.
• If an element has attribute and text content, the text content becomes a property:
<A a="b">1</A>
becomes
{ "A" : { "a" : "b", "#text" : "1" } }
.
• An empty element becomes
null
:
<e/>
becomes
{"e": null}
.
• An element with name
<json:value>
is serialized as a simple value, not an
object:
<json:value>my-value</json:value>
becomes
"my-value"
.
Sometimes it is necessary to ensure that a certain property is serialized as an array,
even if there's only one corresponding element in the XML input, you can use the
attribute
json:array="true|false"
for this.
By default, all values are strings. If you want to output a literal value—for example, to
serialize a number—use the attribute
json:literal="true"
.
The JSON prefix
json
should be bound to the namespace
http://www.json.org
. As
an example, here is some XML:
<Root
xmlns:json=
"http://www.json.org"
>
<Items>
<Item
id=
"1"
>
Bananas
</Item>
<Item>
CPU motherboards
</Item>
</Items>
<Items
>
<Item
json:array=
"yes"
>
Bricks
</Item>
</Items>
<Mixed>
This is
<i>
mixed
</i>
content
</Mixed>
<Empty/>
<Literal
json:literal=
"yes"
>
1
</Literal>
</Root>
And here is its JSON serialization:
{
"Items"
:
[{
"Item"
:
[{
"id"
:
"1"
,
"#text"
:
"Bananas"
},
"CPU motherboards"
]
},
{
"Item"
:
[
"Bricks"
]
}],
"Mixed"
:
{
"i"
:
"mixed"
},
"Empty"
:
null
,
"Literal"
:
1
}
In addition to the JSON serializer in eXist, which attempts to convert XML into
JSON with as little effort from the developer as possible, there are three other XQuery
modules that enable you to work with JSON. The first two modules—JSON XQuery
Search WWH ::
Custom Search