1

Assuming that I have this XML:

<parameters>
  <parameter type="string" isVisible="True" optional="False" id="DealerCode">
     <DealerCode>ABCDEF001</DealerCode>
  </parameter>
</parameters>

I used a Xml-Schema generator to generate me a basic one. This results in this:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="DealerCode" type="xs:string"/>
  <xs:element name="parameter">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="DealerCode"/>
      </xs:sequence>
      <xs:attribute type="xs:string" name="type"/>
      <xs:attribute type="xs:string" name="isVisible"/>
      <xs:attribute type="xs:string" name="optional"/>
      <xs:attribute type="xs:string" name="id"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="parameters">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="parameter"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

The naming of the element DealerCode is dependent to the identifier of the parameter element. For example if the parameter has the identifier ZIP, the sub-element of parameter should also be named that way. How can I achieve this in the Xml-Schema?

Example:

<parameter type="string" isVisible="True" optional="False" id="DealerCode">
    <DealerCode>ABCDEF001</DealerCode>
</parameter>

<parameter type="string" isVisible="True" optional="False" id="ZIP">
    <ZIP>ABCDEF001</ZIP>
</parameter>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
rbr94
  • 2,227
  • 3
  • 23
  • 39

1 Answers1

3

XSD 1.0

Not possible. Options:

  1. Use xs:any and check out-of-band with respect to XSD.
  2. Redesign your XML by replacing the generic parameter with the specific element it wraps:

    <DealerCode type="string" isVisible="True"
                optional="False">ABCDEF001</DealerCode>
    

XSD 1.1

Possible using xs:assert:

      <xs:assert test="*/local-name() = @id"/>

Here it is in context of your complete XSD, which will successfully validate your XML:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">

  <xs:element name="DealerCode" type="xs:string"/>
  <xs:element name="parameter">
    <xs:complexType>
      <xs:sequence>
        <xs:any/>
      </xs:sequence>
      <xs:attribute type="xs:string" name="type"/>
      <xs:attribute type="xs:string" name="isVisible"/>
      <xs:attribute type="xs:string" name="optional"/>
      <xs:attribute type="xs:string" name="id"/>
      <xs:assert test="*/local-name() = @id"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="parameters">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="parameter" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks for your reply! Could you add a sample how I can achieve this using `xs:assert`, please? – rbr94 Dec 02 '16 at 11:35
  • Answer updated to show full XSD 1.1 `xs:assert` example. – kjhughes Dec 02 '16 at 11:44
  • This does not work, I used a validator and it gives me this error: `S4s-elt-invalid-content.1: The Content Of '#AnonType_parameter' Is Invalid. Element 'assert' Is Invalid, Misplaced, Or Occurs Too Often` – rbr94 Dec 02 '16 at 12:37
  • I will try the version for 1.0 first, because I noticed that Visual Studio only supports 1.0. – rbr94 Dec 02 '16 at 12:47
  • 1
    **It does work, but it requires XSD 1.1, [which you said you had](http://stackoverflow.com/questions/40929963/dependent-naming-of-xml-element-in-xml-schema/40931226?noredirect=1#comment69073527_40929963).** That error indicates that your XSD processor is XSD 1.0. – kjhughes Dec 02 '16 at 12:47