How to extend a complex type in a different namespace without changing the name

I have an existing namespace that I cannot change. I need to add elements to a complex type so that the resulting XML looks like this:

Original XML:

<?xml version="1.0"?>
<Book xmlns="http://bookshop.com">
  <Author>Frank Herbert</Author>
  <Title>Dune</Title>
</Book>

New XML:

<?xml version="1.0"?>
<Book xmlns="http://bookshop.com" 
      xlmns:custom="http://custombookshop.com>
  <Author>Frank Herbert</Author>
  <Title>Dune</Title>
  <custom:Publisher>Ace</custom:Publisher>
</Book>

I have requirements for custom elements to be displayed as indicated above with a namespace prefix and that a name of a complex type should not change.

Here is the original XSD and the new XSD with which I tried using redefine. Will this work, or is there a better way to do this? Thanks in advance for your advice.

Original XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns="http://bookshop.com" 
           targetNamespace="bookshop.com">
  <xs:complexType name="Book">
    <xs:complexContent>
      <xs:sequence>
        <xs:element name="Author" type="String32" 
                    minOccurs="1" maxOccurs="1" />
        <xs:element name="Title" type="String32" 
                    minOccurs="1" maxOccurs="1" />
      </xs:sequence>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

My attempt at a new XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns="http://custombookshop.com" 
           targetNamespace="custombookshop.com">
  <xs:redefine schemaLocation="http://bookshop.com">
    <xs:complexType name="Book">
      <xs:complexContent>
        <xs:extension base="Book">
          <xs:sequence>
            <xs:element name="Publisher" type="String32" 
                        minOccurs="1" maxOccurs="1" />
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>
+4
source share
1 answer

sequence complexType:

<xs:complexType name="Book">
    <xs:sequence>
        <xs:element name="Author" type="String32" 
            minOccurs="1" maxOccurs="1" />
        <xs:element name="Title" type="String32" 
            minOccurs="1" maxOccurs="1" />
    </xs:sequence>
</xs:complexType>

targetNamespace xmlns elementFormDefault="qualified", . ( element simpleType) XML:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://bookshop.com" 
    targetNamespace="http://bookshop.com"
    elementFormDefault="qualified">

    <xs:complexType name="Book">
        <xs:sequence>
            <xs:element name="Author" type="String32" 
                minOccurs="1" maxOccurs="1" />
            <xs:element name="Title" type="String32" 
                minOccurs="1" maxOccurs="1" />
        </xs:sequence>
    </xs:complexType>

    <xs:element name="Book" type="Book"/>

    <xs:simpleType name="String32">
        <xs:restriction base="xs:string"></xs:restriction>
    </xs:simpleType>
</xs:schema>

, , Book , . Publisher , . , , Book ( bookshop.xsd:

<xs:redefine schemaLocation="bookshop.xsd">
    <xs:complexType name="Book">
        <xs:complexContent>
            <xs:extension base="Book">
                <xs:sequence>
                    <xs:element ref="cs:Publisher" minOccurs="1" maxOccurs="1" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:redefine>

, Publisher. , http://custombookshop.com:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"
    xmlns="http://custombookshop.com" 
    xmlns:bs="http://bookshop.com" 
    targetNamespace="http://custombookshop.com"> 

    <xs:import namespace="http://bookshop.com" schemaLocation="bookshop.xsd"/>
    <xs:element name="Publisher" type="bs:String32" />

</xs:schema>

:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"
    xmlns="http://bookshop.com" 
    targetNamespace="http://bookshop.com"
    xmlns:cs="http://custombookshop.com"> 

    <xs:import schemaLocation="custombookshop.xsd" namespace="http://custombookshop.com"/>

    <xs:redefine schemaLocation="bookshop.xsd">
        ...
    </xs:redefine>

</xs:schema>

XML .

+2

Source: https://habr.com/ru/post/1543249/


All Articles