xmlxsdschemaxsd-validationxsd-1.1

How to add a custom attribute to a XSD-1.1 validated XML Element


I want to add custom attributes to XML-elements in a validated document without changeing the original *.xsd. The XSD is version 1.1. I use python xmlschema library for validation.

The XML looks similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<outer
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="root.xsd"
>

<inner custom:comment="do it now!" xmlns:custom="http://example.com/custom">
    <regular id="abc">Content</regular>
</inner>

</outer>

Currently the custom: element is rejected by xmlschema

Reason: '{http://example.com/custom}comment' attribute not allowed for element

Solution

  • Michael Kay's comment is correct. Just to provide a little more depth.

    I want to add custom attributes to XML-elements in a validated document without changing the original *.xsd.

    You can add anything you like into the XML document, but... when you do that, the document will no longer validate against the original schema. As you pointed out,

    the custom: element is rejected by the schema.

    That's as expected.

    You can modify the schema to accommodate the desired additional attributes. But that seems to be off the table, from the phrasing of your question.