cvc-complex-type.2.4.c:匹配通配符严格,但找不到声明 [英] cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found

查看:27
本文介绍了cvc-complex-type.2.4.c:匹配通配符严格,但找不到声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解 xsd 中的 元素.我有两个 xsd.

图书目录.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"elementFormDefault="合格"><xs:element name="BookCatalog"><xs:complexType><xs:序列><xs:element name="Book" maxOccurs="unbounded"><xs:complexType><xs:序列><xs:element name="Title" type="xs:string"/><xs:element name="作者" type="xs:string"/><xs:element name="Date" type="xs:string"/><xs:element name="ISBN" type="xs:string"/><xs:element name="Publisher" type="xs:string"/><xs:any namespace="##any" minOccurs="0"/></xs:sequence></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element></xs:schema>

Reviewer.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"elementFormDefault="合格"><xs:element name="审阅者"><xs:complexType><xs:序列><xs:元素名称="名称"><xs:complexType><xs:序列><xs:element name="First" type="xs:string"/><xs:element name="Last" type="xs:string"/></xs:sequence></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element></xs:schema>

但是如果我根据上面的 xsd 验证下面的 xml,我得到 cvc-complex-type.2.4.c:匹配的通配符是严格的,但是找不到元素p:Reviewer"的声明. 错误.两个 xsd 文件不应该在同一个命名空间中吗?

<公关:书><pr:Title>pr:Title</pr:Title><pr:作者>pr:作者</pr:作者><pr:日期>pr:日期</pr:日期><pr:ISBN>pr:ISBN</pr:ISBN><pr:Publisher>pr:Publisher</pr:Publisher><p:Reviewer xmlns:p="http://www.w3schools.com"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.w3schools.com Children.xsd "><p:姓名><p:First>p:First</p:First><p:Last>p:Last</p:Last></p:姓名></p:审稿人></pr:Book></pr:BookCatalog>

解决方案

两个选项...

选项一:如果您不想必须有p:Reviewer的定义,请添加processContents=宽松" 到您的 xs:any 元素:

 

根据 XML 架构第 0 部分:入门第二版::><块引用>

processContents 属性的松散值指示 XML处理器在可以做的基础上验证元素内容:它将验证可以为其获取架构的元素和属性信息,但它不会为无法获得的人发出错误信号任何架构信息.

另请参见 Java 中的 XML 验证:processContents="lax" 似乎无法正常工作.

您还应该仔细调整您的 xsi:schemaLocation 值,以指向正在播放的每个命名空间的每个 XSD 的实际文件名.这是您的 XML 实例,其中包含我所做的更改:

<公关:书><pr:Title>pr:Title</pr:Title><pr:作者>pr:作者</pr:作者><pr:日期>pr:日期</pr:日期><pr:ISBN>pr:ISBN</pr:ISBN><pr:Publisher>pr:Publisher</pr:Publisher><p:审稿人><p:姓名><p:First>p:First</p:First><p:Last>p:Last</p:Last></p:姓名></p:审稿人></pr:Book></pr:BookCatalog>

注意:确保 Review.xsd 中的 targetNamespace 与 BookCatalogue.xml 的 xsi:schemaLocation 属性中为其声明的内容相匹配.

选项二:如果您确实想坚持p:Reviewer的定义存在,只需将上述更改为确保可以根据 xsi:schemaLocation 机制找到 Review.xsd.不需要 processContents 设置;它默认为 strict.

I am trying to understand <any> element in xsd. I had two xsds.

Book Catalogue.xsd

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
    elementFormDefault="qualified">
    <xs:element name="BookCatalogue">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Title" type="xs:string" />
                            <xs:element name="Author" type="xs:string" />
                            <xs:element name="Date" type="xs:string" />
                            <xs:element name="ISBN" type="xs:string" />
                            <xs:element name="Publisher" type="xs:string" />
                            <xs:any namespace="##any" minOccurs="0" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema> 

Reviewer.xsd

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
    elementFormDefault="qualified">

    <xs:element name="Reviewer">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="First" type="xs:string" />
                            <xs:element name="Last" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>



</xs:schema> 

But if i validate the below xml based on above xsd, i am getting cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'p:Reviewer'. error. Does both xsd file should not be in same namespace?

<?xml version="1.0" encoding="UTF-8"?>
<pr:BookCatalogue xmlns:pr="http://www.w3schools.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3schools.com AddRequest.xsd ">
    <pr:Book>
        <pr:Title>pr:Title</pr:Title>
        <pr:Author>pr:Author</pr:Author>
        <pr:Date>pr:Date</pr:Date>
        <pr:ISBN>pr:ISBN</pr:ISBN>
        <pr:Publisher>pr:Publisher</pr:Publisher>
        <p:Reviewer xmlns:p="http://www.w3schools.com"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.w3schools.com Children.xsd ">
            <p:Name>
                <p:First>p:First</p:First>
                <p:Last>p:Last</p:Last>
            </p:Name>
        </p:Reviewer>
    </pr:Book>
</pr:BookCatalogue>

解决方案

Two options...

Option One: If you do not want to have to have the definition of p:Reviewer present, add processContents="lax" to your xs:any element:

      <xs:any namespace="##any" minOccurs="0"  processContents="lax"/>

Per XML Schema Part 0: Primer Second Edition:

The lax value of the processContents attribute instructs an XML processor to validate the element content on a can-do basis: It will validate elements and attributes for which it can obtain schema information, but it will not signal errors for those it cannot obtain any schema information.

See also XML Validation in Java: processContents="lax" seems not to work correctly.

You should also carefully adjust your xsi:schemaLocation values to point to the actual filename of each XSD for each namespace in play. Here is your XML instance with the changes that I made:

<?xml version="1.0" encoding="UTF-8"?>
<pr:BookCatalogue
    xmlns:pr="http://www.w3schools.com"
    xmlns:p="http://www.w3schools.com/1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3schools.com BookCatalogue.xsd http://www.w3schools.com/1 Reviewer.xsd">
    <pr:Book>
        <pr:Title>pr:Title</pr:Title>
        <pr:Author>pr:Author</pr:Author>
        <pr:Date>pr:Date</pr:Date>
        <pr:ISBN>pr:ISBN</pr:ISBN>
        <pr:Publisher>pr:Publisher</pr:Publisher>
        <p:Reviewer>
            <p:Name>
                <p:First>p:First</p:First>
                <p:Last>p:Last</p:Last>
            </p:Name>
        </p:Reviewer>
    </pr:Book>
</pr:BookCatalogue>

Note: Make sure that the targetNamespace in Review.xsd matches what's declared for it in BookCatalogue.xml's xsi:schemaLocation attribute.

Option Two: If you do want to insist that the definition of p:Reviewer be present, just make the above changes to be sure that Review.xsd can be found per the xsi:schemaLocation mechanism. No processContents setting is required; it defaults to strict.

这篇关于cvc-complex-type.2.4.c:匹配通配符严格,但找不到声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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