允许来自具有两个模式的特定命名空间的任何元素 [英] Allowing ANY elements from a particular namespace with two schemas

查看:23
本文介绍了允许来自具有两个模式的特定命名空间的任何元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是标题中描述的问题的一个特定实例.我有一个由两个文档共享的命名空间;一个进口另一个.但是,导入似乎混淆了元素any"上的命名空间属性.

Below is a particular instance of the problem described in the title. I have a single namespace shared by two documents; one imports the other. However, the import seems to confuse the namespace attribute on element"any".

让xml验证;any"元素应该只检查元素的目标命名空间.即,只有元素东西"或产品"(及其子元素)应该验证.

To have the xml validate; "any" element should check only the target namespace for elements. I.e., Only element "stuff" or "Product" (and it's children) should validate.

"匹配通配符严格,但找不到声明对于元素'东西'."我要严格匹配!

"The matching wildcard is strict, but no declaration can be found for element 'stuff'." I want strict matching!

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.company.org"
        xmlns="http://www.company.org"
        elementFormDefault="qualified">

<xsd:include schemaLocation="http://www.product.org"/>

<xsd:element name="Company">

    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="Product" type="ProductType"
                         maxOccurs="unbounded"/>

            <xsd:element name="stuff" type="xsd:string" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:schema 
    xmlns="http://www.company.org" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.company.org" 
    elementFormDefault="qualified"
>

<xsd:complexType name="ProductType">
    <xsd:sequence>
       <xsd:any minOccurs="1" maxOccurs="unbounded"
        namespace="targetNamespace" />
    </xsd:sequence>
</xsd:complexType>

<?xml version="1.0"?>
<Company xmlns="http://www.company.org"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.company.org /u/name/test/file1.xsd"
>

    <Product>
            <stuff>Widget</stuff>
    </Product>
    <stuff>text</stuff>
</Company>

想法:

基于错误,看起来好像xml找不到声明stuff"的schema,也就是targetNamespace,http://www.company.org!我不知道为什么会这样.非常感谢帮助,因为这个问题已经困扰我 2 天了.

Thoughts:

Based on the error, it looks as if the xml can't find the schema that declares "stuff", which is the targetNamespace, "http://www.company.org! I'm lost as to why this is the case. Help would be GREATLY appreciated, as this problem has been bugging me for 2 days now.

架构 1(http://www.company.org):

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.company.org"
        xmlns="http://www.company.org"
        elementFormDefault="qualified">

    <xsd:include schemaLocation="http://www.product.org"/>

    <xsd:element name="Company">
        <xsd:complexType>
            <xsd:choice minOccurs="0" maxOccurs="unbounded" >
                <xsd:any namespace="##targetNamespace"  />
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

架构 2(http://www.product.org):

<xsd:schema 
    xmlns="http://www.company.org" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.company.org" 
    elementFormDefault="qualified"
    >

    <xsd:simpleType name="stuff">
        <xsd:restriction base="xsd:string" />
    </xsd:simpleType>

    <xsd:complexType name="ProductType">
        <xsd:sequence>
            <xsd:any minOccurs="1" maxOccurs="unbounded" 
            namespace="http://www.company.org" processContents="strict" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="Product" type="ProductType" />
    <xsd:element name="stuff" type="stuff" />

</xsd:schema>

到目前为止,这个解决方案对我来说效果很好.元素 "any namespace="##targetNamespace"/" 将找到包含在中央的每个元素,包括文件.这种设置的美妙之处在于,命名空间是同构的,所以我可以忽略 xml 和 xsd 文件中的前缀,同时包括任意数量的支持模式,但我只需要一个文件来验证.

This solution has worked beautifully for me so far. Element "any namespace="##targetNamespace" /" will find each element included in the central, including file. The beauty is in that with this set up, the namespace is homogeneous, so I can ignore prefixes in both the xml and xsd files, while including any number of supporting schema, but I only need one file to validate against.

欢迎反馈:D

推荐答案

看,你有这个错误:

匹配的通配符是严格的,但找不到元素stuff"的声明.

注意,它没有说明元素的命名空间.相反,它只是找不到元素 stuff!

Notice, it says nothing about namespaces of elements. Rather, it just cannot find a declaration for the element stuff!

然而,从表面上看,您确实声明了该元素:

Yet, on the surface, it appears you did declare that element:

<xsd:element name="Company">
  <xsd:complexType>
    <xsd:sequence>
       <xsd:element name="Product" type="ProductType"
                    maxOccurs="unbounded"/>

       <xsd:element name="stuff" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

那么,有什么问题吗?

问题是您已经本地声明了它.它的实际意思是,根据你的模式,您的 元素只能作为 元素的子元素有效.它不能在其他任何地方找到!

The problem is you have declared it locally. What it actually means is that, according to your schemas, your <staff> element can be valid only as a child of <Company> element. It must not be found anywhere else!

但是,在您的 XML 中:

However, in your XML:

<Product>
        <stuff>Widget</stuff>
</Product>
<stuff>text</stuff>

您确实希望将 也用作 的子项,您的架构未提供.

you do want to use <stuff> also as a child of <Product>, which is not provided by your schema.

XML 验证器不会说您的 元素不能在 因为它是 的本地子代.对它来说,本地元素是由路径决定的:

The XML validator doesn't say that your <stuff> element cannot be used within <Product> because it is local child of <Company>. To it, the local element is determined by the path:

Company/stuff

当它在 中找到 时,它会寻找路径:

When it finds <stuff> within <Product>, it looks for a path:

Company/Product/stuff

这是它不知道的.然后,它只是说它找不到 的声明.它不会进一步分析您可能做错了什么.

which is unknown to it. Then, it just says it cannot find a declaration for <stuff>. It doesn't analyze further what you might have done wrong.

所以,问题实际上不在于命名空间,而在于本地声明的元素.你应该重新设计你的架构来修复它!

So, the problem is actually not about namespaces but about locally declared elements. You should redesign your schemas to fix it!

这篇关于允许来自具有两个模式的特定命名空间的任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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