为什么我必须为导入的 XSD 中的属性添加前缀? [英] Why do I have to prefix attributes from imported XSD?

查看:22
本文介绍了为什么我必须为导入的 XSD 中的属性添加前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个XSDS,test.xsd

I have the following two XSDS, test.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"    
  xmlns:xsd="http://www.w3.org/2001/XMLSchema.xsd" 
  xmlns:ns1="http://www.test.com/ns1"     
  targetNamespace="http://www.test.com" 
  elementFormDefault="qualified"
  attributeFormDefault="unqualified">
    <import namespace="http://www.test.com/ns1" schemaLocation="test1.xsd"/>
    <element name="Root">
        <complexType>
            <sequence>
                <element name="Child" type="string"/>
            </sequence>
            <attribute ref="ns1:myAttrib1" use="required"/>
            <attribute ref="ns1:myAttrib2" use="required"/>
        </complexType>
    </element>
</schema>

和 test1.xsd

and test1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema.xsd" 
  xmlns:ns1="http://www.test.com/ns1" 
  targetNamespace="http://www.test.com/ns1" 
  elementFormDefault="qualified" attributeFormDefault="unqualified">
    <attribute name="myAttrib1" type="string"/>
    <attribute name="myAttrib2" type="string"/>
</schema>

实例文档如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns="http://www.test.com" xmlns:ns1="http://www.test.com/ns1" 
  ns1:myAttrib1="1" ns1:myAttrib2="2">
    <Child>Child 1</Child>
</Root>

现在,使用 Xerces 2.11.0,我必须在属性 myAttrib1myAttrib2 前加上 ns1 以使其通过验证.起初我觉得这不符合模式定义(由于 attributeFormDefault="unqualified"ns1 中),但再想一想说得通.这是我的理解:前缀是必要的,因为属性 myAttrib1myAttrib2 没有在 targetNamespace 默认值中定义使用它们的命名空间,即它们没有在 xmlns="http://www.test.com" 中定义.

Now, using Xerces 2.11.0, I have to prefix attributes myAttrib1 and myAttrib2 with ns1 for it to pass validation. At first thought I felt this was not in conformance with the schema definitions (due to the attributeFormDefault="unqualified" in ns1), but giving it a second thought it kind of makes sense. Here's how I understand it: the prefix is necessary because attributes myAttrib1 and myAttrib2 are not defined in the targetNamespace default namespace where they are used, i.e. they're not defined in xmlns="http://www.test.com".

这是我的问题:1) 我是否正确理解了前缀所述属性的必要性,以及 2) 在 W3C rec.txt 中的位置.我可以找到一段描述这种行为的文章吗?谢谢.:)

Here's my questions: 1) have I understood the necessity of prefix said attributes correctly, and 2) where in the W3C rec. may I find a passage that describes this behavior? Thanks. :)

更新:我遇到了以下段落

UPDATE: I came across the following passage

最后两个属性(elementFormDefault 和 attributeFormDefault)是 W3C XML Schema 提供的一种工具,用于在单个模式,属性和元素是否被默认考虑为被限定(在命名空间中).这种区分合格和不合格可以通过指定默认值来表示,如上面,而且在定义元素和属性时,通过添加一个合格或不合格值的表单属性.

The final two attributes (elementFormDefault and attributeFormDefault) are a facility provided by W3C XML Schema to control, within a single schema, whether attributes and elements are considered by default to be qualified (in a namespace). This differentiation between qualified and unqualified can be indicated by specifying the default values, as above, but also when defining the elements and attributes, by adding a form attribute of value qualified or unqualified.

需要注意的是,只有本地元素和属性可以指定为不合格.所有全局定义的元素和属性必须始终合格.

It is important to note that only local elements and attributes can be specified as unqualified. All globally defined elements and attributes must always be qualified.

xml.com 上找到.另一个有趣的来源是 zvon.org.所以这确实支持了公认的答案.但是,我还没有看到(众所周知的神秘)W3C rec 的确切位置.详细提到了这一点.毕竟,他们是这个问题的管理机构.

found over at xml.com. Another interesting source is zvon.org. So this really backs up the accepted answer. However, I have yet to see exactly where the (notoriously cryptic) W3C rec. mentions this in detail. After all, they are the governing body in this question.

推荐答案

1) 正确.但是,attributeFormDefaultelementFormDefault 仅适用于本地定义的属性和元素.换句话说,那些不是 schemaredefine 的直接子代,嵌套在其他架构组件中.

1) Correct. However, the attributeFormDefault and elementFormDefault apply only to locally defined attributes and elements. In other words, those that are not immediate children of schema and redefine, nested within other schema components.

2) 第 3.2 节.2 表明 ref 必须是 QName.通常,在 XSD 中的任何地方您都会看到 ref 属性,它将是 QName 类型.

2) Section 3.2.2 shows that ref must be a QName. In general, everywhere in the XSD you see a ref attribute, it'll be of QName type.

这篇关于为什么我必须为导入的 XSD 中的属性添加前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆