3

can we add XML Schema in cosmos Db? if yes how can we query them? i am able to save XML data as string but how to query them?

below is my document in the collection:

    {
    "id": "4796300e-b1d3-4c60-abc3-9ac28fc87d45",
    "BId": "55ce95c2-b7f6-4c9b-a288-2363881f14bb",
    "SheetName": "<root><Tax_Type>tax</Tax_Type><Buildings_Limit /><Buildings_Value /><Contents_Limit /><Contents_Value /><Number_of_Stories /><Other_Limit /><Other_Value /><PremiumValue /><Roof_Geometry /><IsSoft_Story /><Sprinklers /><Tax>0</Tax><Tax_Percent>0</Tax_Percent><Year_Built /><Construction_Scheme /><Construction_Code /><Construction_Description /><Occupancy_Scheme /><Occupancy_Code /><Occupancy_Description /></root>",
    "HeaderIndex": "1",
    "_rid": "xxxxxxx",
    "_self": "dbs/xxx==/colls/xxx=/docs/xxx==/",
    "_etag": "\"xxxxx\"",
    "_attachments": "attachments/",
    "_ts": 1509170527
}
ABaig
  • 99
  • 1
  • 10

3 Answers3

6

Cosmos DB (which has apparently superseded Document DB) development team deliberately chose to not support XML. Their Schema-Agnostic Indexing Whitepaper sheds some light on the reasoning behind the choice:

The schema of a document describes the structure and the type system of the document independent of the document instance.For example, the XML Schema specification provides the language for representing schemas for XML documents. Unlike XML, no such widely adopted schema standard exists for JSON. In contrast to XML, JSON’s type system is a strict subset of the type systems of many modern programming languages, most notably JavaScript. The simplicity of the JSON grammar is one the reasons for its ubiquitous adoption despite the lack of a schema specification. With a goal to eliminate the impedance mismatch between the database and the application programming models, DocumentDB exploits the simplicity of JSON and its lack of a schema specification. It makes no assumptions about the documents and allows documents within a DocumentDB collection to vary in schema, in addition to the instance specific values. In contrast to other document databases, DocumentDB’s database engine operates directly at the level of JSON grammar, remaining agnostic to the concept of a document schema

Then the engine provides you with a bunch of APIs to query your data: SQL, Cassandra, Mongo, Gremlin, Table and Etcd (the list likely to be expanded in future).

The bad news is you can't just chuck XML into it and call it a day - there's no API that will let you do it.

Good news is you can convert your XML to JSON and back. Once you've got your JSON, there's heaps of client libraries to chose from (and again, the list is likely to grow over time).

Finding specific examples how to convert XML to JSON on StackOverflow is easy, for exmaple:

  1. Javascript example
  2. Or C# if you feel so inclined

Coming back to your specific document you could have converted the string into sub-object like so:

{
"id": "4796300e-b1d3-4c60-abc3-9ac28fc87d45",
"BId": "55ce95c2-b7f6-4c9b-a288-2363881f14bb",
"SheetName": {
    "Tax_Type": "tax",
    "Buildings_Limit": {},
    "Buildings_Value": {},
    "Contents_Limit": {},
    "Contents_Value": {},
    "Number_of_Stories": {},
    "Other_Limit": {},
    "Other_Value": {},
    "PremiumValue": {},
    "Roof_Geometry": {},
    "IsSoft_Story": {},
    "Sprinklers": {},
    "Tax": "0",
    "Tax_Percent": "0",
    "Year_Built": {},
    "Construction_Scheme": {},
    "Construction_Code": {},
    "Construction_Description": {},
    "Occupancy_Scheme": {},
    "Occupancy_Code": {},
    "Occupancy_Description": {}
},
"HeaderIndex": "1",
"_rid": "xxxxxxx",
"_self": "dbs/xxx==/colls/xxx=/docs/xxx==/",
"_etag": "\"xxxxx\"",
"_attachments": "attachments/",
"_ts": 1509170527
}
timur
  • 14,239
  • 2
  • 11
  • 32
1

It is better to save JSON document in CosmosDB and it is easy to query them. In place of saving a one long string of XML, I will suggest to convert it into JSON and save it as a document. check this link to learn how to query JSON ...

Rafat Sarosh
  • 989
  • 7
  • 16
1

CosmosDB is a NOSQL database and the simple answer is NO. It stores the data in JSON documents: https://learn.microsoft.com/en-us/azure/cosmos-db/introduction

Even though it supports mainly 4 APIS, Depending the chosen API, you can handle these JSON documents in graph model (Gremlin), or in document collection (MongoDb, DocumentDB). Or SQL etc. But the result is always a JSON document.

But there are a lot of tools to convert XML to JSON and convert back.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396