XML与XSD验证

时间:2017-03-12 21:10:03

标签: xml xsd

我有几个XSD并尝试验证我的XML,因为:

第一个(在(vl_1,vl_2)中使用nm_service值) common.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>

第二个(带一些递归) recur.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>

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

但是当我尝试验证时

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

我收到错误qname值没有解析为

的(n)简单类型定义
mySeq <- c()
for (i in seq(0, 4.8, by = 0.8)) {
    mySeq <- c(mySeq, rep(i, 3))
}

mySeq
[1] 0.0 0.0 0.0 0.8 0.8 0.8 1.6 1.6 1.6 2.4 2.4 2.4 3.2 3.2 3.2 4.0 4.0 4.0 4.8 4.8 4.8

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

2 个答案:

答案 0 :(得分:1)

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

要解决的两个主要错误:

  1. 在common.xsd中,xsd:schema是自我封闭的,不应该是。
  2. 在try.xml中,val_1应为vl_1
  3. 以下XSD将成功验证您的XML:

    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" ?>
    <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>
    

答案 1 :(得分:0)

您缺少名称空间前缀,例如

<xsd:element ref="recursive" minOccurs="0" />

应该是

<xsd:element ref="p:recursive" minOccurs="0" />