1

Can i describe in xsd that some elements of a particular type must appear, but they can have any name. Visual studio don't allows me to omit the 'name' attribute, like this:

<xs:element type="myType"/>

Is there a way to do this? And if no, is it's possible in other XML schema languages, such as RELAX NG? Thanks

RoadBump
  • 733
  • 7
  • 16

1 Answers1

2

One way in XSD 1.0 is to use substitution groups.

BaseAny.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="anyOfCType" type="myCType" abstract="true"/>
    <xsd:complexType name="myCType">
        <xsd:sequence>
            <xsd:element name="something" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="anyOfSType" type="mySType" abstract="true"/> 
    <xsd:simpleType name="mySType">
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="2"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="anyOfCType"/>
                <xsd:element ref="anyOfSType"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

RefBaseAny.xsd:

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/1/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/1/XMLSchema.xsd" xmlns:b="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:import namespace="http://tempuri.org/XMLSchema.xsd" schemaLocation="BaseAny.xsd"/>

    <xsd:element name="c.one" type="b:myCType" substitutionGroup="b:anyOfCType"/>
    <xsd:element name="s.one" type="b:mySType" substitutionGroup="b:anyOfSType"/>

</xsd:schema>

Sample XML which validates against RefBaseAny.xsd:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:r="http://tempuri.org/1/XMLSchema.xsd">
    <r:c.one>
        <something></something>
    </r:c.one>
    <r:s.one>12</r:s.one>
</root>

With Relax NG you will have to define a named pattern to define your content model (mimic the xsd:complexType).

<define name="myType">
  ...
</define>

Then you define an element that has this structure:

<element>
  <anyName/>
  <ref name="myType"/>
</element>

Test.rng:

<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
    <start>
        <element>
            <anyName/>
            <ref name="myType"/>
        </element>
    </start>
    <define name="myType">
        <element name="c.one">
            <interleave>
                <attribute name="id">
                    <value>AA</value>
                </attribute>
                <attribute name="ref">
                    <text/>
                </attribute>
            </interleave>
            <element name="s.two">
                <text/>
            </element>
        </element>
    </define>
</grammar>

Valid XML1:

<?xml version="1.0" encoding="UTF-8"?>
<Data>
    <c.one id="AA" ref="123">
        <s.two>Hello</s.two>
    </c.one>
</Data>

Valid XML2:

<?xml version="1.0" encoding="UTF-8"?>
<Data1>
    <c.one id="AA" ref="123">
        <s.two>Hello</s.two>
    </c.one>
</Data1>
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • Thank you very much sir, the RELAX NG solution is exactly what i need. But is this not possible in XSD to give ANY name? I find XSD to be very limited. They thought first on the implementors of XSD, and not on the users... – RoadBump Apr 15 '12 at 14:02
  • @RoadBump, with XSD 1.0, any name is supported in the form of wildcard (xsd:any for elements, xsd:anyAttribute for attributes); however, one can only place restrictions related to the namespace that qualifies and the processing model for validation - for sure nothing related to the type as you requested. There is a new spec, recently approved, XSD 1.1; at this time I am not commenting on that, if only because there is no cheap parser available for everyone to use... – Petru Gardea Apr 15 '12 at 14:39
  • @PetruGardea can you check this http://stackoverflow.com/questions/37851057/xsd-for-no-attributes-and-no-self-closing please – Imran Qadir Baksh - Baloch Jun 16 '16 at 06:46