使用耶拿从XMl文件创建RDF [英] Using jena for creating RDF from XMl file
问题描述
可能重复:
从XML创建RDF
Possible Duplicate:
create RDF from XML
对于第二次问这个问题,我感到抱歉,但是从下面的xml文件生成rdf时,我仍然遇到问题.
I am sorry for asking this question for the second time, I am still still having problem in generating rdf from the following xml file.
<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个属性,我希望结果如下所示
I think using jena might solve the issue , but I am not sure how to do so, since each has got three properties and I would like the out put to look like the following
<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>
在此先感谢您的帮助
推荐答案
除非是RDF/XML,否则无法使用Jena解析XML.
You can't parse the XML with Jena, unless it is RDF/XML.
您将必须使用XLST将XML转换为RDF或使用Java XML库解析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.
使用XSLT非常简单,如下面的示例所示.
Using XSLT is fairly simple, as demonstrated from the example below.
因为网站是URL,所以我将其用作URI而不是文字.另外,FOAF是名称的通用名称.所以,我会使用类似的东西:
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>
这将为您提供:
<?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中
You can then load the RDF file into Jena
这篇关于使用耶拿从XMl文件创建RDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!