0

I've a XSD file along with the XML, the schema to which I'm not aware of. You could say that XSD is the schema for the XML present but it's somewhat not very understanding to me how to proceed.

During my search I stumbled into scalaxb package prescribed to achieve this requirement. Could someone please help me in achieving that; I haven't done this kind of xsd processing before so kind of new to this. Although I got a small code to do validation of XML from XSD present, which seemed ok. But the extraction is something I'm stuck now.

Due to my limited understanding of XSD, I couldn't differentiate which element to be considered as the schema field & which shouldn't. Appreciate any help.

XSD File Snippet:

   <xs:element name="preList">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="nNumber" type="remNum"/>
            <xs:element name="fileName" type="FileName"/>
            <xs:element name="recordCnt" type="RecordCount"/>
            <xs:choice maxOccurs="unbounded">
               <xs:element ref="tfiws"/>
               <xs:element ref="structured"/>
            </xs:choice>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="tfiws">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="header" minOccurs="0"/>
            <xs:element name="tfMst" type="TFMsg"/>
            <xs:element name="turns" minOccurs="0">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="tDate" type="SDate" minOccurs="0"/>
                     <xs:element name="direction" type="Direction" minOccurs="0"/>
                     <xs:element name="mstAmount" type="MSTAmount" minOccurs="0"/>
                     <xs:element name="minDate" type="EDate" minOccurs="0"/>
                  </xs:sequence>
                  <xs:attributeGroup ref="RequiredAttrs"/>
               </xs:complexType>
                </xs:element>
...and so on.

Code for validation:

class validate {
  def validateXML(xmlFilePath: String, xsdFilePath: String): Boolean = {
    try{
      val factory: SchemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)

      val schema: Schema = factory.newSchema(new File(xsdFilePath))

      val validator: Validator = schema.newValidator()
      validator.validate(new StreamSource(new File(xmlFilePath)))
      true
    } 
    catch {
      case NonFatal(error) => error.printStackTrace()
      false
    }
  }
}
knowone
  • 840
  • 2
  • 16
  • 37

1 Answers1

0

I got the way around scalaxb. Best library for converting XSD to classes in scala. It creates 3 files from XSD: a scala code with traits & classes from XSD, scalaxb.scala(which I'm still trying to figure out why) & a corresponding XML example file. That's it. Once I got the segregated fields in class format, was easy to create the schema henceforth.

knowone
  • 840
  • 2
  • 16
  • 37