xml 如何在 XSD 中处理具有不同 URI 的多个命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9805359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to handle multiple namespaces with different URI in XSD
提问by prathima
I have an XML (first.xml) which looks like ::
我有一个 XML (first.xml),它看起来像 ::
<?xml version="1.0" encoding="utf-8"?>
<saw:jobInfo xmlns:saw="com.analytics.web/report/v1.1">
<saw:jobStats>...........</saw:jobStats>
<saw:detailedInfo> .....</saw:detailedInfo>
<saw:fileInfo>..........</saw:fileInfo>
</saw:jobInfo>
The below XML (second.xml) is same as the above but with a different namespace.
下面的 XML (second.xml) 与上面的相同,但具有不同的命名空间。
<?xml version="1.0" encoding="utf-8"?>
<soap:jobInfo xmlns:soap="urn://bi.webservices/v6">
<soap:jobStats>...........</saw:jobStats>
<soap:detailedInfo> .....</saw:detailedInfo>
<soap:fileInfo>..........</saw:fileInfo>
</soap:jobInfo>
As I have the same element and attribute names in both the xml's I am using the same xsd file to validate both.
由于我在两个 xml 中具有相同的元素和属性名称,因此我使用相同的 xsd 文件来验证两者。
XSD file ::
XSD 文件 ::
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="com.analytics.web/report/v1.1"
xmlns="com.analytics.web/report/v1.1"
xmlns:saw="com.analytics.web/report/v1.1"
xmlns:soap="urn://bi.webservices/v6"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
After including xmlns:soap="urn://bi.webservices/v6" the schema validation failed for second.xml saying unkown element "soap:jobinfo". I poked around and found the targetNamespace value should be same as the namespace URI. Please let me know how to use the same XSD for two different namespaces having different URIs.
在包含 xmlns:soap="urn://bi.webservices/v6" 之后,second.xml 的模式验证失败,说 unkown 元素“soap:jobinfo”。我四处寻找,发现 targetNamespace 值应该与命名空间 URI 相同。请让我知道如何为具有不同 URI 的两个不同命名空间使用相同的 XSD。
回答by Petru Gardea
Short answer is you can't. You could, however, if you would be using three XSDs. It would allow you to have all the XSD that matters into one file (Chameleon.XSD), and have two more that simply compose Chameleon.XSD, those two having the namespaces you want.
简短的回答是你不能。但是,如果您要使用三个 XSD,则可以。它将允许您将所有重要的 XSD 包含在一个文件 (Chameleon.XSD) 中,并有两个简单地组成 Chameleon.XSD,这两个具有您想要的名称空间。
Chameleon.XSD
变色龙
<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="jobInfo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="jobStats" type="xsd:string" />
<xsd:element name="detailedInfo" type="xsd:string" />
<xsd:element name="fileInfo" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
JobInfo1.xsd
作业信息1.xsd
<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema xmlns="com.analytics.web/report/v1.1" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="com.analytics.web/report/v1.1" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="Chameleon.xsd"/>
</xsd:schema>
JobInfo2.xsd
作业信息2.xsd
<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema xmlns="urn://bi.webservices/v6" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn://bi.webservices/v6" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="Chameleon.xsd"/>
</xsd:schema>
Relationships:
关系:


If you want one XSD to validate them all, then you can go and build a fourth one, that imports these two.
如果你想要一个 XSD 来验证它们,那么你可以去构建第四个,导入这两个。
OneAll.XSD
OneAll.XSD
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xsd:schema xmlns="urn:tempuri-org:XSD:1" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:tempuri-org:XSD:1" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="com.analytics.web/report/v1.1" schemaLocation="JobInfo1.xsd"/>
<xsd:import namespace="urn://bi.webservices/v6" schemaLocation="JobInfo2.xsd"/>
</xsd:schema>
Updated relationships:
更新的关系:



