4

I am new to XSLT. I have an XML file and want to transform it to RDF/XML using XSLT. Actually I found a stylesheet of XSLT and where make link between it and XML file the result appear just " text " in browser instead of XML file. My question is: I want getting the transformation result in RDF/XML format , but unfortunately I get the result as plain text.

the XMl file

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

and XSLT stylesheet is

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

but the result is: Joe contact1 20 Anna contact2 80

user3786761
  • 53
  • 1
  • 6
  • I fixed multiple misspellings and grammatical errors, but it's still unclear what you're asking. Be aware that asking for a library will likely result in the question being closed as such recommendation requests are offtopic here. I suggest showing some sample input and output and what you've tried in XSLT to achieve the desired result. Also suggest that you look at output via a text editor rather than a browser; if the point of your question is the appearance of XML in a browser, please edit your question to clarify that intent. Thanks. – kjhughes Sep 21 '14 at 13:25
  • @kjhughes I add my try , as I said I am new and take this example from http://stackoverflow.com/questions/10499281/using-jena-for-creating-rdf-from-xml-file , but I want learn how to convert and represent the transformation in RDF/XML format – user3786761 Sep 21 '14 at 13:37
  • Thanks for adding actual XML and XSLT -- makes it much easier for us to help. See my detailed [**answer below**](http://stackoverflow.com/a/25960266/2900850). – kjhughes Sep 21 '14 at 14:36

1 Answers1

5

Your XSLT (with a typo fixed near the criticWebsite xsl:value-of):

<?xml version="1.0"?>
<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>

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):

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="file:///c:/path/to/XSLT/qu.xsl"?>
<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>

Produces this RDF document:

<?xml version="1.0" encoding="UTF-8"?><rdf:RDF 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#"><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>

Notes:

  • To see this in Firefox, right-click on the output and select "Inspect Element", otherwise you'll only see "Joecontact1 Annacontact2".
  • This will not work with local files in Chrome because Chrome refuses to run locally loaded XSLT. It will have to be loaded remotely from a server to work.
kjhughes
  • 106,133
  • 27
  • 181
  • 240