代码之家  ›  专栏  ›  技术社区  ›  Jens Marchewka

xsd架构targetNamespace不重写元素命名空间?

xsd
  •  0
  • Jens Marchewka  · 技术社区  · 6 年前

    我有一个XSD,它定义了一个复杂类型,并且 targetNamespace 属性集。正确吗 TestElement 将无法获取由 目标命名空间 ? 它应该从复杂类型中获得名称空间 afn:ElementType http://anotherfancy.namespace ,对吧?

       <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:sfn="http://somefancy.namespace"
                xmlns:afn="http://anotherfancy.namespace"
                attributeFormDefault="unqualified"
                elementFormDefault="qualified"
                targetNamespace="http://somefancy.namespace"
                version="1.0">
          <xs:import namespace="http://anotherfancy.namespace" schemaLocation="..."/>
          <xs:element name="MyComplexType">
             <xs:complexType>
                <xs:sequence>
                   <xs:element minOccurs="0" name="TestElement" type="afn:ElementType">
                   </xs:element>
                </xs:sequence>
             </xs:complexType>
          </xs:element>
       </xs:schema>
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   kjhughes    6 年前

    什么时候? xs:schema/elementFormDefault="qualified"

    (与您的情况一样,这也是推荐和最常用的设置 elementFormDefault .)

    这个 元素 targetNamespace .

    因此,对于您的XSD, TestElement http://somefancy.namespace 使XML文档有效。如果你想把它放在 http://anotherfancy.namespace 要素 类型 不会将元素本身放在另一个命名空间中。一次 测试员 xs:element/@ref .

    How to reference element in other XSD's namespace?

    对于很少需要的,通常不推荐的其他变体

    Michael Kay's answer here my longer answer 对于这个问题: What does elementFormDefault do in XSD?

        2
  •  1
  •   Michael Kay    6 年前

    在局部元素声明中声明的元素的名称空间在以下规则中给出(XSD 1.1第1部分第3.3.2.3节)

    {target namespace}
    
    The appropriate case among the following:
    1 If targetNamespace is present [as an attribute of the xs:element element], then its ·actual value·.
    2 If targetNamespace is not present and one of the following is true
    2.1 form = qualified
    2.2 form is absent and the <schema> ancestor has elementFormDefault = qualified
    then the ·actual value· of the targetNamespace [attribute] of the ancestor <schema> element information item, or ·absent· if there is none.
    3 otherwise ·absent·.
    

    targetNamespace 属性 xs:element 在1.1中是新的,因此对于1.0,可以忽略规则1。

    这个 form 属性 xs:元素 qualified 然后元素进入在包含 xs:schema unqualified 然后它不进入名称空间。如果 形式 未指定(几乎总是如此),则默认值为 elementFormDefault 元素。通常设置为 ,因此元素进入架构的目标命名空间;但默认值(不幸的是)是 不合格 这意味着它不在名称空间中。