简体   繁体   English

是否可以通过附加的XSD为元素定义XML属性?

[英]Is it possible to define an XML attribute for an element by an additional XSD?

Let's say I have an XML something like this: 假设我有这样的XML:

<root xmlns="default" xmlns:add="additional">
    <element foo="fromDefault" />
    <add:element foo="fromDefault" add:bar="fromAdditional" />
</root>

What I would like to do is to merge the two element definitions to avoid duplications, something like this: 我想做的是合并两个element定义以避免重复,如下所示:

<root xmlns="default" xmlns:add="additional">
    <element foo="fromDefault" add:bar="fromAdditional" />
</root>

But I'm not even sure if this is possible. 但是我什至不确定这是否可行。

I have the feeling it might be doable because we use some similar notations for the root elements like: 我觉得这可能可行,因为我们对根元素使用了类似的表示法,例如:

<root xmlns:xsi="..."
         xsi:schemaLocation="...">

Unfortunately I wasn't able to find any docs/references/tutorials/anything about this issue, could someone help me clarify if what I would like to do is possible (or not) and share some links with me? 不幸的是,我找不到有关此问题的任何文档/参考/教程/任何内容,有人可以帮我弄清楚我想做的事情是否可行,并与我分享一些链接吗?

Thanks in advance! 提前致谢!

I think that you are looking for something like this: 我认为您正在寻找这样的东西:

additional.xsd 附加文件

<xs:schema 
  targetNamespace="additional"
  elementFormDefault="qualified"
  xmlns="additional"
  xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:attribute name="bar" type="xs:string"/>

</xs:schema>

default.xsd default.xsd

<xs:schema 
  targetNamespace="default"
  elementFormDefault="qualified"
  xmlns="default"
  xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:add="additional">

  <xs:import namespace="additional" schemaLocation="additional.xsd"/>

  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="element" minOccurs="1" maxOccurs="1">
          <xs:complexType>
            <xs:attribute name="foo" type="xs:string"/>
            <xs:attribute ref="add:bar"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

The first schema defines only the 'additional' stuff (in the example just an attribute) and the second schema imports it and defines everything else, referencing the additional stuff where needed. 第一个模式仅定义“其他”内容(在示例中仅是一个属性),第二个模式导入它并定义其他所有内容,并在需要时引用其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM