XSLT,RDF和XML之间的转换

时间:2013-11-17 23:49:13

标签: xml xslt rdf

我在.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>

我正在使用.xsl表

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

我得到的结果是:

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

有两件事并不顺利。首先是我如何使用.xsl表中定义的“tabs”格式化输出。第二个是某些结果不太好,例如:

中的值

1) http://vivo.iu.edu/individual/n5798必须为http://vivo.iu.edu/individual/grant42915,这是第二个“描述”集中的值

2) 2001-01-01T00:00:00的两个值都应该是2002-02-02T00:00:00,这是第5组描述中的值

非常感谢

1 个答案:

答案 0 :(得分:0)

尽管Ian Roberts和Joshua Taylor对使用RDF库处理rdf文件有一点意见,但如果你确定RDF永远不会改变那么使用XSLT就不那么重要了。但是,如果你不是,最好编写自己的方法来编写语句(从RDF库)到XML。

回答你的问题。

  1. 格式取决于您的XSLTransformer,我使用Java(javax.xml.transform.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");
    
  2. 检索错误的结果。你现在这样做的方式并不是做XSLT的正确方法。您的select语句可以定位任何rdf:Description标记并选择相关属性。如果要处理项目,则应编写与此属性匹配的模板,然后处理这些属性。这样的事情(我不完全确定你的数据,所以这可能是错的):

    <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>
    
  3. 这将选择每个rdf的rdf:about值:具有Project类型的描述。