【问题标题】:XSD targetNamespace does not override element namespace?XSD targetNamespace 不会覆盖元素命名空间?
【发布时间】:2019-03-28 10:25:47
【问题描述】:

我有一个定义复杂类型并设置了targetNamespace 属性的XSD。 TestElement 不会获得targetNamespace 设置的命名空间是否正确?它应该从复杂类型afn:ElementTypehttp://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>

【问题讨论】:

    标签: xml xsd xsd-validation xml-validation


    【解决方案1】:

    xs:schema/elementFormDefault="qualified"

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

    在 XSD 中声明的 元素 必须位于 XSD 的 targetNamespace 给定的命名空间中。

    因此,对于您的 XSD,TestElement 必须在 http://somefancy.namespace 中才能使 XML 文档有效。如果您希望它位于 http://anotherfancy.namespace 中,请在导入的 XSD 中声明 元素;将其 type 存储在那里不会将元素本身放置在其他命名空间中。一旦在导入的命名空间中声明了TestElement,它就可以通过xs:element/@ref在原始命名空间中使用。

    另见How to reference element in other XSD's namespace?

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

    请参阅Michael Kay's answer heremy longer answer 这个问题:What does elementFormDefault do in XSD?

    【讨论】:

    • 这还不是全部;它忽略了局部和全局元素声明之间的区别,以及formelementFormDefault 的影响。
    • @MichaelKay:是的,我应该详细说明或至少提到elementFormDefault 等方面的考虑。答案已更新。谢谢。
    【解决方案2】:

    在本地元素声明中声明的元素的命名空间在以下规则中给出(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·.
    

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

    xs:elementform 属性很少使用,但如果值为qualified,则该元素进入包含xs:schema 声明的targetNamespace,而如果它是unqualified,则进入在没有命名空间。如果未指定form(几乎总是如此),则默认为xs:schema 元素上的elementFormDefault 值。这通常设置为qualified,因此元素进入模式的目标命名空间;但默认(不幸的是)是unqualified,这意味着它没有进入命名空间。

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 2019-11-17
      • 2010-11-04
      • 1970-01-01
      相关资源
      最近更新 更多