如何使用rdflib获取xml文件的基本uri?

时间:2017-05-02 13:33:18

标签: python xml rdflib

我正在使用python的rdflib包将RDF xml解析为三元组。我正在比较多个文件,我希望能够检查<rdf:RDF>标记中定义为xml:base的基本URI是否与两个文件相同。

我在图书馆阅读的代码如下:

import rdflib

g = rdflib.Graph()
g.load("some_file.xml")

在上面的代码中,g包含一个RDF主题 - 谓词 - 对象三元组的图表,我可以迭代并使用它。

我可以使用xmlns:...获取g.namespaces()表示的命名空间元素。

some_file.xml的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
  xmlns:cims="http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
  xmlns:cim="http://iec.ch/TC57/2013/CIM-schema-cim16#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xml:base="http://iec.ch/TC57/2013/CIM-schema-cim16">
  <rdf:Description rdf:about="#Package_newprofile">
    <rdfs:label xml:lang="en">newprofile</rdfs:label>
    <rdf:type rdf:resource="http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#ClassCategory"/>
  </rdf:Description>
  <rdf:Description rdf:about="#ACLineSegment">
    <rdfs:label xml:lang="en">ACLineSegment</rdfs:label>
    <rdfs:comment rdf:parseType="Literal">A wire or combination of wires, with consistent electrical characteristics, building a single electrical system, used to carry alternating current between points in the power system. For symmetrical, transposed
      3ph lines, it is sufficient to use attributes of the line segment, which describe impedances and admittances for the entire length of the segment. Additionally impedances can be computed by using length and associated per length impedances. The
      BaseVoltage at the two ends of ACLineSegments in a Line shall have the same BaseVoltage.nominalVoltage. However, boundary lines may have slightly different BaseVoltage.nominalVoltages and variation is allowed. Larger voltage difference in general
      requires use of an equivalent branch.</rdfs:comment>
    <cims:belongsToCategory rdf:resource="#Package_newprofile"/>
    <cims:stereotype rdf:resource="http://iec.ch/TC57/NonStandard/UML#concrete"/>
    <rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="#ACLineSegment.r">
    <cims:stereotype rdf:resource="http://iec.ch/TC57/NonStandard/UML#attribute"/>
    <rdfs:label xml:lang="en">r</rdfs:label>
    <rdfs:domain rdf:resource="#ACLineSegment"/>
    <cims:dataType rdf:resource="#Resistance"/>
    <cims:multiplicity rdf:resource="http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#M:1..1"/>
    <cims:isFixed rdfs:Literal="10"/>
    <rdfs:comment rdf:parseType="Literal">Positive sequence series resistance of the entire line section.</rdfs:comment>
    <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Property"/>
  </rdf:Description>
  ...
</rdf>

如何获取xml基础(即xml:base="http://iec.ch/TC57/2013/CIM-schema-cim16"表示的值?

0 个答案:

没有答案