开发者

XSLT transformation from RDF to html

I am trying to transform an RDF file to HTML using XSLT.

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>

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>

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

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.rd开发者_StackOverflowfize.com/artists/Metallica">
    <rdf:type rdf:resource="http://purl.org/ontology/mo/MusicArtist"/>
    <rdfs:label>Metallica</rdfs:label>
  ...

So my question is - how should I form an XSLT to process my RDF? Thanks in advance!


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

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.

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.

Solution

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.

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.


Agree with Rob that you should look into a good RDF API, but you might also want to check out grddl.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜