Search code examples
javaxmljaxbxsdbufferedreader

extract element name values from XSD and import into excel


I have an XSD. I would like to extract the values of "element name=", and write them onto an excel sheet. How do i do this?

The ones in my mind are, bufferedreader/XPATH/jaxb/Apache POI. But I haven't used any of these before. Please help!

Sample input:

<?xml version="1.0" ?> 
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gw="XX" xmlns="XXXX" targetNamespace="XXXXX" elementFormDefault="qualified" xmlns:ns0="XXXXXX" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1">
    <xsd:annotation>
        <xsd:appinfo>
            <jaxb:schemaBindings>
                <jaxb:package name="XXXXXXXX" /> 
            </jaxb:schemaBindings>
        </xsd:appinfo>
    </xsd:annotation>

<xsd:import namespace="XXXXXXXX" /> 
<xsd:element name="sample" type="sample" nillable="true" /> 
<xsd:complexType name="sample">
    <xsd:sequence>
        <xsd:element name="tempTerms" minOccurs="0" nillable="true">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="Input" minOccurs="0" maxOccurs="unbounded" nillable="true" type="ns0:tempTerm" /> 
                </xsd:sequence>
            </xsd:complexType>
    </xsd:element>
  <xsd:element name="PatternCode" minOccurs="0" nillable="true" type="xsd:string" gw:type="java.lang.String" /> 
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

sample output:

an XLS where the column names are:

sample
tempTerms
Input
PatternCode

Solution

  • The XPath 2.0 expression distinct-values(//xs:element/@name) will give you the distinct element names in the schema document. You need to ensure that the XPath processor is set up with the namespace binding xs=http://www.w3.org/2001/XMLSchema.

    In general the result is fairly meaningless. It excludes names of elements in included and imported schema documents, and there may be multiple local element declarations with the same name and different definitions. But it's what you wanted, and if it's not useful, that's your problem not mine.