Programmatically accessing schema
I went through the documentation and couldn't find any hints on this. And I am not sure what keywords to search on google. Is there a way to programmatically (or api?) query the XSD for a specific element to get its spec? For example, I would like to know if /MyContainer/Container2 can be repeated. Or if /MyContainer/Container1/Item1/ text() is optional. my.xml```xml<?xml version="1.0" encoding="UTF-8"?><MyContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sample.xsd"> <Container1> <Item1>String</Item1> </Container1> <Container2> <Item2>2014-12-17T09:30:47Z</Item2> </Container2> <Container2> <Item2>2015-01-17T09:30:47Z</Item2> </Container2></MyContainer>``` sample.xsd```<?xml version="1.0" encoding="UTF-8"?><xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="MyContainer"> <xs:annotation> <xs:documentation>docs</xs:documentation> </xs:annotation> <xs:complexType> <xs:sequence> <xs:element name="Container1"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="Item1" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element maxOccurs="unbounded" ref="Container2"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Container2"> <xs:complexType> <xs:sequence> <xs:element name="Item2" type="xs:dateTime"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>``` cheers,Aaron
Brian Pytlik Zillig at the Centre for Digital Research in the Humanities at the University of Nebraska-Lincoln has a program that can read an XML files and harvest its underlying schema. It's called "Abbot" because it was created some year ago as part of the MONK project, which is an acronym for Metadata Offer New Knowledge. Martin Mueller Professor emeritus of English and Classics Northwestern University From: Aaron Storm <aaron_storm@yahoo.com<mailto:aaron_storm@yahoo.com>> Reply-To: Aaron Storm <aaron_storm@yahoo.com<mailto:aaron_storm@yahoo.com>> Date: Saturday, February 28, 2015 5:29 AM To: "lxml@lxml.de<mailto:lxml@lxml.de>" <lxml@lxml.de<mailto:lxml@lxml.de>> Subject: [lxml] Programmatically accessing schema I went through the documentation and couldn't find any hints on this. And I am not sure what keywords to search on google. Is there a way to programmatically (or api?) query the XSD for a specific element to get its spec? For example, I would like to know if /MyContainer/Container2 can be repeated. Or if /MyContainer/Container1/Item1/ text() is optional. my.xml ```xml <?xml version="1.0" encoding="UTF-8"?> <MyContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sample.xsd"> <Container1> <Item1>String</Item1> </Container1> <Container2> <Item2>2014-12-17T09:30:47Z</Item2> </Container2> <Container2> <Item2>2015-01-17T09:30:47Z</Item2> </Container2> </MyContainer> ``` sample.xsd ``` <?xml version="1.0" encoding="UTF-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="MyContainer"> <xs:annotation> <xs:documentation>docs</xs:documentation> </xs:annotation> <xs:complexType> <xs:sequence> <xs:element name="Container1"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="Item1" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element maxOccurs="unbounded" ref="Container2"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Container2"> <xs:complexType> <xs:sequence> <xs:element name="Item2" type="xs:dateTime"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> ``` cheers, Aaron
Aaron Storm schrieb am 28.02.2015 um 12:29:
I went through the documentation and couldn't find any hints on this. And I am not sure what keywords to search on google. Is there a way to programmatically (or api?) query the XSD for a specific element to get its spec? For example, I would like to know if /MyContainer/Container2 can be repeated. Or if /MyContainer/Container1/Item1/ text() is optional.
Similar questions have been discussed in the past. These are related, for example: http://thread.gmane.org/gmane.comp.python.lxml.devel/7318 http://thread.gmane.org/gmane.comp.python.lxml.devel/5619 In general, figuring out what an XML Schema specification allows is rather difficult. It can be done for simple cases (it's XML, you can search in it), but schemas can be arbitrarily complex. Sometimes there are multiple ways to express something, and covering all cases is cumbersome. Stefan
participants (3)
-
Aaron Storm
-
Martin Mueller
-
Stefan Behnel