Akbar Kazimov
Akbar Kazimov

Reputation: 37

What is the best mapping.ttl file for converting xml file into RDF by using RML-Mapper library?

I am using RML-Mapper library in order to convert xml file into rdf file. For the following xml file, catalogs.xml, I am having troubles to write the best mapping.ttl. My question is, what is the best mapping.ttl file for that specific xml? I provide catalogs.xml and catalog_mapping.ttl files below...

Catalogs.xml file

<catalog>
    <product description="Cardigan Sweater" product_image="cardigan.jpg">
        <catalog_item gender="Men's">
            <item_number>QWZ5671</item_number>
            <price>39.95</price>
            <size description="Medium">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            </size>
            <size description="Large">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            </size>
        </catalog_item>
        <catalog_item gender="Women's">
            <item_number>RRX9856</item_number>
            <price>42.50</price>
            <size description="Small">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            </size>
            <size description="Medium">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
                <color_swatch image="black_cardigan.jpg">Black</color_swatch>
            </size>
            <size description="Large">
                <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
                <color_swatch image="black_cardigan.jpg">Black</color_swatch>
            </size>
            <size description="Extra Large">
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
                <color_swatch image="black_cardigan.jpg">Black</color_swatch>
            </size>
        </catalog_item>
    </product>
    <product description="Turkish Jeans" product_image="cardigan.jpg">
        <catalog_item gender="Men's">
            <item_number>FDE456</item_number>
            <price>22</price>
            <size description="Small">
                <color_swatch image="red_cardigan.jpg">Blue</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Black</color_swatch>
            </size>
            <size description="Extra Small">
                <color_swatch image="red_cardigan.jpg">White</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Transparent</color_swatch>
            </size>
        </catalog_item>
        <catalog_item gender="Women's">
            <item_number>LKO478</item_number>
            <price>35</price>
            <size description="Small">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="navy_cardigan.jpg">Blue</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Orange</color_swatch>
            </size>
            <size description="Large">
                <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
                <color_swatch image="black_cardigan.jpg">Black</color_swatch>
            </size>
            <size description="Extra Large">
                <color_swatch image="burgundy_cardigan.jpg">Pink</color_swatch>
                <color_swatch image="black_cardigan.jpg">Yellow</color_swatch>
            </size>
        </catalog_item>
    </product>
</catalog>

catalog_mapping.ttl

@prefix rr: <http://www.w3.org/ns/r2rml#>.
@prefix rml: <http://semweb.mmlab.be/ns/rml#>.
@prefix ex: <http://example.com/ns#>.
@prefix ql: <http://semweb.mmlab.be/ns/ql#>.
@prefix transit: <http://vocab.org/transit/terms/>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.


 rr:ProductMapping
    rml:logicalSource [
        rml:source "Catalogs.xml";
        rml:referenceFormulation ql:XPath;
        rml:iterator "/catalog/product"
    ];

    rr:subjectMap [
        rr:template "http://ex.com/{@description}";
        rr:class ex:product
    ];

    rr:predicateObjectMap [
        rr:predicate ex:catalog_item;
            rr:objectMap [
                rr:parentTriplesMap rr:CatalogItemMapping;
                rr:joinCondition [
                    rr:child "/catalog/product/catalog_item";
                    rr:parent "/catalog/product"
                ]
            ]
     ].

 rr:CatalogItemMapping
    rml:logicalSource [
        rml:source "Catalogs.xml";
        rml:referenceFormulation ql:XPath;
        rml:iterator "/catalog/product/catalog_item"
    ];

    rr:subjectMap [
        rr:template "http://ex.com/{@gender}/id={item_number}";
    ];

    rr:predicateObjectMap [
        rr:predicate ex:item_number;
            rr:objectMap [
                rml:reference "item_number"
            ]
     ];

     rr:predicateObjectMap [
             rr:predicate ex:price;
                 rr:objectMap [
                     rml:reference "price"
                 ]
          ];

    rr:predicateObjectMap [
        rr:predicate ex:size_and_color;
        rr:objectMap [
            rr:template "{size/@description}_{size/color_swatch}"
        ]
    ].

This is my current result......


<http://ex.com/Cardigan%20Sweater> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/ns#product> null
<http://ex.com/Turkish%20Jeans> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/ns#product> null
<http://ex.com/Men%27s/id=QWZ5671> <http://example.com/ns#item_number> "QWZ5671" null
<http://ex.com/Men%27s/id=QWZ5671> <http://example.com/ns#price> "39.95" null
<http://ex.com/Men%27s/id=QWZ5671> <http://example.com/ns#size_and_color> <Medium_Red> null
<http://ex.com/Men%27s/id=QWZ5671> <http://example.com/ns#size_and_color> <Large_Red> null
<http://ex.com/Men%27s/id=QWZ5671> <http://example.com/ns#size_and_color> <Medium_Burgundy> null
<http://ex.com/Men%27s/id=QWZ5671> <http://example.com/ns#size_and_color> <Large_Burgundy> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#item_number> "RRX9856" null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#price> "42.50" null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Small_Red> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Medium_Red> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Large_Red> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Extra%20Large_Red> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Small_Navy> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Medium_Navy> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Large_Navy> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Extra%20Large_Navy> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Small_Burgundy> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Medium_Burgundy> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Large_Burgundy> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Extra%20Large_Burgundy> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Small_Black> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Medium_Black> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Large_Black> null
<http://ex.com/Women%27s/id=RRX9856> <http://example.com/ns#size_and_color> <Extra%20Large_Black> null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#item_number> "FDE456" null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#price> "22" null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#size_and_color> <Small_Blue> null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#size_and_color> <Extra%20Small_Blue> null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#size_and_color> <Small_Black> null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#size_and_color> <Extra%20Small_Black> null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#size_and_color> <Small_White> null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#size_and_color> <Extra%20Small_White> null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#size_and_color> <Small_Transparent> null
<http://ex.com/Men%27s/id=FDE456> <http://example.com/ns#size_and_color> <Extra%20Small_Transparent> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#item_number> "LKO478" null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#price> "35" null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Small_Red> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Large_Red> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Extra%20Large_Red> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Small_Blue> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Large_Blue> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Extra%20Large_Blue> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Small_Orange> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Large_Orange> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Extra%20Large_Orange> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Small_Navy> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Large_Navy> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Extra%20Large_Navy> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Small_Black> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Large_Black> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Extra%20Large_Black> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Small_Pink> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Large_Pink> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Extra%20Large_Pink> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Small_Yellow> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Large_Yellow> null
<http://ex.com/Women%27s/id=LKO478> <http://example.com/ns#size_and_color> <Extra%20Large_Yellow> null

Is this result good or is there a better catalog_mapping.ttl that can conclude a better result?

Upvotes: 2

Views: 787

Answers (1)

Dylan Van Assche
Dylan Van Assche

Reputation: 540

The mapping rules are already quite good, I did the following improvements:

  1. rr:<name of TriplesMap> should not be used as prefix for your own TriplesMaps. The rr prefix is defined by the R2RML specification. To solve this, I added @base http://example.org and removed the rr prefix of the TriplesMaps:
@base <http://example.org/> .

<#ProductMapping>
    a rr:TriplesMap;
  1. I added a rr:TriplesMap for each TriplesMap for RML processors to see that the RDF is actually describing a TriplesMap:
<#ProductMapping>
    a rr:TriplesMap;
  1. To make it easier for querying the RDF, I split the ex:size_and_color up into different TripleMaps. This way, colors and sizes get their own IRI which can be used to query only on the size or color of a product. Also note that I used rml:reference instead of rr:template since we only need the value and not create a string template. You can find more information about rml:reference in the RML specification.
rr:predicateObjectMap [
        rr:predicate ex:color;
        rr:objectMap [
            rml:reference "size/color_swatch"
        ]
    ];

    rr:predicateObjectMap [
        rr:predicate ex:size;
        rr:objectMap [
            rml:reference "size/@description"
        ]
    ].

All mapping rules:

@base <http://example.org/> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix transit: <http://vocab.org/transit/terms/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix ex: <http://example.org/ns#> .

<#ProductMapping>
    a rr:TriplesMap;
    rml:logicalSource [
        rml:source "Catalogs.xml";
        rml:referenceFormulation ql:XPath;
        rml:iterator "/catalog/product";
    ];

    rr:subjectMap [
        rr:template "http://example.org/product/{@description}";
        rr:class ex:product;
    ];

    rr:predicateObjectMap [
        rr:predicate ex:catalogItem;
        rr:objectMap [
            rr:parentTriplesMap <#CatalogItemMapping>;
            rr:joinCondition [
                rr:child "@description";
                rr:parent "../@description";
            ];
        ];
    ].

<#CatalogItemMapping>
    a rr:TriplesMap;
    rml:logicalSource [
        rml:source "Catalogs.xml";
        rml:referenceFormulation ql:XPath;
        rml:iterator "/catalog/product/catalog_item";
    ];

    rr:subjectMap [
        rr:template "http://example.org/item?id={item_number}";
    ];

    rr:predicateObjectMap [
        rr:predicate ex:item_number;
        rr:objectMap [
            rml:reference "item_number";
        ];
    ];

    rr:predicateObjectMap [
        rr:predicate ex:price;
        rr:objectMap [
            rml:reference "price";
        ];
    ];

    rr:predicateObjectMap [
        rr:predicate ex:size;
        rr:objectMap [
            rr:parentTriplesMap <#SizeMapping>;
            rr:joinCondition [
                rr:child "@gender";
                rr:parent "../@gender";
            ];
            rr:joinCondition [
                rr:child "item_number";
                rr:parent "../item_number";
            ];
        ];
    ].

<#SizeMapping>
    a rr:TriplesMap;
    rml:logicalSource [
        rml:source "Catalogs.xml";
        rml:referenceFormulation ql:XPath;
        rml:iterator "/catalog/product/catalog_item/size";
    ];

    rr:subjectMap [
        rr:template "http://example.org/item?id={../item_number}&size={@description}";
    ];

    rr:predicateObjectMap [
        rr:predicate ex:color;
        rr:objectMap [
            rr:parentTriplesMap <#ColorMapping>;
            rr:joinCondition [
                rr:child "@description";
                rr:parent "../@description";
            ];
            rr:joinCondition [
                rr:child "../item_number";
                rr:parent "../../item_number";
            ]
        ];
    ].

<#ColorMapping>
    a rr:TriplesMap;
    rml:logicalSource [
        rml:source "Catalogs.xml";
        rml:referenceFormulation ql:XPath;
        rml:iterator "//color_swatch";
    ];

    rr:subjectMap [
        rr:template "http://example.org/color/{.}";
    ].

Output:

<http://example.org/product/Cardigan%20Sweater> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ns#product>.
<http://example.org/product/Cardigan%20Sweater> <http://example.org/ns#catalogItem> <http://example.org/item?id=QWZ5671>.
<http://example.org/product/Cardigan%20Sweater> <http://example.org/ns#catalogItem> <http://example.org/item?id=RRX9856>.
<http://example.org/product/Turkish%20Jeans> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ns#product>.
<http://example.org/product/Turkish%20Jeans> <http://example.org/ns#catalogItem> <http://example.org/item?id=FDE456>.
<http://example.org/product/Turkish%20Jeans> <http://example.org/ns#catalogItem> <http://example.org/item?id=LKO478>.
<http://example.org/item?id=QWZ5671&size=Medium> <http://example.org/ns#color> <http://example.org/color/Red>.
<http://example.org/item?id=QWZ5671&size=Medium> <http://example.org/ns#color> <http://example.org/color/Burgundy>.
<http://example.org/item?id=QWZ5671&size=Large> <http://example.org/ns#color> <http://example.org/color/Red>.
<http://example.org/item?id=QWZ5671&size=Large> <http://example.org/ns#color> <http://example.org/color/Burgundy>.
<http://example.org/item?id=RRX9856&size=Small> <http://example.org/ns#color> <http://example.org/color/Red>.
<http://example.org/item?id=RRX9856&size=Small> <http://example.org/ns#color> <http://example.org/color/Navy>.
<http://example.org/item?id=RRX9856&size=Small> <http://example.org/ns#color> <http://example.org/color/Burgundy>.
<http://example.org/item?id=RRX9856&size=Medium> <http://example.org/ns#color> <http://example.org/color/Red>.
<http://example.org/item?id=RRX9856&size=Medium> <http://example.org/ns#color> <http://example.org/color/Navy>.
<http://example.org/item?id=RRX9856&size=Medium> <http://example.org/ns#color> <http://example.org/color/Burgundy>.
<http://example.org/item?id=RRX9856&size=Medium> <http://example.org/ns#color> <http://example.org/color/Black>.
<http://example.org/item?id=RRX9856&size=Large> <http://example.org/ns#color> <http://example.org/color/Navy>.
<http://example.org/item?id=RRX9856&size=Large> <http://example.org/ns#color> <http://example.org/color/Black>.
<http://example.org/item?id=RRX9856&size=Extra%20Large> <http://example.org/ns#color> <http://example.org/color/Burgundy>.
<http://example.org/item?id=RRX9856&size=Extra%20Large> <http://example.org/ns#color> <http://example.org/color/Black>.
<http://example.org/item?id=FDE456&size=Small> <http://example.org/ns#color> <http://example.org/color/Blue>.
<http://example.org/item?id=FDE456&size=Small> <http://example.org/ns#color> <http://example.org/color/Black>.
<http://example.org/item?id=FDE456&size=Extra%20Small> <http://example.org/ns#color> <http://example.org/color/White>.
<http://example.org/item?id=FDE456&size=Extra%20Small> <http://example.org/ns#color> <http://example.org/color/Transparent>.
<http://example.org/item?id=LKO478&size=Small> <http://example.org/ns#color> <http://example.org/color/Red>.
<http://example.org/item?id=LKO478&size=Small> <http://example.org/ns#color> <http://example.org/color/Blue>.
<http://example.org/item?id=LKO478&size=Small> <http://example.org/ns#color> <http://example.org/color/Orange>.
<http://example.org/item?id=LKO478&size=Large> <http://example.org/ns#color> <http://example.org/color/Navy>.
<http://example.org/item?id=LKO478&size=Large> <http://example.org/ns#color> <http://example.org/color/Black>.
<http://example.org/item?id=LKO478&size=Extra%20Large> <http://example.org/ns#color> <http://example.org/color/Pink>.
<http://example.org/item?id=LKO478&size=Extra%20Large> <http://example.org/ns#color> <http://example.org/color/Yellow>.
<http://example.org/item?id=QWZ5671> <http://example.org/ns#price> "39.95".
<http://example.org/item?id=QWZ5671> <http://example.org/ns#size> <http://example.org/item?id=QWZ5671&size=Medium>.
<http://example.org/item?id=QWZ5671> <http://example.org/ns#size> <http://example.org/item?id=QWZ5671&size=Large>.
<http://example.org/item?id=QWZ5671> <http://example.org/ns#item_number> "QWZ5671".
<http://example.org/item?id=RRX9856> <http://example.org/ns#price> "42.50".
<http://example.org/item?id=RRX9856> <http://example.org/ns#size> <http://example.org/item?id=RRX9856&size=Small>.
<http://example.org/item?id=RRX9856> <http://example.org/ns#size> <http://example.org/item?id=RRX9856&size=Medium>.
<http://example.org/item?id=RRX9856> <http://example.org/ns#size> <http://example.org/item?id=RRX9856&size=Large>.
<http://example.org/item?id=RRX9856> <http://example.org/ns#size> <http://example.org/item?id=RRX9856&size=Extra%20Large>.
<http://example.org/item?id=RRX9856> <http://example.org/ns#item_number> "RRX9856".
<http://example.org/item?id=FDE456> <http://example.org/ns#price> "22".
<http://example.org/item?id=FDE456> <http://example.org/ns#size> <http://example.org/item?id=FDE456&size=Small>.
<http://example.org/item?id=FDE456> <http://example.org/ns#size> <http://example.org/item?id=FDE456&size=Extra%20Small>.
<http://example.org/item?id=FDE456> <http://example.org/ns#item_number> "FDE456".
<http://example.org/item?id=LKO478> <http://example.org/ns#price> "35".
<http://example.org/item?id=LKO478> <http://example.org/ns#size> <http://example.org/item?id=LKO478&size=Small>.
<http://example.org/item?id=LKO478> <http://example.org/ns#size> <http://example.org/item?id=LKO478&size=Large>.
<http://example.org/item?id=LKO478> <http://example.org/ns#size> <http://example.org/item?id=LKO478&size=Extra%20Large>.
<http://example.org/item?id=LKO478> <http://example.org/ns#item_number> "LKO478".

Note: I contribute to RML and its technologies.

Upvotes: 4

Related Questions