当我定义了XML Schema后,我可以通过引用它来指定XML必须对应于所引用的模式。这样,我可以强制验证此类XML,并为将要编辑此文件的人提供有价值的提示,因为支持XML Schema的XML编辑器将使用此引用生成自动完成,从而使编辑更加容易。
然而,我在JSON Schema文档中没有看到这样的引用。 例如:https://json-schema.org/learn/getting-started-step-by-step.html 看起来它不是标准的一部分,或者我只是找不到它。
以下是使用引用的XSD Schema示例:
现在任何人都可以开始编辑它,并且如果支持的话,可以基于引用的 XSD 模式在他们的编辑器中获得自动完成。但是 JSON Schema 呢?如果我有这样的 JSON Schema:
那么我应该如何将JSON链接到对应的模式?
然而,我在JSON Schema文档中没有看到这样的引用。 例如:https://json-schema.org/learn/getting-started-step-by-step.html 看起来它不是标准的一部分,或者我只是找不到它。
以下是使用引用的XSD Schema示例:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/definitions/product">
<xsd:element name="product">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="id" type="xsd:long" maxOccurs="1" minOccurs="1"/>
<xsd:element name="name" type="xsd:string" maxOccurs="1" minOccurs="1"/>
<xsd:element name="description" type="xsd:string" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
这里有一个使用该模式的XML,通过引用它的逻辑名称:http://example.org/definitions/product
<product:product xmlns:product="http://example.org/definitions/product">
<id>1</id>
<name>One</name>
<description>The One</description>
</product:product>
现在任何人都可以开始编辑它,并且如果支持的话,可以基于引用的 XSD 模式在他们的编辑器中获得自动完成。但是 JSON Schema 呢?如果我有这样的 JSON Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.org/definitions/product",
"title": "product",
"type": "object",
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
}
},
"required": [ "id", "name" ]
}
实际的JSON如下:
{
"id": 1,
"name": "One",
"description": "The one"
}
那么我应该如何将JSON链接到对应的模式?
http://example.org/definitions/product
不是模式的逻辑名称。它是您在文档中使用的命名空间URI,您还有一个模式恰好具有该命名空间中某些元素的定义。命名空间可以拥有比模式更多的内容,并且可以为命名空间提供多个模式。没有1:1映射关系。也许您指的是xsi:schemaLocation
(xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="..."
)。 - Erwin Bolwidt