Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

xsd - Dependent naming of XML element in Xml Schema

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...