S Yuki
S Yuki

Reputation: 95

Use XML Schema to extend an element with attributes rather than a complexType

So I found this post which is very similar to what I want to achieve, but I can't quite figure out if it's not possible to do what I am attempting, or if I'm just missing something...

Use XML Schema to extend an element rather than a complexType

The gist is that I have an XSD that contains a defined Element. I would prefer not to edit this original xsd file. Is there any way for me to extend the Element so that I also can add my own attributes, "attributeC"?

The other post creates a new complexType of fooType, and places the element inside of it, and extends fooType to contain more elements. By doing so, they are able to achieve an Element that contains fooElement, and two other added elements. The issue is that I'd like to add to the Element itself, not add to the element at the same level.

XSD1

<xs:schema xmlns:bas="http://www.base.com"
           xmlns="https://www.adding.com"
           attributeFormDefault="qualified"
           elementFormDefault="qualified"
           targetNamespace="https://www.adding.com"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="XSD2.xsd" namespace="http://www.base.com" />
  <xs:complexType name="FileSpecType">
        <xs:sequence>
            <xs:element ref="bas:FileSpec"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="FullFileSpecType">
        <xs:complexContent mixed="false">
            <xs:extension base="FileSpecType">
                <xs:attribute name="attributeC" type="xs:string" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:element name="FileSpec" type="FileSpecType" />

This code ends up generating a new FileSpec

<FileSpec AttributeC="attCval" />>
    <bas:FileSpec/>
</FileSpec>

What I want to achieve is more like...

<bas:FileSpec AttributeA="attAval" AttributeB="attBval" AttributeC="attCval/>

Can anybody point me in the right direction to solve my issues?

I'm thinking I could define my own dataType=FileSpec and add my own reference to external attributes, but that'd require manually copying over each attribute that exists in my original XSD2 FileSpec so I'd prefer to avoid that if possible. Then I think I could take that new FileSpec that I created and redefine the older FileSpec with it. Is this possible to do? It sounds like a lot of work that must have a simpler solution.

Upvotes: 0

Views: 1455

Answers (1)

Sprotty
Sprotty

Reputation: 5973

Its somewhat unclear what you are trying to do, if this does not cover it, please post the schema you are including as well as the XML you are trying to produce including the containing element.

You can extend an existing xs:complexType like this.

Extending a complexType with Liquid XML Studio

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2020 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="BaseType">
        <xs:attribute name="attributeA" type="xs:string" />
    </xs:complexType>
    <xs:complexType name="ExtendedType">
        <xs:complexContent>
            <xs:extension base="BaseType">
                <xs:attribute name="attributeB" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="RootElm">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="BaseElm" type="BaseType" />
                <xs:element name="ExtendedElm" type="ExtendedType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

This allows you to produce XML that looks like this

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2020 (https://www.liquid-technologies.com) -->
<RootElm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <BaseElm attributeA="XX" />
    <ExtendedElm attributeA="YY" attributeB="ZZ" />
</RootElm>

There is another way to make use of the ExtendedType when ever its valid to use a BaseType

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2020 (https://www.liquid-technologies.com) -->
<RootElm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <BaseElm xsi:type="ExtendedType" attributeB="XX" attributeA="YY" />
    <ExtendedElm attributeA="XX" attributeB="YY" />
</RootElm>

Notice the xsi:type="ExtendedType" in the BaseElm, this tells the XML processor that this element actually contains data for the ExtendedType.

Upvotes: 1

Related Questions