开发者

xs:unique and xs:substitutionGroup

开发者 https://www.devze.com 2023-01-31 00:01 出处:网络
I have a xs:unique declaration in a schema. It\'s work well. But when I 开发者_如何学Csubstitute an element wich is the key, it\'s doesn\'t work any more.

I have a xs:unique declaration in a schema. It's work well. But when I 开发者_如何学Csubstitute an element wich is the key, it's doesn't work any more.

Is there something to ensure that the unique key persists with substitution ?

For instance, I have this xml :

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <el id="1"/>
  <el id="2"/>
</root>

and this schema :

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="typeel">
      <xs:attribute name="id"/>
    </xs:complexType>

  <xs:element name="el" type="typeel"/>

  <xs:element name="root">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element ref="el"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="idgoooood">
      <xs:selector xpath="el"/>
      <xs:field xpath="@id"/>
    </xs:unique>
  </xs:element>
</xs:schema>

That's work very good.

But, if I add in schema :

  <xs:element name="el-bis" type="typeel"  substitutionGroup="el"/>

I can write my xml :

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <el id="1"/>
  <el id="2"/>
  <el-bis id="3"/>
</root>

Very good. But, unfortunately, I can write also :

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <el id="1"/>
  <el id="2"/>
  <el-bis id="2"/>
</root>

I don't wwant that. I should want the unique key persists across substitution... Is it possible ? If not, what workarounds ?

Thanks.


I think this is not possible. You have to copy the unique declaration to the substituting element.


This is probably the oldest question that I have answered, consequently i'm reasonably sure that you no longer care about a solution, but in case someone else like me comes along looking for an answer here you go...

You were close you just needed to alter the selector in your unique constraint to match all not just el, then the constraint will be applied no matter which element you use in your substitutionGroup.

<xs:unique name="idgoooood">
  <xs:selector xpath="*"/>
  <xs:field xpath="@id"/>
</xs:unique>

Full Xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="typeel">
      <xs:attribute name="id"/>
    </xs:complexType>

  <xs:element name="el" type="typeel"/>

  <xs:element name="el-bis" type="typeel"  substitutionGroup="el"/>

  <xs:element name="root">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element ref="el"/>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="idgoooood">
      <xs:selector xpath="*"/>
      <xs:field xpath="@id"/>
    </xs:unique>
  </xs:element>
</xs:schema>

Example Xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <el id="1"/>
  <el id="2"/>
  <el-bis id="2" /> <!-- Fails due to duplication -->
</root>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号