问题描述
我有一个包含一长串允许属性的 attributeGroup,比如 a:attr-group,我将其导入另一个 xsd 以限制元素(在另一个 xsd 中定义)的允许属性.
I have an attributeGroup with long list of allowed attributes say a:attr-group, that I import to another xsd to restrict an element's (defined in another xsd) allowed attributes with.
我还希望允许同一个元素在第二个命名空间下使用相同的属性,比如 b:attr-group,并希望使用相同的文件(而不是重复所有的属性和组定义).
I also want to allow the same element to use the same attributes under a second namespace say b:attr-group, and wish to use the same file (rather than repeat all the attributes and group definition).
有没有一种简单的方法可以做到这一点?到目前为止,我的所有尝试都被导入命名空间必须等于目标命名空间规则所挫败.
Is there a simple way of doing this? All my attempts so far have been thwarted by the import namespace must equal targetNamespace rule.
提前致谢!
推荐答案
这种模式也被称为变色龙.这意味着您包括一个没有目标命名空间的 XML 模式,这反过来使该模式假定成为父模式的命名空间.
This pattern is also known as chameleon. It means you're including an XML Schema that has no target namespace, which in turn makes that schema assume the namespace of the parent schema.
更新:考虑评论中提供的示例 XML:
UPDATE: considering the sample XML provided in the comments:
第一个架构文件 (AttrGroup.xsxd):
First schema file (AttrGroup.xsxd):
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema
elementFormDefault="qualified" attributeFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:attributeGroup name="attr-group">
<xsd:attribute name="attr1" type="xsd:string"/>
<xsd:attribute name="attr2" type="xsd:int"/>
</xsd:attributeGroup>
</xsd:schema>
第二个架构文件 (A.xsd):
Second schema file (A.xsd):
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd/a"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd/a"
xmlns:a="http://tempuri.org/XMLSchema.xsd/a"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="AttrGroup.xsd"/>
<xsd:attributeGroup name="a-group">
<xsd:attributeGroup ref="attr-group"/>
</xsd:attributeGroup>
</xsd:schema>
第三个架构文件(B.xsd):
Third schema file (B.xsd):
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd/b"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd/b"
xmlns:b="http://tempuri.org/XMLSchema.xsd/b"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="AttrGroup.xsd"/>
<xsd:attributeGroup name="b-group">
<xsd:attributeGroup ref="attr-group"/>
</xsd:attributeGroup>
</xsd:schema>
第四个 XML 架构 (Element.xsd):
Fourth XML Schema (Element.xsd):
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:a="http://tempuri.org/XMLSchema.xsd/a" xmlns:b="http://tempuri.org/XMLSchema.xsd/b">
<xsd:import namespace="http://tempuri.org/XMLSchema.xsd/a" schemaLocation="A.xsd"/>
<xsd:import namespace="http://tempuri.org/XMLSchema.xsd/b" schemaLocation="B.xsd"/>
<xsd:element name="element">
<xsd:complexType>
<xsd:attributeGroup ref="a:a-group"/>
<xsd:attributeGroup ref="b:b-group"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
有效 XML 示例:
<element xmlns:a="http://tempuri.org/XMLSchema.xsd/a" xmlns:b="http://tempuri.org/XMLSchema.xsd/b" a:attr1="hello" b:attr2="10" />
无效 XML 示例:
<element xmlns:a="http://tempuri.org/XMLSchema.xsd/a" xmlns:b="http://tempuri.org/XMLSchema.xsd/b" a:attr1="hello" b:attr2="10a" />
在针对 Element.xsd 验证无效样本时,我收到此错误消息(在我的工具上):加载 [] 时出错,第 1 行位置 116'http://tempuri.org/XMLSchema.xsd/b:attr2'属性无效 - 根据其数据类型 'http://www.w3.org/2001/XMLSchema:int' - 字符串 '10a' 不是有效的 Int32 值.D:...\SampleAttrGroup.xml 无效.
When validating against Element.xsd the invalid sample, I am getting this error message (on my tool):Error occurred while loading [], line 1 position 116The 'http://tempuri.org/XMLSchema.xsd/b:attr2' attribute is invalid - The value '10a' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:int' - The string '10a' is not a valid Int32 value.D:...\SampleAttrGroup.xml is invalid.
这篇关于将 xs:attributeGroups 导入多个命名空间 xsd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!