8

Because of limitations of certain systems, we need to use XMLs that are formatted a bit inconveniently. Those we need to transform into a convenient form.

The question: how do I define in an XSD schema an element that has the following properties:

  • Does not have any children
  • Does not have any attributes
  • Has any name (that is what's causing problems)
GSerg
  • 76,472
  • 17
  • 159
  • 346

1 Answers1

7

You can use the <xsd:any /> element together with the Xml Schema Instance type attribute.

Schema

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence maxOccurs="unbounded">
                <xsd:any processContents="strict" namespace="##local"></xsd:any>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:simpleType name="st">
        <xsd:restriction base="xsd:string" />
    </xsd:simpleType>
</xsd:schema>

Test Xml instance

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <!-- valid -->
    <one xsi:type="st">value one</one>
    <emptyone xsi:type="st"/>

    <!-- invalid -->
    <two name="myname" xsi:type="st">value two</two>

    <!-- invalid -->
    <three xsi:type="st">
        <four xsi:type="st">value four</four>
    </three>
</root>

Conclusion

You cannot enforce a simple type in the xsd schema alone.

bernhof
  • 6,219
  • 2
  • 45
  • 71
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • No I cannot. xsd:any will allow arbitrary structure whereas I want the any-name element(s) to not have children or attributes. – GSerg Jan 18 '10 at 17:23
  • In that case it looks like your requirements are mutually exclusive. – Filburt Jan 18 '10 at 18:19
  • Why? They are not, AFAIC. Any value element is eligible, regardless actual text that takes place of "node", whereas any is not. – GSerg Jan 18 '10 at 21:47
  • Yes they are because there obviously doesn't exist a way to restrict a any-name element. – Filburt Jan 28 '10 at 18:31
  • Ok, this is probably the closest one can have. Despite I've no control over those xmls so I can't make them have an attribute. – GSerg Feb 05 '10 at 21:01