如何在 XSD 中引用全局类型? [英] How to reference global types in XSD?
问题描述
如何实现对 simpleType 的 XSD 内部引用?
How to implement XSD inner reference to simpleType?
我遇到了您在下图中看到的错误.
I am facing the error you see in the image below.
XSD
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://example.com/ct-required"
xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
targetNamespace="http://example.com/know-type">
<xs:import namespace="http://www.w3.org/2005/05/xmlmime"
schemaLocation="http://www.w3.org/2005/05/xmlmime"/>
<xs:simpleType name="JPEGPictureType"
xmime:expectedContentTypes="image/jpeg">
<xs:restriction base="xs:base64Binary"/>
</xs:simpleType>
<xs:simpleType name="PNGPictureType"
xmime:expectedContentTypes="image/png">
<xs:restriction base="xs:base64Binary"/>
</xs:simpleType>
<xs:simpleType name="Money">
<xs:restriction base="xs:decimal">
<xs:totalDigits value="13" />
<xs:fractionDigits value="2" />
<xs:minInclusive value="0.00" />
<xs:maxInclusive value="99999999999.99" />
</xs:restriction>
</xs:simpleType>
<xs:element name="Envelope">
<xs:complexType>
<xs:sequence>
<xs:element name="Deposit" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="ClientId" type="xs:unsignedLong" />
<xs:element name="Account" type="xs:unsignedShort" />
<xs:element name="Currency" type="xs:string" />
<xs:element name="TotalSum" type="xs:Money" />
<xs:element name="SignedDate" type="xs:date" />
<xs:element name="Cheques">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Cheque">
<xs:complexType>
<xs:sequence>
<xs:element name="Images">
<xs:complexType>
<xs:sequence>
<xs:element name="Front" type="PNGPictureType" />
<xs:element name="Back" type="PNGPictureType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="MicrCodeCmc" type="xs:integer" />
<xs:element name="Amount" type="xs:unsignedLong" />
<xs:element name="PaymentDate" type="xs:date" />
<xs:element name="EmissionDate" type="xs:date" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML
<?xml version="1.0" encoding="utf-8"?>
<Envelope>
<Deposit>
<ClientId>1234567890123</ClientId>
<Account>0045678</Account>
<Currency>USD</Currency>
<TotalSum>500000</TotalSum>
<SignedDate>2016-02-15</SignedDate>
<Cheques>
<Cheque>
<Images>
<Front>
SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAAfQUAAAEBBAABAAAAWgIAAAIBAwABAAAAAQAA
AAMBAwABAAAABAAAAAYBAwABAAAAAAAAAA4BAgAhAAAA0AAAABEBBAABAAAAAAIAABIBAwABAAAA
AQAAABUBAwABAAAAAQAAABYBBAABAAAAWgIAABcBBAABAAAAvi8AABoBBQABAAAAAAEAABsBBQAB
EdF0fRG0R0YRdH2R2XzaI6OxRQAQAQA=
</Front>
<Back>
SUkqAAgAAAAOAP4ABAABAAAAAAAAAAABBAABAAAAgQUAAAEBBAABAAAAVAIAAAIBAwABAAAAAQAA
AAMBAwABAAAABAAAAAYBAwABAAAAAAAAABEBBAABAAAAAAEAABIBAwABAAAAAQAAABUBAwABAAAA
AQAAABYBBAABAAAAVAIAABcBBAABAAAAcggAABoBBQABAAAAwAAAABsBBQABAAAA0AAAACgBAwAB
AAAAAgAAAAAAAAAAAAAAAAAAAAAAyAAAAAEAAAAAAAAAAAAAAMgAAAABAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//LZCdO1f7iRrKXTqt//pePx///9lNoL487Ajh
jFyzUrtwg+6gg9VmvCIWA42XMwziSUEEw7GoIcSUWZ3Y0oKmGR3LToGVC2LhkK6H4sorQNRUMLjH
LTGzlpv3RFCcH4NLB9hvLTmD8tMgOsG+WVaR5AweTcEWMMfaDQxDIx5NwVQx8OMPeGFHLSUlLcSS
1JtNV/9/rrffuO+h9bx////kfvABABAA
</Back>
</Images>
<MicrCodeCmc>123456789012345678901234567890</MicrCodeCmc>
<Amount>99999999999</Amount>
<PaymentDate>2016-02-15</PaymentDate>
<EmissionDate>2016-02-15</EmissionDate>
</Cheque>
<Cheque>
<Images>
<Front>
SUkqAAgAAAAPAP4ABAABAAAAAAAAAAABBAABAAAAfQUAAAEBBAABAAAAWgIAAAIBAwABAAAAAQAA
AAMBAwABAAAABAAAAAYBAwABAAAAAAAAAA4BAgAhAAAA0AAAABEBBAABAAAAAAIAABIBAwABAAAA
AQAAABUBAwABAAAAAQAAABYBBAABAAAAWgIAABcBBAABAAAAvi8AABoBBQABAAAAAAEAABsBBQAB
EdF0fRG0R0YRdH2R2XzaI6OxRQAQAQA=
</Front>
<Back>
SUkqAAgAAAAOAP4ABAABAAAAAAAAAAABBAABAAAAgQUAAAEBBAABAAAAVAIAAAIBAwABAAAAAQAA
AAMBAwABAAAABAAAAAYBAwABAAAAAAAAABEBBAABAAAAAAEAABIBAwABAAAAAQAAABUBAwABAAAA
AQAAABYBBAABAAAAVAIAABcBBAABAAAAcggAABoBBQABAAAAwAAAABsBBQABAAAA0AAAACgBAwAB
AAAAAgAAAAAAAAAAAAAAAAAAAAAAyAAAAAEAAAAAAAAAAAAAAMgAAAABAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//LZCdO1f7iRrKXTqt//pePx///9lNoL487Ajh
jFyzUrtwg+6gg9VmvCIWA42XMwziSUEEw7GoIcSUWZ3Y0oKmGR3LToGVC2LhkK6H4sorQNRUMLjH
LTGzlpv3RFCcH4NLB9hvLTmD8tMgOsG+WVaR5AweTcEWMMfaDQxDIx5NwVQx8OMPeGFHLSUlLcSS
1JtNV/9/rrffuO+h9bx////kfvABABAA
</Back>
</Images>
<MicrCodeCmc>123456789012345678901234567890</MicrCodeCmc>
<Amount>99999999999</Amount>
<PaymentDate>2016-02-15</PaymentDate>
<EmissionDate>2016-02-15</EmissionDate>
</Cheque>
</Cheques>
</Deposit>
</Envelope>
推荐答案
如何在 XSD 中引用全局类型或元素
使用 @targetNamespace
[这一半的答案解决了原来的问题.]
首先为@targetNamespace
指定的命名空间在xs:schema
上定义一个命名空间前缀:
First define a namespace prefix on xs:schema
for the namespace specified by @targetNamespace
:
<xs:schema targetNamespace="http://example.com/know-type"
xmlns:kt="http://example.com/know-type"
...
/>
然后在您引用此命名空间中的全局类型的地方使用它.
Then use it where you reference global types in this namespace.
使用它来引用 Money
,这会导致您立即出错:
Use it to reference Money
, which is causing your immediate error:
<xs:element name="TotalSum" type="kt:Money" />
在您引用 PNGPictureType
的地方也使用它:
Use it also where you reference PNGPictureType
:
<xs:element name="Front" type="kt:PNGPictureType" />
<xs:element name="Back" type="kt:PNGPictureType" />
进行这些更改后,您的 XSD 将没有错误.
After making these changes, your XSD will be free of errors.
要引用 Money
,请将 type="xs:Money"
替换为 type="Money"
,并确保目标引用是全局定义的,而不是本地定义的(您是):
To reference Money
, replace type="xs:Money"
with type="Money"
, and be sure that the target of the reference is globally, not locally, defined (your is):
<xs:element name="TotalSum" type="Money" />
在这种情况下不需要更改对 PNGPictureType
的引用,因为它已经正确地处理了无命名空间的全局类型,而没有使用不正确的命名空间前缀.
No change is needed in this case for the references to PNGPictureType
because it already properly addresses the no-namespace global types without using an improper namespace prefix.
这篇关于如何在 XSD 中引用全局类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!