XML 与 XSD 验证

什么波罗

我有几个 XSD 并尝试根据它们验证我的 XML:

第一个(在 (vl_1, vl_2) 中有 nm_service 值)common.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://tii.ru/crmcom/uiconf/common"/>

    <xsd:simpleType name="nm_service">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="vl_1" />
            <xsd:enumeration value="vl_2" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

第二个(有一些递归)recur.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module 
    (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:p="http://tii.ru/crmcom/uiconf/common">
    <xsd:import namespace="http://tii.ru/crmcom/uiconf/common"
        schemaLocation="common.xsd" />
    <xsd:element name="object">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="recursive" minOccurs="0" />
                <xsd:element ref="center" minOccurs="0" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="recursive">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="recursive" minOccurs="0" />
                <xsd:element ref="center" minOccurs="0" />
            </xsd:sequence>
            <xsd:attribute name="nm_service" type="p:nm_service"></xsd:attribute>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="center" />
</xsd:schema>

具有递归名称的元素可以具有属性 nm_service,其值在 common.xsd 中定义

但是当我尝试验证时

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
    <recursive>
        <recursive nm_service="val_1" >
            <recursive>
                <center>
                    <recursive>
                    </recursive>
                </center>
            </recursive>
        </recursive>
    </recursive>
</object>

我收到错误 qname 值未解析为 (n) 简单类型定义

<xsd:attribute name="nm_service" type="p:nm_service"></xsd:attribute>

任何帮助,将不胜感激。谢谢

克休斯

注意:您发布的 XSD 似乎与您报告的错误消息不一致。

要修复的两个主要错误:

  1. 在 common.xsd 中,xsd:schema它在不应该出现的地方是自关闭的。
  2. 在 try.xml 中,val_1应该是vl_1.

总之,以下 XSD 将成功验证您的 XML:

常见的.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://tii.ru/crmcom/uiconf/common">

  <xsd:simpleType name="nm_service">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="vl_1" />
      <xsd:enumeration value="vl_2" />
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

递归.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
            elementFormDefault="qualified" 
            xmlns="http://tempuri.org/XMLSchema.xsd"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:p="http://tii.ru/crmcom/uiconf/common">
  <xsd:import namespace="http://tii.ru/crmcom/uiconf/common"
              schemaLocation="common.xsd" />
  <xsd:element name="object">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="recursive" minOccurs="0" />
        <xsd:element ref="center" minOccurs="0" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="recursive">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="recursive" minOccurs="0" />
        <xsd:element ref="center" minOccurs="0" />
      </xsd:sequence>
      <xsd:attribute name="nm_service" type="p:nm_service"></xsd:attribute>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="center" />
</xsd:schema>

XML格式

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://tempuri.org/XMLSchema.xsd"
        xsi:schemaLocation="http://tempuri.org/XMLSchema.xsd recur.xsd">
  <recursive>
    <recursive nm_service="vl_1" >
      <recursive>
        <center>
          <recursive>
          </recursive>
        </center>
      </recursive>
    </recursive>
  </recursive>
</object>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章