I have 2 schemas.
First:
我有2个架构。第一:
<?xml version="1.0" encoding="utf-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="ns1"
elementFormDefault="qualified">
<element name="A" type="string"/>
</schema>
Second:
第二:
<?xml version="1.0" encoding="utf-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="ns2"
xmlns:ns1="ns1"
elementFormDefault="qualified">
<element name="B">
<complexType>
<sequence>
<element ref="ns1:A"/>
</sequence>
</complexType>
</element>
</schema>
So in instance xml I can use 2 root elements.
Example 1:
所以在实例xml中我可以使用2个根元素。例1:
<?xml version="1.0" encoding="utf-8" ?>
<A xmlns="ns1">sdfdfd</A>
Example 2:
例2:
<?xml version="1.0" encoding="utf-8" ?>
<B xmlns="ns2"><A xmlns="ns1">sdfdfd</A></B>
How to allow only example 2?
如何仅允许示例2?
1 个解决方案
#1
1
It depends on the API of your schema validator.
它取决于架构验证器的API。
In general if an element is valid against a schema, then every child of that element is also valid: therefore, if your second instance is valid, your first one is too, since XSD validates elements rather than documents.
通常,如果元素对模式有效,那么该元素的每个子元素也是有效的:因此,如果第二个实例有效,则第一个实例也是有效的,因为XSD验证元素而不是文档。
However, a schema validator may allow you to specify the root node for validation. For example if you use Saxon as your schema validator, then from the command line you can specify
但是,架构验证程序可能允许您指定根节点以进行验证。例如,如果使用Saxon作为模式验证器,则可以从命令行指定
-top:{ns2}B
机顶:{NS2}乙
See http://www.saxonica.com/documentation/schema-processing/commandline.xml
请参阅http://www.saxonica.com/documentation/schema-processing/commandline.xml
#1
1
It depends on the API of your schema validator.
它取决于架构验证器的API。
In general if an element is valid against a schema, then every child of that element is also valid: therefore, if your second instance is valid, your first one is too, since XSD validates elements rather than documents.
通常,如果元素对模式有效,那么该元素的每个子元素也是有效的:因此,如果第二个实例有效,则第一个实例也是有效的,因为XSD验证元素而不是文档。
However, a schema validator may allow you to specify the root node for validation. For example if you use Saxon as your schema validator, then from the command line you can specify
但是,架构验证程序可能允许您指定根节点以进行验证。例如,如果使用Saxon作为模式验证器,则可以从命令行指定
-top:{ns2}B
机顶:{NS2}乙
See http://www.saxonica.com/documentation/schema-processing/commandline.xml
请参阅http://www.saxonica.com/documentation/schema-processing/commandline.xml