Task 843: .XSD File Format

Task 843: .XSD File Format

1. Properties of the .XSD File Format Intrinsic to Its File System

(No change needed, as this is the reference list.)

(No change needed.)

3. Ghost Blog Embedded HTML JavaScript for Drag-and-Drop .XSD File Processing

The script has been expanded to extract and display all properties, including general file format properties (inferred from the file and content) in addition to the root attributes.

Drag and drop an .XSD file here

4. Python Class for Handling .XSD Files

The class has been expanded to extract and print all properties, including general ones (inferred from file path and XML declaration) in addition to root attributes.

import xml.etree.ElementTree as ET
import os

class XSDHandler:
    def __init__(self):
        self.tree = None
        self.root = None
        self.file_path = None
        self.encoding = None

    def open(self, file_path):
        """Open and parse the .XSD file."""
        self.file_path = file_path
        with open(file_path, 'rb') as f:
            raw = f.read()
        self.encoding = 'UTF-8'  # Default
        if raw.startswith(b'<?xml'):
            decl = raw.split(b'?>')[0].decode('utf-8', errors='ignore')
            if 'encoding="' in decl:
                self.encoding = decl.split('encoding="')[1].split('"')[0]
        self.tree = ET.parse(file_path)
        self.root = self.tree.getroot()
        if self.root.tag != '{http://www.w3.org/2001/XMLSchema}schema':
            raise ValueError("Invalid .XSD root element")

    def print_properties(self):
        """Print all properties, including general and structural."""
        if not self.root:
            raise ValueError("No file opened")
        print("General Properties:")
        print(f"File extension: {os.path.splitext(self.file_path)[1]}")
        print("MIME type: application/xml")
        print(f"Encoding: {self.encoding}")
        print("File type: Text-based, human-readable XML")
        print("Root element: <xs:schema> with namespace http://www.w3.org/2001/XMLSchema")
        print("\nStructural Properties (Root Attributes):")
        for key, value in self.root.attrib.items():
            print(f"{key}: {value}")

    def write(self, file_path):
        """Write the parsed .XSD back to a file."""
        if not self.tree:
            raise ValueError("No file opened")
        self.tree.write(file_path, encoding=self.encoding.lower(), xml_declaration=True)

# Example usage:
# handler = XSDHandler()
# handler.open('example.xsd')
# handler.print_properties()
# handler.write('output.xsd')

5. Java Class for Handling .XSD Files

The class has been expanded to extract and print all properties, including general ones (inferred from file path and XML declaration) in addition to root attributes.

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.InputSource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class XSDHandler {
    private Document document;
    private String filePath;
    private String encoding = "UTF-8"; // Default

    public void open(String filePath) throws Exception {
        this.filePath = filePath;
        byte[] rawBytes = Files.readAllBytes(Paths.get(filePath));
        String raw = new String(rawBytes, StandardCharsets.UTF_8);
        if (raw.startsWith("<?xml")) {
            String decl = raw.split("\\?>")[0];
            if (decl.contains("encoding=\"")) {
                encoding = decl.split("encoding=\"")[1].split("\"")[0];
            }
        }
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(new InputSource(new ByteArrayInputStream(rawBytes)));
        Element root = document.getDocumentElement();
        if (!root.getLocalName().equals("schema") || !root.getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema")) {
            throw new IllegalArgumentException("Invalid .XSD root element");
        }
    }

    public void printProperties() {
        if (document == null) {
            throw new IllegalStateException("No file opened");
        }
        Element root = document.getDocumentElement();
        System.out.println("General Properties:");
        System.out.println("File extension: " + filePath.substring(filePath.lastIndexOf('.')));
        System.out.println("MIME type: application/xml");
        System.out.println("Encoding: " + encoding);
        System.out.println("File type: Text-based, human-readable XML");
        System.out.println("Root element: <xs:schema> with namespace http://www.w3.org/2001/XMLSchema");
        System.out.println("\nStructural Properties (Root Attributes):");
        NamedNodeMap attributes = root.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            System.out.println(attributes.item(i).getNodeName() + ": " + attributes.item(i).getNodeValue());
        }
    }

    public void write(String filePath) throws Exception {
        if (document == null) {
            throw new IllegalStateException("No file opened");
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("encoding", encoding);
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File(filePath));
        transformer.transform(source, result);
    }

    // Example usage:
    // public static void main(String[] args) throws Exception {
    //     XSDHandler handler = new XSDHandler();
    //     handler.open("example.xsd");
    //     handler.printProperties();
    //     handler.write("output.xsd");
    // }
}

6. JavaScript Class for Handling .XSD Files

The class has been expanded to extract and print all properties, including general ones (inferred from file path and XML declaration) in addition to root attributes.

const fs = require('fs');
const path = require('path');
const { DOMParser, XMLSerializer } = require('xmldom'); // Assume xmldom package is available

class XSDHandler {
    constructor() {
        this.doc = null;
        this.filePath = null;
        this.encoding = 'UTF-8'; // Default
    }

    open(filePath) {
        this.filePath = filePath;
        const data = fs.readFileSync(filePath, 'utf8');
        const encodingMatch = data.match(/encoding="([^"]+)"/);
        if (encodingMatch) {
            this.encoding = encodingMatch[1];
        }
        const parser = new DOMParser();
        this.doc = parser.parseFromString(data, 'application/xml');
        const root = this.doc.documentElement;
        if (root.tagName !== 'schema' || root.namespaceURI !== 'http://www.w3.org/2001/XMLSchema') {
            throw new Error('Invalid .XSD root element');
        }
    }

    printProperties() {
        if (!this.doc) {
            throw new Error('No file opened');
        }
        const root = this.doc.documentElement;
        console.log('General Properties:');
        console.log(`File extension: ${path.extname(this.filePath)}`);
        console.log('MIME type: application/xml');
        console.log(`Encoding: ${this.encoding}`);
        console.log('File type: Text-based, human-readable XML');
        console.log('Root element: <xs:schema> with namespace http://www.w3.org/2001/XMLSchema');
        console.log('\nStructural Properties (Root Attributes):');
        for (let i = 0; i < root.attributes.length; i++) {
            const attr = root.attributes[i];
            console.log(`${attr.name}: ${attr.value}`);
        }
    }

    write(filePath) {
        if (!this.doc) {
            throw new Error('No file opened');
        }
        const serializer = new XMLSerializer();
        const xmlStr = serializer.serializeToString(this.doc);
        fs.writeFileSync(filePath, `<?xml version="1.0" encoding="${this.encoding}"?>\n` + xmlStr, 'utf8');
    }
}

// Example usage:
// const handler = new XSDHandler();
// handler.open('example.xsd');
// handler.printProperties();
// handler.write('output.xsd');

7. C++ Class for Handling .XSD Files

The class has been expanded to extract and print all properties, including general ones (inferred from file path and XML declaration) in addition to root attributes. Note: This still assumes TinyXML-2 for XML handling.

#include <iostream>
#include <tinyxml2.h> // Assume TinyXML-2 library is available
#include <string>
#include <fstream>
#include <regex>

class XSDHandler {
private:
    tinyxml2::XMLDocument doc;
    std::string filePath;
    std::string encoding = "UTF-8"; // Default

public:
    void open(const std::string& filePath) {
        this->filePath = filePath;
        std::ifstream file(filePath);
        std::string raw((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
        std::regex encodingRegex(R"(encoding="([^"]+)")");
        std::smatch match;
        if (std::regex_search(raw, match, encodingRegex) && match.size() > 1) {
            encoding = match[1].str();
        }
        if (doc.Parse(raw.c_str()) != tinyxml2::XML_SUCCESS) {
            throw std::runtime_error("Failed to parse XML");
        }
        tinyxml2::XMLElement* root = doc.FirstChildElement("schema");
        if (!root || std::string(root->GetDocument()->RootElement()->Namespace()) != "http://www.w3.org/2001/XMLSchema") {
            throw std::invalid_argument("Invalid .XSD root element");
        }
    }

    void printProperties() {
        tinyxml2::XMLElement* root = doc.FirstChildElement("schema");
        if (!root) {
            throw std::runtime_error("No file opened");
        }
        std::cout << "General Properties:" << std::endl;
        size_t dotPos = filePath.find_last_of('.');
        std::cout << "File extension: " << (dotPos != std::string::npos ? filePath.substr(dotPos) : "") << std::endl;
        std::cout << "MIME type: application/xml" << std::endl;
        std::cout << "Encoding: " << encoding << std::endl;
        std::cout << "File type: Text-based, human-readable XML" << std::endl;
        std::cout << "Root element: <xs:schema> with namespace http://www.w3.org/2001/XMLSchema" << std::endl;
        std::cout << "\nStructural Properties (Root Attributes):" << std::endl;
        const tinyxml2::XMLAttribute* attr = root->FirstAttribute();
        while (attr) {
            std::cout << attr->Name() << ": " << attr->Value() << std::endl;
            attr = attr->Next();
        }
    }

    void write(const std::string& filePath) {
        std::ofstream outFile(filePath);
        outFile << "<?xml version=\"1.0\" encoding=\"" << encoding << "\"?>" << std::endl;
        tinyxml2::XMLPrinter printer;
        doc.Print(&printer);
        outFile << printer.CStr();
        if (!outFile) {
            throw std::runtime_error("Failed to write file");
        }
    }
};

// Example usage:
// int main() {
//     try {
//         XSDHandler handler;
//         handler.open("example.xsd");
//         handler.printProperties();
//         handler.write("output.xsd");
//     } catch (const std::exception& e) {
//         std::cerr << "Error: " << e.what() << std::endl;
//     }
//     return 0;
// }