Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD: Define an element with any name

Tags:

xml

xsd

msxml

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)
like image 543
GSerg Avatar asked Jan 18 '10 09:01

GSerg


People also ask

How can an element be defined within an XSD?

Each element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.

Which schema tag allows to specify elements in any order?

<xsd:all> Element Allows the elements in the group to appear (or not appear) in any order in the containing element.

Which element is used to indicate that elements defined in the XSD may appear in any order in the XML file?

The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.

What is the difference between element and attribute in XSD?

An element is an XML node - and it can contain other nodes, or attributes. It can be a simple type or a complex type. It is an XML entity. An attribute is a descriptor.


1 Answers

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.

like image 81
Filburt Avatar answered Nov 13 '22 15:11

Filburt