Search code examples
pythonpython-3.xschemardfrdflib

How do I describe a temperature with min/max values in RDFlib with schema.org?


I'm using schema.org to create a RDF/XML file using RDFlib in Python, and am trying to nest elements into a PropertyValue element like so, but it's not working...

g.add((p, n.PropertyValue, (p, n.minValue, Literal(130.15))))

I'm trying to end up with this result...

<schema:PropertyValue>
    <schema:maxValue rdf:datatype="http://www.w3.org/2001/XMLSchema#double">308.0</schema:maxValue>
    <schema:name>Temperature</schema:name>
    <schema:minValue rdf:datatype="http://www.w3.org/2001/XMLSchema#double">130.15</schema:minValue>
</schema:PropertyValue>

How could I do this in RDFlib? Thanks in advance.


Solution

  • As per your data, a Blank Node, [ ], with a name, minValue & maxValue property:

    [] a <https://schema.org/PropertyValue> ;
        schema:name "Temperature" ;
        schema:minValue "130.15"^^xsd:double ;
        schema:maxValue "308.0"^^xsd:double ;
    

    Better, use a URI for the PropertyValue thing:

    <http://example.org/propval/1>
        a <https://schema.org/PropertyValue> ;
        schema:name "Temperature" ;
        schema:minValue "130.15"^^xsd:double ;
        schema:maxValue "308.0"^^xsd:double ;
    

    Use this RDFlib Python:

    SDO = Namespace("https://schema.org/")
    g.bind("schema", SDO)
    
    pv = URIRef("http://example.org/propval/1")
    g.add((pv, RDF.type, SDO.PropertyValue))
    g.add((pv, SDO.name, Literal("Temperature")))
    g.add((pv, SDO.minValue, Literal("130.15", datatype=XSD.double)))
    g.add((pv, SDO.maxValue, Literal("308.0", datatype=XSD.double)))