简体   繁体   中英

Extend a public XSD schema with new attribute

I want to extend the existing XSD schema ( trans-unit element) with new attribute called newAttr .

So, the original schema has the following element called trans-unit which I want to extend:

<xsd:element name="trans-unit">
    <xsd:complexType>
      <xsd:attribute name="id" type="xsd:string" use="required"/>
      <xsd:attribute name="approved" type="xlf:AttrType_YesNo" use="optional"/>
      <xsd:anyAttribute namespace="##other" processContents="strict"/>
    </xsd:complexType>
    <xsd:unique name="U_tu_segsrc_mid">
      <xsd:selector xpath="./xlf:seg-source/xlf:mrk"/>
      <xsd:field xpath="@mid"/>
    </xsd:unique>
    <xsd:keyref name="KR_tu_segsrc_mid" refer="xlf:U_tu_segsrc_mid">
      <xsd:selector xpath="./xlf:target/xlf:mrk|./xlf:alt-trans"/>
      <xsd:field xpath="@mid"/>
    </xsd:keyref>
</xsd:element>

To do that I created new schema redefining the original schema:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:oasis:names:tc:xliff:document:1.2" xml:lang="en">
    <xsd:redefine schemaLocation="http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd">
        <xsd:complexType name="trans-unit">
            <xsd:extension base="trans-unit">
                <xsd:attribute name="newAttr" type="xsd:string" use="optional"/>
            </xsd:extension>
        </xsd:complexType>
    </xsd:redefine>
</xsd:schema>

But, unfortunately, the validation of a file, using this extended schema, fails with the following error:

'attribute' is not a valid grandchild element. children of elements must have or descendants, with 'base' attributes that refer to themselves.

I am not sure how I should do the extension. It would be great if someone can explain what I am doing wrong.

the original schema has the following element called trans-unit which I want to extend

XML Schema does not allow an element declaration to be redefined. See https://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#modify-schema :

 <redefine
  id = ID
  schemaLocation = anyURI
  {any attributes with non-schema namespace . . .}>
  Content: (annotation | (simpleType | complexType | group | attributeGroup))*
</redefine>

Note that 'element' is not one of the options for the content of xs:redefine.

I would like to be helpful, and suggest some method of extending this schema. But the public schema seems to have been designed to prohibit extensions. If complex type was global then it could be extended (much better than redefine, btw). But the complex type is locally-defined and so cannot be modified in any way.

I cannot think of any way to change the type definition of this element declaration using the extension mechanisms of XML Schema. I think you will need to modify the XSD.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM