我有两个xml模式:
1) infrastructureRoot.xsd:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mif="urn:hl7-org:v3/mif"
xmlns:v3="urn:hl7-org:v3"
xmlns:ex="urn:hl7-org/v3-example"
xmlns="urn:hl7-org:v3"
targetNamespace="urn:hl7-org:v3"
elementFormDefault="unqualified">
<xs:include schemaLocation="datatypes-base.xsd"/>
<xs:group name="InfrastructureRootElements">
<xs:sequence>
<xs:element name="realmCode"
type="Norwegian_customer"
minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:group>
<xs:attributeGroup name="InfrastructureRootAttributes"/>
</xs:schema>2)datatypes-base.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:sch="http://www.ascc.net/xml/schematron"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified">
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Norwegian_customer">
<xs:complexContent>
<xs:restriction base="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country"
type="xs:string"
fixed="Norway"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>我使用以下C#代码加载包含所有内容的根模式:
Func<XmlReader> xmlReaderFactory = () =>
{
XmlReaderSettings schemaReaderSettings = new XmlReaderSettings{ DtdProcessing = DtdProcessing.Parse};
XmlTextReader reader = new XmlTextReader(@"InfrastructureRoot.xsd");
return XmlReader.Create(reader, schemaReaderSettings);
};
XmlSchema xsd = XmlSchema.Read(xmlReaderFactory(), (sender, eventArgs) => {});
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += (sender, eventArgs) => Console.WriteLine(eventArgs.Message + " " + eventArgs.Severity);
try
{
try
{
XmlReaderSettings schemaReaderSettings = new XmlReaderSettings {DtdProcessing = DtdProcessing.Parse};
XmlReader schemaReader = XmlReader.Create(new DummyReader(), schemaReaderSettings);
schemaSet.Add(null, schemaReader);
}
catch
{
// the only thing this code is needed is to set ProhibitDTD to false
// there is no acceptable public way to do that
}
schemaSet.Add(xsd);
schemaSet.Compile();
XmlSchemaInclude external = ((XmlSchemaInclude)xsd.Includes[0]);
String targetNamespace = external.Schema.TargetNamespace;
Debug.Assert(targetNamespace == null);
}
catch{}执行后,"targetNamespace“值等于"urn:hl7-org:v3”,这与原始模式"datatypes-base.xsd“不同,破坏了验证。有人能帮我解决这个问题吗?
发布于 2013-02-06 08:19:54
我们一直在与BizTalk和.Net中的HL7v3和CDA合作。为了让CDA模式正确地进行验证和发送消息,我必须将目标名称空间urn:hl7-org:v3添加到所有"coreschemas“xsd文件中。验证生效后,BizTalk消息就开始流动了。
我对将目标名称空间添加到我并不真正拥有的模式感到不舒服,但这是一个很好的折衷方案,因为我实际上从未更改过模式本身,之后一切都会正常工作。
发布于 2013-02-06 07:58:22
当具有显式目标名称空间的模式文档(如infrastructureRoot.xsd)使用xs:include包含未指定目标名称空间的模式文档(如datatypes-base.xsd)时,第二个模式文档中的声明将被解释为其包含的模式文档与包含模式文档具有相同的名称空间。这种机制有时被称为“变色龙包含”--包含的模式文档中的声明根据上下文采用这个或那个目标名称空间。
如果希望复杂类型Customer和NorwegianCustomer不被名称空间urn:hl7-org:v3捕获,那么infrastructureRoot.xsd需要使用xs:import,而不是xs:include,并且需要更改默认名称空间,使对type="Norwegian_customer"的引用成为对{}Norwegian_customer的引用(即,以Norwegian_customer作为本地名称且没有名称空间的限定名称),而不是(就像现在一样)对{urn:hl7-org:v3}Norwegian_customer的引用。
导入构造xs:import从不同的名称空间导入组件,而xs:include包含来自相同名称空间的组件。Chameleon include可以看作是一种使包含的模式文档中的组件位于同一名称空间的方法;如果它不存在,那么您的基础架构模式中的xs:include将简单地引发一个错误。
发布于 2013-02-07 02:09:23
最初的问题是在将XML源导入我们的自定义数据存储格式并向后导出到HL7v3 schema的过程中发现的。第二个模式获取目标命名空间,而不是默认命名空间,这会导致验证错误。下面是第二个模式导出结果:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns:sch="http://www.ascc.net/xml/schematron"
xmlns="urn:hl7-org123:v3"
xmlns:v3="urn:hl7-org:v3"
elementFormDefault="unqualified"
targetNamespace="urn:hl7-org:v3"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
<xs:element name="country" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Norwegian_customer">
<xs:complexContent mixed="false">
<xs:restriction base="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
<xs:element fixed="Norway" name="country" type="xs:string" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>在限制基本属性值上验证失败。根据目标模式设计解释,我们有两种可能的解决方案:
https://stackoverflow.com/questions/14713513
复制相似问题