Using jena for creating RDF from XMl file [duplicate]

橙三吉。 提交于 2019-12-09 01:16:27

You can't parse the XML with Jena, unless it is RDF/XML.

You'll have to use XLST to transform the XML to RDF or parse the XML with a Java XML library to get the data and create the triples from the data of interest.

Using XSLT is fairly simple, as demonstrated from the example below.

Since the website is URL I'd use it as a URI rather than a literal. Also, FOAF is common for names. So, I'd use something like:

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:html="http://www.w3.org/1999/xhtml"
            xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
            xmlns:foaf="http://xmlns.com/foaf/spec/"
            xmlns:foo="http://example.com/foo#">

<xsl:template match="/">
    <rdf:RDF>
        <rdf:Description rdf:about="http://www.example.com/xml">
            <xsl:apply-templates/>
    </rdf:Description>
    </rdf:RDF>
</xsl:template>

<xsl:template match="person">
<xsl:variable name="critic"><xsl:value-of select="name"/></xsl:variable>
<xsl:variable name="criticWebsite"><xsl:value-of select="website/@url"/</xsl:variable>
<foo:hasCritic>
    <rdf:Description rdf:about="http://www.example.com/critic/{$critic}">
        <foaf:name><xsl:value-of select="name"/></foaf:name>
        <foaf:homepage>
            <rdf:Description rdf:about="http://{$criticWebsite}">
                <rdfs:label><xsl:value-of select="website"/></rdfs:label>
            </rdf:Description>
        </foaf:homepage>
    </rdf:Description>
</foo:hasCritic>
</xsl:template>

</xsl:stylesheet>

This will give you:

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:html="http://www.w3.org/1999/xhtml"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:foaf="http://xmlns.com/foaf/spec/"
    xmlns:foo="http://example.com/foo#">

    <rdf:Description rdf:about="http://www.example.com/xml">
        <foo:hasCritic>
            <rdf:Description rdf:about="http://www.example.com/critic/Joe">
                <foaf:name>Joe</foaf:name>
                <foaf:homepage>
                    <rdf:Description rdf:about="http://www.example1.com">
                        <rdfs:label>contact1</rdfs:label>
                    </rdf:Description>
                </foaf:homepage>
            </rdf:Description>
        </foo:hasCritic>
        <foo:hasCritic>
        <rdf:Description rdf:about="http://www.example.com/critic/Anna">
            <foaf:name>Anna</foaf:name>
                <foaf:homepage>
                    <rdf:Description rdf:about="http://www.example2.com">
                        <rdfs:label>contact2</rdfs:label>
                    </rdf:Description>
                </foaf:homepage>
            </rdf:Description>
        </foo:hasCritic>
        </rdf:Description>
    </rdf:RDF>

You can then load the RDF file into Jena

I would suggest using XSLT to convert that XML into RDF. Since it is probably very regular, the XSLT will be quite straightforward to write and apply to get the RDF you want. Then you can use Jena to parse the resulting RDF and do something with that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!