Dependent naming of XML element in Xml Schema

眉间皱痕 提交于 2021-02-11 08:10:52

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/40929963/dependent-naming-of-xml-element-in-xml-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!