我是XSLT的新手 . 我有一个XML文件,并希望使用XSLT将其转换为RDF / XML . 实际上我找到了一个XSLT的样式表,并且在它和XML文件之间 Build 链接,结果在浏览器中显示为“text”而不是XML文件 . 我的问题是:我希望以RDF / XML格式获得转换结果,但不幸的是我得到的结果是纯文本 .
XMl文件
<xml>
<?xml-stylesheet type="text/xsl" href="qu.xsl"?>
<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>
和XSLT样式表是
<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>
但结果是:Joe contact1 20 Anna contact2 80
2 回答
Your XSLT (with a typo fixed near the criticWebsite xsl:value-of):
Applied to your input XML file (with the xml-stylesheet declaration moved immediately beneath the XML declaration, and the path to the XSLT file made absolute):
Produces this RDF document:
注意:
要在Firefox中查看此内容,请右键单击输出并选择"Inspect Element",否则您将只看到"Joecontact1 Annacontact2" .
这不适用于Chrome中的本地文件,因为Chrome refuses to run locally loaded XSLT . 它必须从服务器远程加载才能工作 .
将XML转换为RDF也可以使用XmlToRdf库在Java中完成 .
我在github上写了一个有用的例子:https://github.com/hmottestad/XmlToRdf---Example-from-Stack-Overflow-1
这是转换为RDF Turtle的结果
使用以下java代码