问题描述
我正在尝试使用 XSLT 将 RDF 文件转换为 HTML.
I am trying to transform an RDF file to HTML using XSLT.
我正在使用我在网上找到的模板:http://snippets.dzone.com/posts/show/1164
I am using the template that i've found on the web:http://snippets.dzone.com/posts/show/1164
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:foo="http://purl.org/rss/1.0/">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:apply-templates select="/rdf:RDF/foo:channel"/>
</xsl:template>
<xsl:template match="/rdf:RDF/foo:channel">
<h3><xsl:value-of select="foo:title"/></h3>
<p><xsl:value-of select="foo:description"/></p>
<ul>
<xsl:apply-templates select="/rdf:RDF/foo:item"/>
</ul>
</xsl:template>
<xsl:template match="/rdf:RDF/foo:item">
<li>
<a href="{foo:link}" title="{substring(dc:date, 0, 11)}"><xsl:value-of select="foo:title"/></a>
<p><xsl:value-of select="foo:description" disable-output-escaping="yes" /></p>
</li>
</xsl:template>
</xsl:stylesheet>
它与这样的 RDF 文件完美配合:(这是完整文件的链接)http://dl.dropbox.com/u/2232733/rdfexample.xml
It works perfectly with an RDF file like that:(Here is the link to the complete file)http://dl.dropbox.com/u/2232733/rdfexample.xml
<?xml version="1.0" encoding="utf-8"?><!-- generator="wordpress/1.2" -->
<rdf:RDF
xmlns="http://purl.org/rss/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
>
<channel rdf:about="http://www.wasab.dk/morten/blog/rdf">
<title>Binary Relations</title>
<link>http://www.wasab.dk/morten/blog</link>
<description>Reflections on the web</description>
<dc:language>en</dc:language>
<dc:date>2004-05-29T23:02:37Z</dc:date>
<admin:generatorAgent rdf:resource="http://wordpress.org/?v=1.2"/>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<sy:updateBase>2000-01-01T12:00+00:00</sy:updateBase>
<items>
<rdf:Seq>
<rdf:li rdf:resource="http://www.wasab.dk/morten/blog/archives/2004/05/30/wordpress-plugin-linkifier"/>
...
<rdf:li rdf:resource="http://www.wasab.dk/morten/blog/archives/2004/05/20/yet-another-semweb-blogger"/>
</rdf:Seq>
</items>
</channel>
<item rdf:about="http://www.wasab.dk/morten/blog/archives/2004/05/30/wordpress-plugin-linkifier">
<title>WordPress Plugin: Linkifier</title>
但是,我无法使其适用于从 lastfm.rdfize.com/获得的 RDF\XML:(这是文件的链接)http://dl.dropbox.com/u/2232733/metala_rdf_xml.xml
However, I can't make it work for an RDF\XML that i get from lastfm.rdfize.com/:(here is the link for the file)http://dl.dropbox.com/u/2232733/metallica_rdf_xml.xml
它有一个不同的结构,我不知道我应该使用 XSTL 中的哪些标签来制作 HTML:
It has a different structure and I can't figure out what tags in XSTL should i use to make an HTML from it:
rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:terms="http://purl.org/dc/terms/"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:mo="http://purl.org/ontology/mo/"
xmlns:ov="http://open.vocab.org/terms/"
xmlns:event="http://purl.org/NET/c4dm/event.owl#"
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
xmlns:v="http://www.w3.org/2006/vcard/ns#">
<rdf:Description rdf:about="http://lastfm.rdfize.com/artists/Metallica">
<rdf:type rdf:resource="http://purl.org/ontology/mo/MusicArtist"/>
<rdfs:label>Metallica</rdfs:label>
...
所以我的问题是 - 我应该如何形成 XSLT 来处理我的 RDF?提前致谢!
So my question is - how should I form an XSLT to process my RDF?Thanks in advance!
推荐答案
您的主要问题是您的样式表过于特定于领域.RDF/XML 是一种非常复杂的序列化,它允许以多种方式陈述事物.您的样式表指的是非常具体的元素名称,这些名称仅在非常窄的 RDF/XML 文档范围内有效,例如
Your main problem is that your stylesheet is far too domain specific. RDF/XML is a very complex serialization that allows multiple ways of stating things. Your stylesheet is referring to very specific element names which are only valid in a very narrow range of RDF/XML documents e.g.
<xsl:template match="/rdf:RDF/foo:item">
以上尝试匹配根级 元素,这实际上不是强制性的 - 使用替代根元素或
是可以接受的;
嵌入到其他 XML 元素中,前提是只有一个.
The above attempts to match the root level <rdf:RDF>
elements which is actually not mandatory - it's acceptable for alternative root elements to be used or for <rdf:RDF>
to be embedded inside other XML elements provided there is only one.
然后它尝试匹配 <foo:item>
元素,它是一个文档特定的 ID,这个元素名称绝对可以是来自一般 <rdf:Description>
到前面的例子.
Then it attempts to match the <foo:item>
element which is a document specific ID, this element name could be absolutely anything from the general <rdf:Description>
to the previous example.
解决方案
不要尝试使用样式表来转换 RDF/XML,因为您编写的任何可以转换全范围 RDF/XML 文档的样式表都将非常复杂,以至于毫无意义.
Don't attempt to transform RDF/XML with a stylesheet as invariably any stylesheet you write which can transform a full range of RDF/XML documents will be so ridicously complex as to make no sense whatsoever.
相反发现自己是一个很好的 RDF 库(大多数主要语言都有一个/多个),它提供了一个 RDF/XML 解析器和一个 HTML 或 HTML+RDFa 编写器并使用它们.如果您想控制 HTML 输出作为基础,请使用库的 HTML 编写器作为指导并对其进行调整或仅在其输出上应用 CSS 样式表.
Instead find yourself a good RDF library (there's one/more out there for most major languages) which provides an RDF/XML parser and a HTML or HTML+RDFa writer and use those. If you want to control the HTML output as a base use the library's HTML writer as a guide and adapt it or just apply a CSS stylesheet over its output.
这篇关于从 RDF 到 html 的 XSLT 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!