You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

XML命名空间问题咨询:crs前缀未绑定错误与xsi:schemaLocation疑问

解决“元素'crs:course'的前缀'crs'未绑定”错误及命名空间相关问题

嘿,我一眼就找到了你这个错误的直接原因——命名空间声明的拼写错误!你在根元素里把xmlns写成了xmins(多了个i,少了个l),这就导致XML处理器根本识别不了你给crs前缀绑定的命名空间,自然会抛出“前缀未绑定”的错误。

1. 先修复拼写错误

把根元素里的xmins:crs改成xmlns:crs,这是第一步要解决的问题:

<crs:course courseID="PSAT-080-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:crs="http://example.com/higheredtestprep/courses/ns" xsi:schemaLocation="http://example.com/higheredtestprep/courses/ns courses.xsd">

2. 解释xsi:schemaLocation的作用

你提到对这个声明的作用不确定,简单来说:

  • 它是W3C XML Schema Instance(xsi)命名空间下的属性,用来映射命名空间URI到对应的Schema文件位置
  • 格式是成对出现的:[命名空间URI] [Schema文件的路径/URL],多个映射之间用空格或换行分隔。
  • 它的作用是让XML处理器知道,哪些元素属于哪个命名空间,并且应该用哪个Schema文件来验证这些元素的结构。

3. 解决firstName/lastName的命名冲突

你现在的instructorstudent下的firstNamelastName都没有加命名空间前缀,所以还是存在命名冲突的风险。要解决这个问题,你需要:

  • student相关的元素绑定你已经声明的stu命名空间(注意你现在stu:students的命名空间声明写错了,应该绑定到你自己的学生命名空间,而不是xsi的URI)。
  • instructor下的元素绑定crs命名空间,或者单独声明一个新的命名空间(比如ins:)来区分。

完整修正后的XML代码

<crs:course courseID="PSAT-080-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:crs="http://example.com/higheredtestprep/courses/ns" xmlns:stu="http://example.com/higheredtestprep/students/ns" xsi:schemaLocation="http://example.com/higheredtestprep/courses/ns courses.xsd http://example.com/higheredtestprep/students/ns studentsvb.xsd">
    <crs:name>
        <crs:title>PSAT Mathematics Course</crs:title>
        <crs:session>5</crs:session>
    </crs:name>
    <crs:description>
        This is a focused course for students who have previously taken the PSAT exam as a sophomore but did not receive a desired score on the mathematics portion or students who will be taking the PSAT and wish to strengthen their skills in the mathematics area. Outcomes of the course will be measured using the original PSAT mathematics score compared to a course post-test, or a course pre-test compared to the subsequent PSAT mathematics score.
    </crs:description>
    <crs:instructor>
        <crs:firstName>Rachel</crs:firstName>
        <crs:lastName>Polygoni</crs:lastName>
    </crs:instructor>
    <stu:students>
        <stu:student stuID="I8900-041" courseID="PSAT-080-5">
            <stu:lastName>Garcia</stu:lastName>
            <stu:firstName>Georgianna</stu:firstName>
            <stu:examDate>2017-10-17</stu:examDate>
            <stu:pretest level="M">55</stu:pretest>
            <stu:score>51</stu:score>
        </stu:student>
        <stu:student stuID="I7711-121" courseID="PSAT-080-5">
            <stu:lastName>Smith</stu:lastName>
            <stu:firstName>Ryan</stu:firstName>
            <stu:examDate>2017-10-17</stu:examDate>
            <stu:pretest level="L">25</stu:pretest>
        </stu:student>
        <stu:student stuID="I7012-891" courseID="PSAT-080-5">
            <stu:lastName>Zheng</stu:lastName>
            <stu:firstName>Paddy</stu:firstName>
            <stu:examDate>2017-10-17</stu:examDate>
            <stu:pretest level="H">65</stu:pretest>
        </stu:student>
        <stu:student stuID="I8053-891" courseID="PSAT-080-5">
            <stu:lastName>Steinke</stu:lastName>
            <stu:firstName>Devon</stu:firstName>
            <stu:examDate>2017-10-22</stu:examDate>
            <stu:pretest level="M">51</stu:pretest>
        </stu:student>
        <stu:student stuID="I8154-741" courseID="PSAT-080-5">
            <stu:lastName>Browne</stu:lastName>
            <stu:firstName>Brenda</stu:firstName>
            <stu:examDate>2017-10-22</stu:examDate>
            <stu:pretest level="L">30</stu:pretest>
        </stu:student>
    </stu:students>
</crs:course>

修正说明

  • stu命名空间的声明移到了根元素,这样整个文档都可以复用这个前缀,不用在students元素上重复声明。
  • 给所有课程相关的元素加上了crs前缀,学生相关的加上了stu前缀,这样firstNamelastName就分别属于不同的命名空间,彻底解决了命名冲突。
  • xsi:schemaLocation里同时指定了课程和学生两个命名空间对应的Schema文件。

内容的提问来源于stack exchange,提问作者Michael Horton

火山引擎 最新活动