2010-11-15 29 views
4

我正嘗試使用XSLT將RDF文件轉換爲HTML。從RDF到HTML的XSLT轉換

我使用,我已經在網絡上找到的模板: 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

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

但是,我不能讓它一個RDF \ XML,我從lastfm.rdfize.com/得到工作: (這裏是該文件的鏈接http://dl.dropbox.com/u/2232733/metallica_rdf_xml.xml

它有不同的結構,我想不出在XSTL標籤,我應該用什麼來使從它的HTML:

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? 在此先感謝!

+0

1.這兩個文本都是嚴重畸形的 - 不完整的XML文檔。 2.根本沒有問題。請改正。 – 2010-11-15 03:02:58

+0

我提供了上面的鏈接 - http://dl.dropbox.com/u/2232733/rdfexample.xml http://dl.dropbox.com/u/2232733/metallica_rdf_xml.xml 問題是 - 如何正確處理我的rdf? – Hippopotamus 2010-11-15 10:37:46

+0

也許如果你提供了一個期望的輸出,那麼有人會爲你的輸入樣本發佈一個樣式表的例子。 – 2010-11-15 21:16:22

回答

4

你的主要問題是你的樣式表太過域名特定。 RDF/XML是一個非常複雜的序列化,它允許多種陳述事物的方式。您的樣式表是指非常特定的元素名稱,它們僅在RDF/XML文檔的非常窄的範圍內有效,例如

<xsl:template match="/rdf:RDF/foo:item"> 

上述嘗試匹配根級別<rdf:RDF>元件,其實際上不是強制性的 - 這是可以接受的,以使用替代的根元素或<rdf:RDF>要被嵌入其它XML元素的內部設置有唯一的一個。

然後它試圖匹配<foo:item>元素,這是一個文檔特定的ID,這個元素名稱可以是絕對的任何東西,從一般<rdf:Description>到前面的例子。

解決方案

不要嘗試用樣式錶轉換RDF/XML作爲不約而同你寫的任何樣式表可以改變一個全方位的RDF/XML文檔會如此ridicously複雜,以使沒有任何意義。

相反發現自己良好的RDF庫(有一個/多個那裏大多數主要語言),它提供了一個RDF/XML解析器和HTML或HTML + RDFa的作家,並使用這些。如果您想要將HTML輸出作爲基礎控件使用該庫的HTML編寫器作爲指導,並對其進行調整或僅在其輸出上應用CSS樣式表。

+0

謝謝! 你能指點我一個有HTML編寫器的人嗎? 我目前正在使用Jena,但是Jena只允許導出爲XML和TURTLE。 – Hippopotamus 2010-11-15 09:51:43

+0

你對「不使用通用樣式表」的建議是正確的。 RDF是一種嘗試標準「關係」的詞彙加上標準「語義」詞彙的持有者。但是,只要您知道如何處理這兩個字段,就可以使用通用樣式表。 – 2010-11-15 21:15:17

+0

@Hippopotamus Jena有RDFa作家,但他們不是標準jena發行版的一部分 – RobV 2010-11-16 10:07:16

-1

與Rob一致,你應該看看一個好的RDF API,但你也可能想看看grddl

+0

-1。 GRDDL正試圖解決相反的問題:將XML轉換爲RDF,並使用XSLT進行處理。將RDF/XML轉換爲其他XML格式是沒有用的。 – 2016-03-27 23:32:24