【问题标题】:How many different XML Namespaces are in this document?该文档中有多少个不同的 XML 命名空间?
【发布时间】:2016-05-12 12:59:59
【问题描述】:

好的,我有这个文件:

    <?xml version="1.0" encoding="utf-8"?>
    <md:madcow xmlns:md="urn:Annotation"
               xsi:schemaLocation="urn:Annotation D:\projects\DELOS\Annotation.xsd"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns ="urn:Annotation"
               kind="withContent" media="text">
        <md:metadata>
            <md:author>ferro697702212</md:author>
            <md:title> Eine weitere Annotation </md:title>
            <md:creationDate>18/11/2009 10.26.09</md:creationDate>
            <md:modificationDate>18/11/2015 10.26.09</md:modificationDate>    

            <md:sourceHttp>687474703a2f2f7777772e676f6f676c652e69742f</md:sourceHttp>
                <md:type>example</md:type>
                <md:public>true</md:public>
        </md:metadata>
        <annotationBody xmlns="urn:Annotation">
            <contents id="1">
                <textContent>Das ist ein Beispieltext für eine Annotation</textContent>
                <attachments>
                   <attachedImage> file:://A/B/C</attachedImage>
                </attachments>
            </contents>

            <contents id="2">
                <md:textContent> Eine weitere Annotation </md:textContent>
                <attachments>
                    <attachedAudio>http://www.h_da.de/xml/test.mp3</attachedAudio>
                </attachments>
            </contents>

            <textSelection>
                <path>BODY/CENTER/FORM/TABLE[2]/TBODY/TR[2]/TD/FONT/LABEL[3],23,6</path>
                <contentRef>1</contentRef>
            </textSelection>
        </annotationBody>
    </md:madcow>

这是我的问题:

此文档中有多少个不同的 XML 命名空间?

据我所知,第一个:

xmlns:md="urn:Annotation"

这意味着这是第一个。

那么我有:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

这是第二个。

那么我们有:

xsi:schemaLocation="urn:Annotation D:\projects\DELOS\Annotation.xsd"

它告诉我们 xsi 命名空间的架构是在哪里定义的。

那么我们有:

xmlns="urn:Annotation"

告诉我们默认命名空间是什么。

最后我们有:

<annotationBody xmlns="urn:Annotation">

这告诉我们在这里我们覆盖了这个特定元素的默认命名空间。顺便说一句,这是最让我困惑的地方,因为既然它是默认命名空间,为什么我们需要在这里声明命名空间?

是啊……

我是否正确理解/阅读了代码,如果没有,请告诉我为什么以及本文档中有多少不同的命名空间?

提前感谢大家。

【问题讨论】:

  • 不清楚为什么&lt;annotationBody xmlns="urn:Annotation"&gt; 有多余的默认命名空间声明,但是是的,你正确理解了这个 XML 命名空间 AFAICS

标签: xml xsd namespaces xml-namespaces


【解决方案1】:

此文档中有多少个不同的 XML 命名空间?

没有。

正如@Damien_The_Unbeliever 正确指出的那样,文档中出现了两个不同的命名空间URI:urn:Annotationhttp://www.w3.org/2001/XMLSchema-instance。但是这些命名空间不在文档“内部”,它们在文档外部。这就是命名空间的全部意义所在,它们不是文档本地的。

如果我计算正确,文档包含 4 个命名空间声明,代表 3 个不同的命名空间绑定到 2 个不同的命名空间。

【讨论】:

    【解决方案2】:

    本文档中有两个不同的命名空间,分别是urn:Annotationhttp://www.w3.org/2001/XMLSchema-instance

    urn:Annotation 被提到了 3 次,它既是文档的默认命名空间,也与 md 命名空间相关联前缀。命名空间 前缀 是任意的,仅在定义它们的元素中才有意义。

    这意味着以下三个元素中的每一个都在 same 命名空间中,并且携带完全相同的信息内容:

    <Element xmlns="urn:example"/>
    <a:Element xmlns:a="urn:example"/>
    <b:Element xmlns:b="urn:example"/>
    

    在您的特定文档中,存在一定数量的冗余,因为默认命名空间不需要annotationBody元素中重新分配.我们也确实需要 md 前缀。

    这份文件携带完全相同的信息

    <?xml version="1.0" encoding="utf-8"?>
    <madcow
               xsi:schemaLocation="urn:Annotation D:\projects\DELOS\Annotation.xsd"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns ="urn:Annotation"
               kind="withContent" media="text">
        <metadata>
            <author>ferro697702212</author>
            <title> Eine weitere Annotation </title>
            <creationDate>18/11/2009 10.26.09</creationDate>
            <modificationDate>18/11/2015 10.26.09</modificationDate>    
    
            <sourceHttp>687474703a2f2f7777772e676f6f676c652e69742f</sourceHttp>
                <type>example</type>
                <public>true</public>
        </metadata>
        <annotationBody>
            <contents id="1">
                <textContent>Das ist ein Beispieltext für eine Annotation</textContent>
                <attachments>
                   <attachedImage> file:://A/B/C</attachedImage>
                </attachments>
            </contents>
    
            <contents id="2">
                <md:textContent> Eine weitere Annotation </md:textContent>
                <attachments>
                    <attachedAudio>http://www.h_da.de/xml/test.mp3</attachedAudio>
                </attachments>
            </contents>
    
            <textSelection>
                <path>BODY/CENTER/FORM/TABLE[2]/TBODY/TR[2]/TD/FONT/LABEL[3],23,6</path>
                <contentRef>1</contentRef>
            </textSelection>
        </annotationBody>
    </madcow>
    

    【讨论】:

      猜你喜欢
      • 2013-05-11
      • 1970-01-01
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 2010-10-08
      相关资源
      最近更新 更多