如何将 2 xsd 模式包含到 1 xml 文件中?
How to include 2 xsd schemas into 1 xml file?
我的问题是关于 2 个 XSD 模式,它们必须是 XML 文档的基础。对我来说,首先创建我需要的 XML 然后创建那些 XSD 模式更容易。所以,我这里有三个文件。
第一 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com">
<xs:element name="studentAI">
<xs:complexType>
<xs:sequence>
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="female"/>
<xs:enumeration value="male"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="other" type="desc"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="desc">
<xs:sequence>
<xs:element name="height" type="xs:string"/>
<xs:element name="weight" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
第二 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com">
<xs:element name="group">
<xs:complexType>
<xs:sequence>
<xs:element name="student" minOccurs="2" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="student">
<xs:sequence>
<xs:element name="surname" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="birthday">
<xs:simpleType>
<xs:restriction base="xs:date"/>
</xs:simpleType>
</xs:element>
<xs:element name="parents" type="parentsDetails"/>
<xs:element name="allCourses" type="allCoursesDetails"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="parentsDetails">
<xs:sequence>
<xs:element name="father" type="xs:string"/>
<xs:element name="mother" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="allCoursesDetails">
<xs:sequence>
<xs:element name="course" type="courseDetails" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="courseDetails">
<xs:attribute name="nr" use="required"/>
</xs:complexType>
</xs:schema>
还有 XML 文档,我需要在这 2 XSD 模式的基础上获取(抱歉,现在它是空的,但当然必须有信息,而且会不止一个学生在群里):
<group xmlns:xs="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com ">
<student id="1">
<surname></surname>
<name></name>
<birthday></birthday>
<parents>
<father></father>
<mother></mother>
</parents>
<allCourses>
<course nr="01"></course>
<course nr="02"></course>
</allCourses>
<studentAI>
<gender></gender>
<age></age>
<other>
<height></height>
<weight></weight>
</other>
</studentAI>
</student>
</group>
我真的不知道如何在我的 XML 文档中包含这 2 个 XSD 模式。我应该将它们都包含在 XML 文档中,还是应该首先将第一个 XSD 模式包含在第二个模式中(结果 XML 也是如此),然后再包含联合会进入 XML 文件?以及如何做到这一点?
我最大的问题是关于所有三个文档的上半部分应该放置名称空间声明等的地方。
至于我尝试使用的程序是 Oxygen(我有 30 天的 lisense)。
我真的希望有人能帮助我解决这个问题。我也希望我的问题很清楚。
非常感谢您的帮助!
您可以将两个模式的内容合并到一个独特的模式中,使一个模式包含另一个模式。无论如何你应该纠正这个问题:
如果您使用 name
或 ref
来引用命名空间中的元素,您需要限定该引用(或者简单地使用您的 targetNamespace 作为 xsds 中的默认命名空间那更容易)。因此,将您的目标命名空间绑定到前缀。
xmlns:w3="http://www.w3schools.com"
然后更正您的引用,例如:
<xs:element name="allCourses" type="w3:allCoursesDetails"/>
或者只需将 targetNamespace 设置为您的 xsds 中的默认命名空间:
xmlns="http://www.w3schools.com"
这样,非限定引用指向默认命名空间,即设置为目标命名空间。
那么你应该在your/yoursxs:schema
中使用elementFormDefault="qualified"
,如果你想让你的XML对属于 targetNampesce 的所有元素都有效。否则,您的某些 XML 元素应属于目标命名空间,而其中一些元素不应属于任何命名空间。
将第一个模式的内容添加到包含 student 元素的模式或 添加一个包含子句使 student 模式包含 studentAI:
<xs:include schemaLocation="studentAI.xsd"/>
之后你只需要根据包含 student 元素的模式验证 XML,所以你需要 在 XML:
中添加 xsi:schemaLocation
xsi:schemaLocation="http://www.w3schools.com xsd1.xsd"
请记住,您可以使用 ref
引用元素。您需要在序列中的 allCourses 元素之后添加 studentAI:
<xs:element name="allCourses" type="allCoursesDetails"/>
<xs:element ref="studentAI"/>
此外请记住,您可以简化生日元素,如果您不添加任何限制,则不需要限制元素。
希望对您有所帮助。
编辑:添加了更多内容(第 5 项和第 6 项)。
首先,您的术语是乱七八糟的。模式是一组模式组件。如果你有两个模式,那意味着你真的有一个模式,它是两个模式组件的联合。您所说的不是模式,而是模式文档,它们是包含 XML 模式组件表示的文件。
您的两个架构文档共享同一个目标命名空间。您使用的命名空间 http://www.w3schools.com 是一个糟糕的选择,因为它是您不拥有的域名。但这是一个不礼貌的问题,而不是技术问题。
不过,看到xsi:schemaLocation="http://www.w3schools.com "
就奇怪了。如果您必须使用 xsi:schemaLocation 从实例文档引用模式,则该属性应包含偶数个 URI:它本质上是从名称空间 URI 到模式文档的 URL 位置的映射该目标命名空间。
如果特定命名空间的架构被拆分成多个架构文档,那么 xsi:schemaLocation 无法引用它们。最好有一个完整描述命名空间的模式文档,可以在单个文档中,也可以使用 xs:include 声明将所有组成模式文档放在一起。
就我个人而言,我发现从实例中引用模式文档的想法并不令人满意。如果您不信任实例文档,则只需验证它。但是,如果您不信任它,那么您想根据您选择的模式进行验证,而不是实例文档本身选择的模式。说 "I want to check that this document is valid, but I don't care what schema it is valid against, I'll let the document itself decide that.".
没有多大意义
我的问题是关于 2 个 XSD 模式,它们必须是 XML 文档的基础。对我来说,首先创建我需要的 XML 然后创建那些 XSD 模式更容易。所以,我这里有三个文件。
第一 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com">
<xs:element name="studentAI">
<xs:complexType>
<xs:sequence>
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="female"/>
<xs:enumeration value="male"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="other" type="desc"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="desc">
<xs:sequence>
<xs:element name="height" type="xs:string"/>
<xs:element name="weight" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
第二 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com">
<xs:element name="group">
<xs:complexType>
<xs:sequence>
<xs:element name="student" minOccurs="2" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="student">
<xs:sequence>
<xs:element name="surname" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="birthday">
<xs:simpleType>
<xs:restriction base="xs:date"/>
</xs:simpleType>
</xs:element>
<xs:element name="parents" type="parentsDetails"/>
<xs:element name="allCourses" type="allCoursesDetails"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="parentsDetails">
<xs:sequence>
<xs:element name="father" type="xs:string"/>
<xs:element name="mother" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="allCoursesDetails">
<xs:sequence>
<xs:element name="course" type="courseDetails" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="courseDetails">
<xs:attribute name="nr" use="required"/>
</xs:complexType>
</xs:schema>
还有 XML 文档,我需要在这 2 XSD 模式的基础上获取(抱歉,现在它是空的,但当然必须有信息,而且会不止一个学生在群里):
<group xmlns:xs="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com ">
<student id="1">
<surname></surname>
<name></name>
<birthday></birthday>
<parents>
<father></father>
<mother></mother>
</parents>
<allCourses>
<course nr="01"></course>
<course nr="02"></course>
</allCourses>
<studentAI>
<gender></gender>
<age></age>
<other>
<height></height>
<weight></weight>
</other>
</studentAI>
</student>
</group>
我真的不知道如何在我的 XML 文档中包含这 2 个 XSD 模式。我应该将它们都包含在 XML 文档中,还是应该首先将第一个 XSD 模式包含在第二个模式中(结果 XML 也是如此),然后再包含联合会进入 XML 文件?以及如何做到这一点?
我最大的问题是关于所有三个文档的上半部分应该放置名称空间声明等的地方。
至于我尝试使用的程序是 Oxygen(我有 30 天的 lisense)。
我真的希望有人能帮助我解决这个问题。我也希望我的问题很清楚。
非常感谢您的帮助!
您可以将两个模式的内容合并到一个独特的模式中,使一个模式包含另一个模式。无论如何你应该纠正这个问题:
如果您使用
name
或ref
来引用命名空间中的元素,您需要限定该引用(或者简单地使用您的 targetNamespace 作为 xsds 中的默认命名空间那更容易)。因此,将您的目标命名空间绑定到前缀。xmlns:w3="http://www.w3schools.com"
然后更正您的引用,例如:
<xs:element name="allCourses" type="w3:allCoursesDetails"/>
或者只需将 targetNamespace 设置为您的 xsds 中的默认命名空间:
xmlns="http://www.w3schools.com"
这样,非限定引用指向默认命名空间,即设置为目标命名空间。
那么你应该在your/yours
xs:schema
中使用elementFormDefault="qualified"
,如果你想让你的XML对属于 targetNampesce 的所有元素都有效。否则,您的某些 XML 元素应属于目标命名空间,而其中一些元素不应属于任何命名空间。将第一个模式的内容添加到包含 student 元素的模式或 添加一个包含子句使 student 模式包含 studentAI:
<xs:include schemaLocation="studentAI.xsd"/>
之后你只需要根据包含 student 元素的模式验证 XML,所以你需要 在 XML:
中添加 xsi:schemaLocationxsi:schemaLocation="http://www.w3schools.com xsd1.xsd"
请记住,您可以使用
ref
引用元素。您需要在序列中的 allCourses 元素之后添加 studentAI:<xs:element name="allCourses" type="allCoursesDetails"/> <xs:element ref="studentAI"/>
此外请记住,您可以简化生日元素,如果您不添加任何限制,则不需要限制元素。
希望对您有所帮助。
编辑:添加了更多内容(第 5 项和第 6 项)。
首先,您的术语是乱七八糟的。模式是一组模式组件。如果你有两个模式,那意味着你真的有一个模式,它是两个模式组件的联合。您所说的不是模式,而是模式文档,它们是包含 XML 模式组件表示的文件。
您的两个架构文档共享同一个目标命名空间。您使用的命名空间 http://www.w3schools.com 是一个糟糕的选择,因为它是您不拥有的域名。但这是一个不礼貌的问题,而不是技术问题。
不过,看到xsi:schemaLocation="http://www.w3schools.com "
就奇怪了。如果您必须使用 xsi:schemaLocation 从实例文档引用模式,则该属性应包含偶数个 URI:它本质上是从名称空间 URI 到模式文档的 URL 位置的映射该目标命名空间。
如果特定命名空间的架构被拆分成多个架构文档,那么 xsi:schemaLocation 无法引用它们。最好有一个完整描述命名空间的模式文档,可以在单个文档中,也可以使用 xs:include 声明将所有组成模式文档放在一起。
就我个人而言,我发现从实例中引用模式文档的想法并不令人满意。如果您不信任实例文档,则只需验证它。但是,如果您不信任它,那么您想根据您选择的模式进行验证,而不是实例文档本身选择的模式。说 "I want to check that this document is valid, but I don't care what schema it is valid against, I'll let the document itself decide that.".
没有多大意义