针对xml架构的Xml验证在php中失败但在.NET中有效(VS2010)

时间:2012-07-24 12:08:42

标签: php xml xsd primary-key keyref

我有几个xml文件,但我只会提供一个和我验证它的xsd。我先说一下这个问题。我编辑VS2010中的文件,VS运行时解析器在编辑xml文件时没有显示错误。然后,当我运行一个php命令行脚本,它加载xml文件并根据xml架构验证它时,它以一个提取词结束

  

警告:DOMDocument :: schemaValidate():元素'element':找不到keyref'elementRef'的键序列['answers']匹配。

但是,我真的看不到xsd文件中的错误。

Xml文件:

<domain xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Answer" namespace="Custom\Surveys" xsi:noNamespaceSchemaLocation="../../xsd/domain.xsd">
    <entities>
        <entity name="Answer" main="true" schema="answers"/>
    </entities>
    <bindings>      
    </bindings>
    <key>
        <element entity="answers" property="question_option_id"/>
        <element entity="answers" property="survey_voter_id"/>
    </key>
</domain>

xsd文件中的Xml架构:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">   
    <xs:element name="domain">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="entities">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="entity" minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:attribute name="name" type="xs:string" use="required" id="entityName" />
                                    <xs:attribute name="main" type="xs:boolean" use="optional" default="false" />
                                    <xs:attribute name="schema" type="xs:string" use="required" id="schemaName" />                                  
                                </xs:complexType>                               
                            </xs:element>
                            <xs:element name="multientity" minOccurs="0" maxOccurs="1">
                                <xs:complexType>
                                    <xs:attribute name="name" type="xs:string" use="required" />
                                    <xs:attribute name="schema" type="xs:string" use="required" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>                   
                </xs:element>
                <xs:element name="bindings" minOccurs="0" maxOccurs="1">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="bind" minOccurs="0" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="referenced">
                                            <xs:complexType>
                                                <xs:attribute name="entity" type="xs:string" use="required" />
                                                <xs:attribute name="property" type="xs:string" use="required" />
                                            </xs:complexType>
                                            <xs:keyref name="referencedRef" refer="entityId">
                                                <xs:selector xpath="." />
                                                <xs:field xpath="@entity" />
                                            </xs:keyref>
                                        </xs:element>
                                    </xs:sequence>
                                    <xs:attribute name="entity" type="xs:string" use="required" />
                                    <xs:attribute name="property" type="xs:string" use="required" />
                                </xs:complexType>
                                <xs:keyref name="bindRef" refer="entityId">
                                    <xs:selector xpath="." />
                                    <xs:field xpath="@entity" />
                                </xs:keyref>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="key">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="element" minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:attribute name="entity" type="xs:string" use="required" />
                                    <xs:attribute name="property" type="xs:string" use="required" />
                                </xs:complexType>
                                <xs:keyref name="elementRef" refer="entityId">
                                    <xs:selector xpath="." />
                                    <xs:field xpath="@entity" />
                                </xs:keyref>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>                   
                </xs:element>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="required" />            
            <xs:attribute name="namespace" type="xs:string" use="required" />
        </xs:complexType>
        <xs:key name="entityId">
            <xs:selector xpath="./entities/entity|./entities/multientity" />
            <xs:field xpath="@schema" />
        </xs:key>
    </xs:element>
</xs:schema>

2 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,我必须在最后根元素中定义所有键和keyref。

这是新的xsd文件:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">   
    <xs:element name="domain">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="entities">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="entity" minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:attribute name="name" type="xs:string" use="required" id="entityName" />
                                    <xs:attribute name="main" type="xs:boolean" use="optional" default="false" />
                                    <xs:attribute name="schema" type="xs:string" use="required" id="schemaName" />                                  
                                </xs:complexType>                               
                            </xs:element>
                            <xs:element name="multientity" minOccurs="0" maxOccurs="1">
                                <xs:complexType>
                                    <xs:attribute name="name" type="xs:string" use="required" />
                                    <xs:attribute name="schema" type="xs:string" use="required" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>                   
                </xs:element>
                <xs:element name="bindings" minOccurs="0" maxOccurs="1">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="bind" minOccurs="0" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="referenced">
                                            <xs:complexType>
                                                <xs:attribute name="entity" type="xs:string" use="required" />
                                                <xs:attribute name="property" type="xs:string" use="required" />
                                            </xs:complexType>                                           
                                        </xs:element>
                                    </xs:sequence>
                                    <xs:attribute name="entity" type="xs:string" use="required" />
                                    <xs:attribute name="property" type="xs:string" use="required" />
                                </xs:complexType>                               
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="key">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="element" minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:attribute name="entity" type="xs:string" use="required" />
                                    <xs:attribute name="property" type="xs:string" use="required" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>                   
                </xs:element>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="required" />            
            <xs:attribute name="namespace" type="xs:string" use="required" />
        </xs:complexType>
        <xs:key name="entityId">
            <xs:selector xpath="./entities/entity|./entities/multientity" />
            <xs:field xpath="@schema" />
        </xs:key>
        <xs:keyref name="elementRef" refer="entityId">
            <xs:selector xpath="./key/element" />
            <xs:field xpath="@entity" />
        </xs:keyref>
        <xs:keyref name="bindRef" refer="entityId">
            <xs:selector xpath="./bindings/bind" />
            <xs:field xpath="@entity" />
        </xs:keyref>
        <xs:keyref name="referencedRef" refer="entityId">
            <xs:selector xpath="./bindings/bind/referenced" />
            <xs:field xpath="@entity" />
        </xs:keyref>
    </xs:element>
</xs:schema>

答案 1 :(得分:0)

如果可能,我建议始终将keyref及其相应的键放在同一级别(在相同的元素声明中)。它在理论上并不是必需的,但是当你不这样做时,规则非常复杂,有时甚至是反直觉的。 (我没有告诉你规则是什么的原因是我知道阅读这个区域的规格会伤害我的大脑,如果不阅读规范我就不可能记住规则。)