1
votes

How can I check that the root element of an XML file has a specific name?

I can correctly assert whether the root has a specific attribute like this:

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
    <pattern id="1">
        <rule context="root_element">
            <assert test="@OBJID">Missing OBJID.</assert>
        </rule>
    </pattern>

In this case, I'd like to test that the root element is root_element.

For example, this should fail (but it doesn't):

<?xml version='1.0' encoding='UTF-8'?>
<mets OBJID="123">
    <Age>30</Age>
</mets>

For completeness, I am testing using unittest in python 3.

Any link to some documentation about this is appreciated.

1

1 Answers

1
votes

Use "/" to create a rule that fires on the document itself. Then assert the presence of an element named "root_element".

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
  <pattern id="1">
    <rule context="/">
      <assert test="root_element">Root element should be named "root_element".</assert>
    </rule>
  </pattern>
</schema>