php – 在XML文档中使用XSD验证属性唯一性

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 在XML文档中使用XSD验证属性唯一性脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试验证 XML文档中存在的所有元素的属性的唯一性.

示例XML:

<exampleXML>
  <a id="1"/>
  <a id="2">
    <b id="1"/>
  </a>
</exampleXml>

我的XSD架构:

<xs:schema elementFormDefault="qualified">
  <xs:element name="exampleXml">
    <xs:complexTyPE>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="a">
          <xs:complexType>
            <xs:complexContent>
              <xs:extension base="baseRuleType">
                <xs:sequence>
                  <xs:element minOccurs="0" maxOccurs="1" name="b">
                    <xs:complexType>
                      <xs:complexContent>
                        <xs:extension base="baseRuleType"/>
                      </xs:complexContent>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:extension>
            </xs:complexContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="duplicateidsForbidden">
      <xs:selector xpath="//"/>
      <xs:field xpath="@id"/>
    </xs:unique>
  </xs:element>
  <xs:complexType name="baseRuleType">
    <xs:attribute name="id" use="optional"/>
  </xs:complexType>
</xs:schema>

xpath是这里的问题.我想匹配root下的每个元素,但上面的选择器xpath返回:

Element '{http://www.w3.org/2001/XMLSchema}selector',attribute 'xpath': 
The XPath exPression '//' Could not be compiled

我可以将xpath更改为“*”,但这只会验证作为根的直接后代的元素的id属性.

我使用DOMDocument :: schemaValidate()在PHP中使用lib_xml验证这一点.任何帮助非常感谢.

使用< xs:selector xpath =“.//*”/>.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 在XML文档中使用XSD验证属性唯一性全部内容,希望文章能够帮你解决php – 在XML文档中使用XSD验证属性唯一性所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。