使用RML-Mapper库将xml文件转换为RDF的最佳map.ttl文件是什么?

时间:2019-05-23 08:15:18

标签: java xml turtle-rdf

我正在使用RML-Mapper library以便将xml文件转换为rdf文件。 对于以下xml文件catalogs.xml,我很难编写最佳的mapping.ttl。我的问题是,该特定xml的最佳map.ttl文件是什么?我在下面提供catalogs.xml和catalog_mapping.ttl文件...

Catalogs.xml文件

<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}"
        ]
    ].

这是我目前的结果……


<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

这个结果好还是有更好的catalog_mapping.ttl可以得出更好的结果?

1 个答案:

答案 0 :(得分:3)

映射规则已经相当不错,我做了以下改进:

  1. rr:<name of TriplesMap>不应用作您自己的前缀 三元组地图。 rr前缀由 R2RML specification。 为了解决这个问题,我添加了@base http://example.org并删除了{ TriplesMaps:
rr
  1. 我为每个用于RML处理器的TriplesMap添加了@base <http://example.org/> . <#ProductMapping> a rr:TriplesMap; ,以便看到 RDF实际上是在描述 TriplesMap
a rr:TriplesMap
  1. 为了简化查询RDF的过程,我将<#ProductMapping> a rr:TriplesMap; 拆分为 放入不同的TripleMap。这样,颜色和尺寸就会获得自己的IRI, 只能用于查询产品的尺寸或颜色。 另请注意,由于我们仅使用ex:size_and_color而不是rml:reference 需要该值而不创建字符串模板。 您可以在 RML specification中找到有关rr:template的更多信息。
rml:reference

所有映射规则

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"
        ]
    ].

输出

@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/{.}";
    ].

注意:我为RML及其技术做出了贡献。