Question

I'd like to use a Relax NG schema to validate an XML file. As part of the validation, I'd like to make sure that there is no white space in a set of nodes. For example, I'd like for the first two <emptyCheck> nodes to validate, but the third and fourth ones to fail in this example:

<?xml version="1.0" encoding="UTF-8"?>
<testRoot>
    <emptyCheck/>
    <emptyCheck></emptyCheck>
    <emptyCheck> </emptyCheck>
    <emptyCheck>x</emptyCheck>
</testRoot>

The following Relax NG Schema almost works. The above sample XML fails for the fourth instance of <emptyCheck>, but not for the third.

<?xml version="1.0" encoding="UTF-8"?>
<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0">
    <start>
        <element name="testRoot">
            <oneOrMore>
                <element name="emptyCheck">
                    <empty/>
                </element>
            </oneOrMore>
        </element>
    </start>
</grammar>

I recognize that in most XML processing the node with just white space won't really matter. I'd still like to solve this so I can enforce a coding style guide. So, is there a way to setup a Relax NG schema so that a document won't validate if a node has just white space in it when it should be completely empty?

Was it helpful?

Solution

It can seem quirky of RELAX NG to allow elements declared empty to contain whitespace, though the decision has a rational basis.

Anyway, if your RELAX NG validator supports XSD data types (most do), then you can use those to constrain the allowed element content further, by specifying that the element content must be a zero-length string:

<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
  datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <element name="testRoot">
      <oneOrMore>
        <element name="emptyCheck">
          <data type="string">
            <param name="maxLength">0</param>
          </data>
        </element>
      </oneOrMore>
    </element>
  </start>
</grammar>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top