Reputation: 1
I want to get the datatypes of an XSD. It have to be static, cause the XSD could be variate. In my case I know the element names.
Small sample of XSD, but it could be go deeper:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:float" name="OrderNo"/>
<xs:element type="xs:string" name="OrderDate"/>
<xs:element type="xs:string" name="Name"/>
<xs:element type="xs:float" name="NameNo"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I tried with org.apache.xerces.xs
but don´t know how to get the Object XSParticleDecl
with Interface XSElementDeclaration
from root Elem
I expect the output for each individual element name to be the type.
Upvotes: 0
Views: 812
Reputation: 163282
It's generally best to work with a schema processor that gives you some kind of access to the "schema component model" rather than working directly with source XSD documents.
The Xerces schema API is one approach but I don't have experience with that and can't help you with it.
Saxon offers two alternatives:
(a) you can generate an SCM file representing the compiled schema. This is an XML file, so it can be processed easily using XSLT or XQuery.
(b) there's a set of extension functions, starting with saxon:schema() that allow you to explore the schema directly from XPath.
I would personally work with an SCM file. The SCM file for your schema is:
<?xml version="1.0" encoding="UTF-8"?>
<scm:schema xmlns:scm="http://ns.saxonica.com/schema-component-model"
generatedAt="2019-08-22T18:14:23.59+01:00"
xsdVersion="1.1">
<scm:element id="C0"
name="Order"
type="C1"
global="true"
nillable="false"
abstract="false"/>
<scm:complexType id="C1"
base="#anyType"
derivationMethod="restriction"
abstract="false"
variety="element-only">
<scm:modelGroupParticle minOccurs="1" maxOccurs="1">
<scm:sequence>
<scm:elementParticle minOccurs="1" maxOccurs="1" ref="C2"/>
<scm:elementParticle minOccurs="1" maxOccurs="1" ref="C3"/>
<scm:elementParticle minOccurs="1" maxOccurs="1" ref="C4"/>
<scm:elementParticle minOccurs="1" maxOccurs="1" ref="C5"/>
</scm:sequence>
</scm:modelGroupParticle>
<scm:finiteStateMachine initialState="0">
<scm:state nr="0">
<scm:edge term="C2" to="1"/>
</scm:state>
<scm:state nr="1">
<scm:edge term="C3" to="2"/>
</scm:state>
<scm:state nr="2">
<scm:edge term="C4" to="3"/>
</scm:state>
<scm:state nr="3">
<scm:edge term="C5" to="4"/>
</scm:state>
<scm:state nr="4" final="true"/>
</scm:finiteStateMachine>
</scm:complexType>
<scm:element id="C2"
name="OrderNo"
type="#float"
global="false"
containingComplexType="C1"
nillable="false"
abstract="false"/>
<scm:element id="C3"
name="OrderDate"
type="#string"
global="false"
containingComplexType="C1"
nillable="false"
abstract="false"/>
<scm:element id="C4"
name="Name"
type="#string"
global="false"
containingComplexType="C1"
nillable="false"
abstract="false"/>
<scm:element id="C5"
name="NameNo"
type="#float"
global="false"
containingComplexType="C1"
nillable="false"
abstract="false"/>
</scm:schema>
<?Σ 954c7f5b?>
Built-in types are represented using, for example, type="#float", whereas user-defined types will be represented by a reference such as type="C89"
where C89 is a reference to the @id
attribute of an scm:simpleType
or scm:complexType
child of the scm:schema
element.
Upvotes: 1