1

重複の可能性:
XMLからRDFを作成する

この質問を2回目にして申し訳ありませんが、次のxmlファイルからrdfを生成する際にまだ問題があります。

<xml>
<person>
<name>Joe</name>
<website url="www.example1.com">contact1</website >
<vote>20</vote>
</person>
<person>
 <name>Anna</name>
<website url="www.example2.com">contact2</website>
 <vote>80</vote>
 </person>
 </xml>

jenaを使用すると問題が解決する可能性があると思いますが、それぞれに3つのプロパティがあり、出力を次のようにしたいと思います。

<rdf:Description rdf:about="http://www.example.com/xml">
 <j.0:hasCritic>Joe</j.0:hasCritic>
 <rdf:Description rdf:about=Joe >
 <j.0:haswebsite>"www.example1.com"</j.0:haswebsite>
  <j.0:hascontact>contact1</j.0:hascontact>
  <j.0:hasvote>80</j.0:hasvote>
 </rdf:Description>
<j.0:hasCritic>Anna</j.0:hasCritic>
 <rdf:Description rdf:about=Anna>
 <j.0:haswebsite>"www.example2.com"</j.0:haswebsite>
  <j.0:hascontact>contact2</j.0:hascontact>
  <j.0:hasvote>20</j.0:hasvote>

助けてくれてありがとう

4

2 に答える 2

5

RDF / XMLでない限り、JenaでXMLを解析することはできません。

XLSTを使用してXMLをRDFに変換するか、Java XMLライブラリでXMLを解析してデータを取得し、対象のデータからトリプルを作成する必要があります。

以下の例からわかるように、XSLTの使用は非常に簡単です。

ウェブサイトはURLなので、文字通りではなくURIとして使用します。また、FOAFは名前に一般的です。だから、私は次のようなものを使用します:

<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>

これはあなたに与えるでしょう:

<?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>

その後、RDFファイルをJenaにロードできます

于 2012-05-09T11:16:50.157 に答える
2

XSLTを使用してそのXMLをRDFに変換することをお勧めします。おそらく非常に定期的であるため、XSLTは、必要なRDFを取得するために記述および適用するのが非常に簡単です。次に、Jenaを使用して結果のRDFを解析し、それを使って何かを行うことができます。

于 2012-05-08T16:40:27.813 に答える