Skip to content

Instantly share code, notes, and snippets.

@vadimii
Created November 23, 2012 00:26
Show Gist options
  • Save vadimii/4133444 to your computer and use it in GitHub Desktop.
Save vadimii/4133444 to your computer and use it in GitHub Desktop.
Validate and transform XML with lxml
from lxml import etree
with open('./sportobj.xsd') as f:
schema = etree.XMLSchema(etree.parse(f))
with open('./sportobj-sample.xml') as f:
sample = etree.parse(f)
with open('./sportobj.xsl') as f:
transform = etree.XSLT(etree.parse(f));
print(schema.validate(sample))
result = transform(sample)
binc = etree.tostring(result, pretty_print=True, encoding='utf-8')
strc = binc.decode('utf-8')
print(strc)
# with open('./sportobj.html', 'w') as f:
# f.write(strc)
<div>
<b>Негосударственное учреждение культурно-спортивный реабилитационный комплекс Всероссийского Общества Слепых&nbsp;
[273]
</b>
<br/>
<span>Москва, улица Куусинена, 19а</span>
<ul>
<li>Спортивный комплекс</li>
<li>Хозяйственное ведение</li>
</ul>
</div>
<SportObj oid="273">
<Name>Негосударственное учреждение культурно-спортивный реабилитационный комплекс Всероссийского Общества Слепых</Name>
<Address>Москва, улица Куусинена, 19а</Address>
<Description>
<Facility>Спортивный комплекс</Facility>
<Usage>Хозяйственное ведение</Usage>
</Description>
<Entity_Spatial>
<Spatial_Element>
<Spelement_Unit Type_Unit="Точка" Su_Nmb="1">
<Ordinate X="55.787589" Y="37.510960" Ord_Nmb="1"/>
</Spelement_Unit>
</Spatial_Element>
</Entity_Spatial>
</SportObj>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">
<xs:element name="SportObj" type="SportObjType"/>
<xs:complexType name="SportObjType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Address" type="xs:string"/>
<xs:element name="Description" type="DescriptionType"/>
<xs:element name="Entity_Spatial" type="Entity_SpatialType"/>
</xs:sequence>
<xs:attribute name="oid" type="xs:int" use="required"/>
</xs:complexType>
<xs:complexType name="DescriptionType">
<xs:sequence>
<xs:element name="Facility" type="xs:string" minOccurs="0"/>
<xs:element name="Usage" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Entity_SpatialType">
<xs:sequence>
<xs:element name="Spatial_Element" type="Spatial_ElementType" nillable="true" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Spatial_ElementType">
<xs:sequence>
<xs:element name="Spelement_Unit" type="Spelement_UnitType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Spelement_UnitType">
<xs:sequence>
<xs:element name="Ordinate" type="OrdinateType" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="Type_Unit" type="spelementTypeUnitType"/>
<xs:attribute name="Su_Nmb" type="xs:int"/>
</xs:complexType>
<xs:simpleType name="spelementTypeUnitType">
<xs:restriction base="xs:string">
<xs:enumeration value="Точка"/>
<xs:enumeration value="Линия"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="OrdinateType">
<xs:attribute name="X" type="xs:double"/>
<xs:attribute name="Y" type="xs:double"/>
<xs:attribute name="Ord_Nmb" type="xs:int"/>
</xs:complexType>
</xs:schema>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" media-type="text/html"/>
<xsl:template match="SportObj">
<div>
<b>
<xsl:value-of select="Name"/>
<xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>
[<xsl:value-of select="./@oid"/>]
</b>
<br/>
<span><xsl:value-of select="Address"/></span>
<ul>
<xsl:for-each select="Description/*">
<li><xsl:value-of select="text()"/></li>
</xsl:for-each>
</ul>
</div>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment