Create well-formed XML and schema efficiently

Posted on by Rachel Appel

Most companies, both large and small still use XML, even if JSON has overtaken it in popularity. However, many are using it, for example to exchange data, or have configuration files that can contain comments (unlike JSON). Whatever your reason for using XML, Rider contains many great XML features that enable you to work with it more efficiently. In this blog post, we’ll look at using Rider to create and maintain perfectly well-formed XML, schemas, and more. Fortunately for us, Rider contains many great XML features that enable you to work with it more efficiently. In this blog post, we’ll look at using Rider to create and maintain perfectly well-formed XML, schemas, and more.

XML Overview

XML is a markup language that describes data, and is meant to be both human and machine readable. It allows developers to structure text-based schema, usually to share with a vendor, partner, other system, etc… XML has enjoyed longevity as a markup language for many years now, and although JSON has overtaken it in popularity, XML is still used every day.

Work with XML elements and data

You might be crafting XML to match an existing database schema, or the schematic requirements of a customer, vendor, or partner. Alternatively, you might be creating XML from scratch. Either way, Rider has you covered, with dozens of intentions, autocompletions, and other features to make XML element creation go smoothly. A bonus for web developers: Because HTML has a tag-based syntax, many of the intention actions and refactorings in this blog post work for HTML too!

Renaming code, XML, or other objects is a daily task of any programmer, regardless of language or technology. Rider contains rename refactorings for just about everything, including XML elements and attributes. For example, if you’re working on a restaurant’s online menu, you might have some renaming to do as the menu evolves. Rider enables you to rename a single element or attribute, or each instance of an element or attribute. To rename each instance of an attribute, for example, use Rename Element. If you choose only the Rename option, it will only rename the individual attribute at the cursor position.

Rename attribute

Often, creating XML elements means moving tags and attributes around, so they make better sense. Converting tags to attributes or attributes to tags is often necessary. Using our menu as an example, it seems better to convert the name element to an attribute of a menu item. To convert a tag to an attribute, or vice versa, use Alt+Enter.

Convert tag to attribute

Conversely, converting attributes to tags and making them a child element is also a frequent but necessary activity when structuring XML.

Convert attribute to child element

It’s easy to miss something while combing through miles of XML! When you need to insert a tag around all instances of another tag, use Ctrl+Shift+R and choose the Wrap Tag refactoring. The example menu is missing some nutrition information, so wrap the calories tag with it.

Wrap tag

Continuing with the example, allergy information should be added inside all nutrition-information elements. Use Ctrl+Shift+R again, but this time, to invoke the Add Subtag refactoring.

Add subtag

Rider has so many more ways to work with XML data. Open any XML document and use Ctrl+Shift+R (or Refactor | Refactor This) to see a list of all the helpful XML refactoring features available. If you don’t see a refactoring, place your cursor on an element and use Alt+Enter for contextual suggestions.

Validate XML Documents

Rider performs automatic validation for well-formedness of XML while you type XML entries. However, you may want to do your own manual validation to double check things, since Rider doesn’t validate every detail such as structure, until the XML file is linked to an XSD or DTD. Therefore, Rider presents several options for validating XML and creating DTD (Document Type Definitions) and schema files, located in the Tools | XML Actions menu, or by right clicking in any editor window containing an open .xml file. Being able to generate schema means you can form and use your XML to dictate how the structure looks.

Generate DTD

Alternatively, you may want to start by creating a schema file then generating template XML from it.

Creating valid XML means that you may have to create portions of the file that are designated as simple character data, rather than a defined type of data. That’s where CDATA comes into play. CDATA is defined as blocks of text that are not parsed by the parser, but are otherwise recognized as markup. Some examples for using CDATA are when XML documents have text that contain characters such as < and >, or &. Use Alt+Enter to switch between CDATA and text.

Convert CDATA to text

As an alternative to CDATA, Rider can convert special characters into their HTML character reference codes &. <, > Using Alt+Enter, you can switch between character codes and the characters.

Convert special characters to numeric

To properly validate documents, you must work with namespaces in XML. There are many public namespaces that define generic schema. For example, http://www.w3.org/2001/XMLSchema is the namespace that defines basic types. You’ll also use your own namespaces, whether publicly or privately hosted. Your namespaces map to one of your schema or DTD files, and you must link your XML documents to those namespaces for proper validation.

Namespaces are important concepts in XML, as they denote virtual locations that often map to physical locations that contain definitions of data types for XML documents. If you use a namespace identifier in elements without a namespace declaration, Rider recognizes that and marks the namespace as unidentified. Use Alt+Enter to add the namespace.

Add a namespace

XPath and XSLT

XPath (XML Path Language) and XSLT (Extensible Stylesheet Language Transformations) are two complimentary languages that work with XML. XPath is a query language used to select nodes in an XML document. XSLT is a way to format XML for presentation. These are often used together along with XML and its schema. Rider contains features to help you make the most of XPath and XSLT as well as all the great features for XML.

XPath uses a pattern matching syntax, to match nodes or node values in an XML or XSLT document. To easily find a path that might be queried often, Rider has a View Unique XPath option. Use View | Unique XPath from the menu or Ctrl+Alt+X,P.

Find the unique Xpath path

Similarly, you can easily search through mountains of XML by using an XPath expression. Go with Ctrl+Alt+X,E or use the Edit menu, then Find | Find by XPath.

Find a node using XPath

Summary

Rider contains everything you need to craft high quality, well formed, XML documents, schemas, and DTDs. Even working with XPath and XSLT is more simple with Rider. So download Rider and let us know how you use it to manage all things XML.

Subscribe

Subscribe to .NET Tools updates