问题
I have this file in .rdf
<rdf:RDF
xmlns:vitro-public="http://vitro.mannlib.cornell.edu/ns/vitro/public#"
xmlns:c4o="http://purl.org/spar/c4o/"
xmlns:ero="http://purl.obolibrary.org/obo/"
xmlns:pvs="http://vivoweb.org/ontology/provenance-support#"
xmlns:owl2="http://www.w3.org/2006/12/owl2-xml#"
xmlns:scirr="http://vivoweb.org/ontology/scientific-research-resource#"
xmlns:vivo="http://vivoweb.org/ontology/core#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:bibo="http://purl.org/ontology/bibo/"
xmlns:afn="http://jena.hpl.hp.com/ARQ/function#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:scires="http://vivoweb.org/ontology/scientific-research#"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:geo="http://aims.fao.org/aos/geopolitical.owl#"
xmlns:skco="http://www.w3.org/2004/02/skos/core#"
xmlns:event="http://purl.org/NET/c4dm/event.owl#"
xmlns:dcelem="http://purl.org/dc/elements/1.1/"
xmlns:vitro="http://vitro.mannlib.cornell.edu/ns/vitro/0.7#"
xmlns:vann="http://purl.org/vocab/vann/"
xmlns:skos="http://www.w3.org/2008/05/skos#"
xmlns:swvs="http://www.w3.org/2003/06/sw-vocab-status/ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:fabio="http://purl.org/spar/fabio/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<rdf:Description rdf:about="http://vivo.iu.edu/individual/n5798">
<rdf:type rdf:resource="http://vivoweb.org/ontology/core#Project"/>
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Kuali Open Library Environment</rdfs:label>
<vivo:abbreviation>KOLE</vivo:abbreviation>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
<vivo:webpage rdf:resource="https://www.kuali.org/ole"/>
<vivo:dateTimeInterval rdf:resource="http://vivo.iu.edu/individual/n5812339"/>
</rdf:Description>
<rdf:Description rdf:about="http://vivo.iu.edu/individual/grant42915">
<rdf:type rdf:resource="http://vivoweb.org/ontology/core#Grant"/>
<vivo:totalAwardAmount>12452.7</vivo:totalAwardAmount>
<vivo:hasFundingVehicle rdf:resource="http://vivo.iu.edu/individual/n5798"/>
</rdf:Description>
<rdf:Description rdf:about="http://vivo.iu.edu/individual/n5812339">
<rdf:type rdf:resource="http://vivoweb.org/ontology/core#DateTimeInterval"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
<vivo:start rdf:resource="http://vivo.iu.edu/individual/n2359024"/>
<vivo:end rdf:resource="http://vivo.iu.edu/individual/n2788825"/>
<vitro:mostSpecificType rdf:resource="http://vivoweb.org/ontology/core#DateTimeInterval"/>
</rdf:Description>
<rdf:Description rdf:about="http://vivo.iu.edu/individual/n2359024">
<rdf:type rdf:resource="http://vivoweb.org/ontology/core#DateTimeValue"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
<vivo:dateTimePrecision rdf:resource="http://vivoweb.org/ontology/core#yearPrecision"/>
<vivo:dateTime rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2001-01-01T00:00:00</vivo:dateTime>
<vitro:mostSpecificType rdf:resource="http://vivoweb.org/ontology/core#DateTimeValue"/>
</rdf:Description>
<rdf:Description rdf:about="http://vivo.iu.edu/individual/n2788825">
<rdf:type rdf:resource="http://vivoweb.org/ontology/core#DateTimeValue"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
<vivo:dateTimePrecision rdf:resource="http://vivoweb.org/ontology/core#yearPrecision"/>
<vivo:dateTime rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2002-02-02T00:00:00</vivo:dateTime>
<vitro:mostSpecificType rdf:resource="http://vivoweb.org/ontology/core#DateTimeValue"/>
</rdf:Description>
</rdf:RDF>
And I am using that .xsl sheet
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:vivo="http://vivoweb.org/ontology/core#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:owl2="http://www.w3.org/2006/12/owl2-xml#"
xmlns:iuvivo="http://vivo.iu.edu/ontology/iuvivo#"
version="1.0">
<xsl:output
method="xml"
indent="yes"
omit-xml-declaration="no"
encoding="utf-8"/>
<xsl:template match="rdf:RDF">
<CERIF
xmlns="urn:xmlns:org:eurocris:cerif-1.5-1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
release="1.5" date="2012-10-17"
sourceDatabase="Project Profile">
<xsl:if test="rdf:Description/rdf:type/@rdf:resource='http://vivoweb.org/ontology/core#Project'">
<cfProj>
<cfProjId><xsl:value-of select="rdf:Description/@rdf:about"/></cfProjId>
<cfAcro><xsl:value-of select="rdf:Description/vivo:abbreviation"/></cfAcro>
<cfURI><xsl:value-of select="rdf:Description/vivo:webpage //@rdf:resource"/></cfURI>
<xsl:if test="rdf:Description/vivo:dateTimeInterval/@rdf:resource=rdf:Description/@rdf:about and rdf:Description/vivo:start/@rdf:resource=rdf:Description/@rdf:about">
<cfStartDate><xsl:value-of select="rdf:Description/vivo:dateTime"/></cfStartDate>
</xsl:if>
<xsl:if test="rdf:Description/vivo:dateTimeInterval/@rdf:resource=rdf:Description/@rdf:about and rdf:Description/vivo:end/@rdf:resource=rdf:Description/@rdf:about">
<cfEndDate><xsl:value-of select="rdf:Description/vivo:dateTime"/></cfEndDate>
</xsl:if>
<cfProj_Class>
<cfClassId>0bd2d47a-8688-4758-a63c-45e76825a0f6</cfClassId>
<cfClassSchemeId>794234b8-25bb-46df-9d26-ae660bca64bc</cfClassSchemeId>
</cfProj_Class>
<xsl:if test="rdf:Description/vivo:hasFundingVehicle/@rdf:resource=rdf:Description/@rdf:about">
<cfProj_Fund>
<cfFundId><xsl:value-of select="rdf:Description/@rdf:about"/></cfFundId>
<cfAmount><xsl:value-of select="rdf:Description/vivo:totalAwardAmount"/></cfAmount>
<cfClassId>eda2b2e6-34c5-11e1-b86c-08000200c9a66</cfClassId>
<cfClassSchemeId>759af93b-34c5-11e1-b86c-0800200c9a66</cfClassSchemeId>
</cfProj_Fund>
<xsl:if test="rdf:Description/vivo:dateTimeInterval/@rdf:resource=rdf:Description/@rdf:about and rdf:Description/vivo:start/@rdf:resource=rdf:Description/@rdf:about">
<cfStartDate><xsl:value-of select="rdf:Description/vivo:dateTime"/></cfStartDate>
</xsl:if>
<xsl:if test="rdf:Description/vivo:dateTimeInterval/@rdf:resource=rdf:Description/@rdf:about and rdf:Description/vivo:end/@rdf:resource=rdf:Description/@rdf:about">
<cfEndDate><xsl:value-of select="rdf:Description/vivo:dateTime"/></cfEndDate>
</xsl:if>
</xsl:if>
</cfProj>
</xsl:if>
<xsl:if test="rdf:Description/rdf:type/@rdf:resource='http://xmlns.com/foaf/0.1/Person'">
<cfPers>
<cfPersId><xsl:value-of select="rdf:Description/@rdf:about"/></cfPersId>
<cfGender><xsl:value-of select="rdf:Description/owl2:gender"/></cfGender>
<cfURI><xsl:value-of select="rdf:Description/vivo:webpage //@rdf:resource"/></cfURI>
<cfPersName>
<cfFirstNames><xsl:value-of select="concat(rdf:Description/foaf:firstName, ' ', rdf:Description/vivo:middleName)"/></cfFirstNames>\n
<cfFamilyNames><xsl:value-of select="rdf:Description/foaf:lastName"/></cfFamilyNames>
</cfPersName>
<cfPersKeyw>
<cfKeyw><xsl:value-of select="rdf:Description/iuvivo:freetextKeyword"/></cfKeyw>
</cfPersKeyw>
<cfPers_OrgUnit>
<cfClassId>eda2b2e6-34c5-11e1-b86c-08000200c9a66</cfClassId>
<cfClassSchemeId>759af93b-34c5-11e1-b86c-0800200c9a66</cfClassSchemeId>
<xsl:if test="rdf:Description/vivo:dateTimeInterval/@rdf:resource=rdf:Description/@rdf:about and rdf:Description/vivo:start/@rdf:resource=rdf:Description/@rdf:about">
<cfStartDate><xsl:value-of select="rdf:Description/vivo:dateTime"/></cfStartDate>
</xsl:if>
<xsl:if test="rdf:Description/vivo:dateTimeInterval/@rdf:resource=rdf:Description/@rdf:about and rdf:Description/vivo:end/@rdf:resource=rdf:Description/@rdf:about">
<cfEndDate><xsl:value-of select="rdf:Description/vivo:dateTime"/></cfEndDate>
</xsl:if>
</cfPers_OrgUnit>
</cfPers>
</xsl:if>
<xsl:if test="rdf:Description/rdf:type/@rdf:resource='http://xmlns.com/foaf/0.1/Organization'">
<cfOrgUnit>
<cfOrgId><xsl:value-of select="rdf:Description/@rdf:about"/></cfOrgId>
<cfAcro><xsl:value-of select="rdf:Description/vivo:abbreviation"/></cfAcro>
<cfURI><xsl:value-of select="rdf:Description/vivo:webpage //@rdf:resource"/></cfURI>
<cfOrgKeyw>
<cfKeyw><xsl:value-of select="rdf:Description/iuvivo:freetextKeyword"/></cfKeyw>
</cfOrgKeyw>
<cfPers_OrgUnit>
<cfClassId>eda2b2e6-34c5-11e1-b86c-08000200c9a66</cfClassId>
<cfClassSchemeId>759af93b-34c5-11e1-b86c-0800200c9a66</cfClassSchemeId>
<xsl:if test="rdf:Description/vivo:dateTimeInterval/@rdf:resource=rdf:Description/@rdf:about and rdf:Description/vivo:start/@rdf:resource=rdf:Description/@rdf:about">
<cfStartDate><xsl:value-of select="rdf:Description/vivo:dateTime"/></cfStartDate>
</xsl:if>
<xsl:if test="rdf:Description/vivo:dateTimeInterval/@rdf:resource=rdf:Description/@rdf:about and rdf:Description/vivo:end/@rdf:resource=rdf:Description/@rdf:about">
<cfEndDate><xsl:value-of select="rdf:Description/vivo:dateTime"/></cfEndDate>
</xsl:if>
</cfPers_OrgUnit>
</cfOrgUnit>
</xsl:if>
</CERIF>
</xsl:template>
</xsl:stylesheet>
The results I get are this:
<?xml version="1.0" encoding="utf-8"?><CERIF release="1.5" date="2012-10-17" sourceDatabase="Project Profile" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:iuvivo="http://vivo.iu.edu/ontology/iuvivo#" xmlns:owl2="http://www.w3.org/2006/12/owl2-xml#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vivo="http://vivoweb.org/ontology/core#" xmlns="urn:xmlns:org:eurocris:cerif-1.5-1">
<cfProj>
<cfProjId>http://vivo.iu.edu/individual/n5798</cfProjId>
<cfAcro>KOLE</cfAcro>
<cfURI>https://www.kuali.org/ole</cfURI>
<cfStartDate>2001-01-01T00:00:00</cfStartDate>
<cfEndDate>2001-01-01T00:00:00</cfEndDate>
<cfProj_Class>
<cfClassId>0bd2d47a-8688-4758-a63c-45e76825a0f6</cfClassId>
<cfClassSchemeId>794234b8-25bb-46df-9d26-ae660bca64bc</cfClassSchemeId>
</cfProj_Class>
<cfProj_Fund>
<cfFundId>http://vivo.iu.edu/individual/n5798</cfFundId>
<cfAmount>12452.7</cfAmount>
<cfClassId>eda2b2e6-34c5-11e1-b86c-08000200c9a66</cfClassId>
<cfClassSchemeId>759af93b-34c5-11e1-b86c-0800200c9a66</cfClassSchemeId>
</cfProj_Fund>
<cfStartDate>2001-01-01T00:00:00</cfStartDate>
<cfEndDate>2001-01-01T00:00:00</cfEndDate>
</cfProj>
There are two things that are not well. First is how I can format the output with the "tabs" define in the .xsl sheet. The second is that some results are not well for example the values in:
1) http://vivo.iu.edu/individual/n5798 has to be http://vivo.iu.edu/individual/grant42915 which is the value in the second "Description" set
2) Both values of 2001-01-01T00:00:00 should be 2002-02-02T00:00:00 which is a value from the 5th set of Description
Thanks a lot
回答1:
Although Ian Roberts and Joshua Taylor have a point regarding using a RDF library to process the rdf file, if you are sure the RDF will never change then using XSLT is less work. However, if you are not it is better write your own method to write statements (from the RDF library) to XML.
To answer your questions.
The formatting depends on on your XSLTransformer, I use Java's(javax.xml.transform.Transformer) and then I need to set the ident size like this (it might be different for your transformer):
TransformerFactory factory = TransformerFactory.newInstance(); Source xslt = new StreamSource(new File("test.xslt")); Transformer transformer = factory.newTransformer(xslt); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Retrieving the wrong results. The way your are doing it now is not the correct way to do XSLT. Your select statements can target any of the rdf:Description tags and select the relevant property. If you want to process the projects then you should write a template that matches this property and then process these attributes. Something like this (I'm not entirely sure about your data, so this could be wrong):
<xsl:template match="rdf:Description"> <xsl:if test="rdf:type/@rdf:resource='http://vivoweb.org/ontology/core#Project'"> <cfProj> <cfProjId><xsl:value-of select="@rdf:about"/></cfProjId>
This will select the rdf:about value of each rdf:Description that has type Project.
来源:https://stackoverflow.com/questions/20037781/xslt-transformation-between-rdf-and-xml