targetNamespace 有什么作用?我做对了吗? [英] What does targetNamespace do ? Am I getting it right?

查看:144
本文介绍了targetNamespace 有什么作用?我做对了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 XSD 的 标签:

Here is the <schema> tag of my XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.cmu.edu/ns/blank"
    targetNamespace="http://www.cmu.edu/ns/blank"
    elementFormDefault="qualified">  

如果我的理解是正确的,这就是它的意思:

  • 这个模式本身属于 http://www.w3.org/2001/XMLSchema 命名空间
  • XML 实例的根应该属于 http://www.cmu.edu/ns/blank 命名空间
  • XML 实例中所有没有前缀的元素自动属于 http://www.cmu.edu/ns/blank 命名空间,因为 elementFormDefault合格
  • If my understanding is correct, here is what it means:

  • This schema itself belongs to http://www.w3.org/2001/XMLSchema namespace
  • The root of the XML instance should belong to http://www.cmu.edu/ns/blank namespace
  • All the elements within the XML instance which do not have a prefix automatically belong to http://www.cmu.edu/ns/blank namespace as elementFormDefault is qualified
  • 问题 1:我的理解是否正确.如果没有,有什么问题?

    Question1: Is my understanding correct. If not, what is wrong ?

    问题 2 看下面提到的 XML 实例:

    Question2 Look at the undermentioned XML instance:

    <people
    xmlns="http://www.cmu.edu/ns/blank"
    xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.cmu.edu/ns/blank student.xsd"
    >
        <student>
            <name>John</name>
            <course>Computer Technology</course>
            <semester>6</semester>
            <scheme>E</scheme>
        </student>
    </people>  
    

    这里一切都属于http://www.cmu.edu/ns/blank命名空间,包括和元素由于 elementFormDefault 而包含在其中.正确吗?

    Here everything belongs to http://www.cmu.edu/ns/blank namespace including <student> and the elements contained within because of elementFormDefault. Correct?

    问题 3
    现在,我想添加来自各个大学的 .带有像伯克利的 berk、哈佛的 harv 等前缀.每个 都有一组不同的元素.我想验证一下.这怎么可能?

    Question3
    Now, I want to add <student> from various Universities. with prefixes like berk for Berkley, harv for Harvard, etc. Each <student> has a different set of elements within. And I want to validate that. How is that possible?

    推荐答案

    (1) 前两点可以;第三个:

    (1) The first two points are OK; the third one:

    XML 实例中所有没有前缀的元素自动属于 http://www.cmu.edu/ns/blank 命名空间,因为 elementFormDefault 是合格的

    不正确.

    在架构中声明前缀并不意味着 XML 实例必须使用相同的前缀.XSD 文件中的任何命名空间声明,仅适用于 XSD 的 XML 文件(XSD 是 XML,因此...)

    Declaring a prefix in the schema doesn't mean that the XML instance must use the same prefixes. Any namespace declaration in the XSD file, applies only to the XML file that is XSD (XSD is an XML, therefore...)

    一般来说,没有办法对任何带前缀或不带前缀的元素名称做任何假设;即下面的例子都是正确的.

    In general, there is no way to assume anything about any prefixed or un-prefixed element name; i.e. below examples are all correct.

    <some xmlns="" .../>
    <some xmlns="urn:tempuri-org:XSD:1" .../>
    <x:some xmlns:x="urn:tempuri-org:XSD:1" .../>
    

    唯一确定的是,表示非限定名称的唯一方法是通过没有前缀的名称(即不能为空"命名空间添加前缀).

    The only sure thing is that the only way to represent an unqualified name is through a name without a prefix (i.e. one cannot prefix the "empty" namespace).

    elementFormDefault 控制元素名称的形式,当元素在内容模型中声明时(即不是全局的).

    elementFormDefault controls the form of the element's name, when an element is declared within a content model (i.e. is not global).

    (2) 部分正确.因为 elementFormDefault. 部分不正确.同样,XSD 只是一种模式规范;XML 存在并有自己的规则,与 XSD 或任何其他模式语言无关.此处适用的规则是 XML 命名空间,特别是 范围.

    (2) Partially correct. The part because of elementFormDefault. is incorrect. Again, XSD is just one schema spec; XML exists and has its own rules, irrespective of XSD, or any other schema language. The rule that applies here is that of XML namespaces, specifically scoping.

    (3) 您必须为每个命名空间创建一个 XSD;在每个命名空间中,您声明学生及其内容.然后定义人员的 XSD 将导入其他 XSD 并适当地引用学生.

    (3) You would have to create an XSD for each namespace; within each namespace, you declare the student and it's content. Then the XSD which defines people would import the other XSDs and reference students appropriately.

    这是一个基本设置:

    伯克利.xsd

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
    <xsd:schema targetNamespace="urn:berkeley-org" xmlns="urn:berkeley-org" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="student"/>   
    </xsd:schema>
    

    哈佛.xsd

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
    <xsd:schema targetNamespace="urn:harvard-org" xmlns="urn:harvard-org" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="student"/>   
    </xsd:schema>
    

    people.xsd

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
    <xsd:schema targetNamespace="urn:people-org" xmlns="urn:people-org" xmlns:harv="urn:harvard-org" xmlns:berk="urn:berkeley-org" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:import namespace="urn:harvard-org"  schemaLocation="harvard.xsd"/>
        <xsd:import namespace="urn:berkeley-org" schemaLocation="berkeley.xsd"/>
    
        <xsd:element name="people">
            <xsd:complexType>
                <xsd:choice maxOccurs="unbounded">
                    <xsd:element ref="harv:student"/>
                    <xsd:element ref="berk:student"/>               
                </xsd:choice>
            </xsd:complexType>
        </xsd:element>
    </xsd:schema>
    

    文件图:

    示例 XML(显示名称空间的使用):

    A sample XML (shows the use of namespaces):

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
    <people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:harv="urn:harvard-org" xmlns:berk="urn:berkeley-org" xmlns="urn:people-org">
        <harv:student/>
        <berk:student/>
    </people>
    

    这篇关于targetNamespace 有什么作用?我做对了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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