简体   繁体   中英

How to Represent collection of alternatives in JSON-LD

I have properties in my model which have value as alternate array (which indicates a collection of alternates from which only one value is to be chosen). Earlier I was using RDF/XML to do this using rdf:Alt. See the following example

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ex="http://ns.example.com/example/">
<rdf:Description>
    <ex:prop1>
        <rdf:Alt>
            <rdf:li>100</rdf:li>
            <rdf:li>120</rdf:li>
            <rdf:li>130</rdf:li>
        </rdf:Alt>
    </ex:prop1>
</rdf:Description>
</rdf:RDF>

But now I want to do same thing in JSON-LD. I tried converting the above snippet to JSON-LD by online converter and got the following result

{
  "@context": {
    "ex": "http://ns.example.com/example/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  },
  "@graph": [
    {
      "@id": "_:g70327238021300",
      "ex:prop1": {
        "@id": "_:g70327280101680"
      }
    },
    {
      "@id": "_:g70327280101680",
      "@type": "rdf:Alt",
      "rdf:_1": "100",
      "rdf:_2": "120",
      "rdf:_3": "130"
    }
  ]
}

Actually I found out that rdf:Alt/Seq/Bag are marked as archaic by w3c. There is @list and @set for ordered and unordered arrays respectively in JSON-LD. So is there any other way to do this in JSON-LD without using "rdf:Alt" as @type?

The rdf:li expansion in to rdf:_n is a feature of RDF/XML, which is really the only format to provide native support for rdf:Alt/Bag/Seq . As you note, these are considered archaic, so don't look for any native support in other serializations, including JSON-LD.

For other ways of collecting information, other than RDF Collections, which have semantic support in RDF Concepts, and are directly supported by most RDF serializations, look to ontologies such as the Ordered List Ontology , which shows how you might do something similar to rdf:Alt semantics using semantics, rather than syntax.

For example, schema.org has a Choose Action , which seems like it is similar to what you want to do. The related Vote Action is also similar.

RDFS

In RDFS, there exist RDF Containers ( RDF:Container ) and RDF Collections ( rdf:List ). The difference is that containers are open , whereas collections are closed , see also this question .

There are three kinds of rdf:Container : rdf:Bag , rdf:Seq and rdf:Alt .

There is not formal (ie semantic ) difference between these kind of containers, the difference is rather pragmatic . The difference is in what the consumer is intended to do with the data, see this question .

Strictly speaking, both RDF Collections and RDF Containers are not parts of RDF data model, but rather elements of a particular RDF vocabulary (though this vocabulary is very common).

JSON-LD

JSON-LD data model is not well aligned with RDF, see eg this article of one of the primary creators.

  • JSON-LD ignores the difference between open and closed .
  • JSON-LD ignores the aforementioned pragmatic differences.
  • JSON-LD keeps the difference between ordered and unordered .
  • JSON-LD keeps the difference between (lexically) non-distinct and distinct ,
    identifying this difference with the previous one.

Mapping

From RDFS 1.1:

The same resource may appear in a container more than once .

The first member of the container, ie the value of the rdf:_1 property, is the default choice .

Thus, in general, elements of rdf:Alt are not unique, but ordered. Hence, one should use @list . However, if your alternatives are unique and unordered, you could use @set .

In other cases, be aware that there is not any harm in asserting an order when there isn't any.

See also ISSUE-24 for discussion and motivation.

UPDATE

Yes, it is impossible to express pragmatic (ie related to language pragmatics) differences in the JSON-LD data model. For instance, it is impossible to express the difference between rdf:Seq and rdf:Alt . If you want to express these differences, you need a vocabulary.

RDFS is a kind of such vocabulary. Use JSON-LD as a serialization format for RDF abstract syntax and write "@type": "rdf:Alt" etc. as you previously did.

Probably, you are confused by the abundance of surrogate @id 's in your JSON-LD. Just do not use blank nodes in your RDF, then JSON-LD will look like this:

{
  "@context": {
    "ex": "http://example.com/example/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  },
  "@graph": [
    {
      "@id": "ex:object1",
      "ex:availableOptions": {
        "@id": "ex:optionsFor1"
      }
    },
    {
      "@id": "ex:optionsFor1",
      "@type": "rdf:Alt",
      "rdf:_1": "100",
      "rdf:_2": "120",
      "rdf:_3": "130"
    }
  ]
}

Another option is to use another vocabulary, eg schema.org . I'm not sure that this example is correct:

{
  "@context": {"schema": "http://schema.org/",
               "adobe" : "http://ns.adobe.com/xap/1.0/smp/"},
  "@type": "schema:ChooseAction",             
  "@id": "adobe:price",
  "schema:option": ["100", "120", "130"]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM