https://www.alternetsoft.com/blog/xml-validation-and-codecompletion

XML validation and code completion

Learn how AlterNET Studio's XML parser uses schema-based validation and code completion to streamline XML editing and ensure accurate, error-free code.
18 Aug 2021 4 min read

XML text editing

Our code editor is widely used for editing XML text. Like other supported languages, XML editing in AlterNET Studio operates in conjunction with our parser components. These components perform syntax analysis, driving features such as syntax highlighting, formatting, and diagnostics.

While our XML parser previously offered basic features, it lacked essential capabilities found in popular XML formatters and validators: XSD-based schema validation and auto-completion.

Our latest update addresses these shortcomings by introducing schema-based validation and code completion for XML editing. These enhancements significantly improve the XML editing experience within our code editor, making it a more powerful and efficient tool for XML development.

XML parsing and validating

Our XML parser employs a similar approach to other advanced parsers: converting the text into an Abstract Syntax Tree (AST). For XML, the AST is relatively straightforward, consisting of elements like tags, tag parameters, processing instructions, and the body.

This syntax analysis provides valuable information for driving features like code outlining, XML formatting, and diagnosing incorrect syntax within XML documents.

XML documents are often bound to XML schemas, which formally describe the elements and their constraints. These schemas validate XML syntax, ensuring it adheres to the defined rules. Additionally, schemas can provide guidance on possible input values, significantly improving the efficiency of code editing.

xml validation and code completion

In previous releases, we introduced an Language Server protocol-based XML parser that incorporates all the features mentioned earlier. However, please note that this LSP server is implemented using Java, requiring users to have Java installed on their target computer.

XML code completion

We’re excited introduce the addition of XmlParserWithSchema to our code editor. This new XML parser leverages the System.Xml.Schema.XmlSchema class, ensuring it relies solely on the .NET Framework and introduces no additional dependencies.

Schema-based validation

XmlParserWithSchema attempts to automatically load an XML schema from the XML document if explicitly declared. Alternatively, developers can set the schema file or URL directly via the parser’s properties. Once loaded, the parser validates the XML document content against the schema, displaying any validation errors within the text editor.

We’ve also implemented auto-completion functionality within this parser. By identifying the corresponding XmlSchema type at the current cursor position, the parser suggests possible input values for elements and attributes. This feature significantly improves coding efficiency and accuracy.

Performance optimization

To enhance performance, especially when working with large XML documents, we’ve designed XmlParserWithSchema to operate in a separate thread. This allows for smoother editing and validation processes.

Current scope and future enhancements

While we currently support code completion for complex types, choice, and sequence elements, as well as enumerations for parameter values, we acknowledge that our coverage of the full XSD syntax and complex schema definitions may still be limited. We welcome your feedback on this new feature to help us identify areas for improvement and expansion.  

feature.