我有一个我无法改变的现有命名空间。我需要向复杂类型添加元素,以便生成的XML如下所示:
原始XML:
<?xml version="1.0"?>
<Book xmlns="http://bookshop.com">
<Author>Frank Herbert</Author>
<Title>Dune</Title>
</Book>
新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>
我要求自定义元素必须如上所示显示名称空间前缀,并且复杂类型名称不得更改。
以下是我尝试使用重新定义的原始XSD和新XSD。这会有效,还是有更好的方法来实现这一目标?提前感谢您的建议。
原创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>
我尝试了新的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>
答案 0 :(得分:3)
您的原始架构应在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文件。