使用模式验证xml

时间:2016-08-02 20:59:44

标签: xml xsd axapta microsoft-dynamics dynamics-ax-2012

我对此很失落:

我以xml为例:

<?xml version="1.0" encoding="UTF-8"?>
<!--20160722121507-600701340-->
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
   <Action>http://tempuri.org/SalesOrder_AXService/create</Action>
</Header>
  <Body>
    <MessageParts>
       <SalesOrder_AX xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder_AX">
          <SenderId>AX</SenderId>
          <SalesOrderHeaderAIF class="entity">
             <DeliveryMode>1</DeliveryMode>
             <SalesOrderLinesAIF class="entity">
                <DeliveryMode>1</DeliveryMode>
             </SalesOrderLinesAIF>
             <SalesOrderCustomersAIF class="entity">
                <City />
             </SalesOrderCustomersAIF>
          </SalesOrderHeaderAIF>
       </SalesOrder_AX>
    </MessageParts>
  </Body>
</Envelope>

架构:

 <?xml version="1.0" encoding="utf-8"?>
 <xsd:schema targetNamespace="http://schemas.microsoft.com/dynamics/2011/01/documents/Message" 
        xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        elementFormDefault="qualified">

<xsd:element name="Envelope" type="EnvelopeType"/>
<xsd:element name="MessageParts" type="MessagePartsType"/>

<xsd:complexType name="EnvelopeType">
    <xsd:annotation>
        <xsd:documentation xml:lang="en-us">Envelope of the message.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:element name="Header" type="HeaderType"/>
        <xsd:element name="Body" type="BodyType"/>
    </xsd:sequence> 
</xsd:complexType>

<xsd:complexType name="HeaderType">
    <xsd:annotation>
        <xsd:documentation xml:lang="en-us">Header containing message metadata.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>          
        <xsd:element name="Action" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="BodyType">
    <xsd:annotation>
        <xsd:documentation xml:lang="en-us">Body of the message.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:element name="MessageParts" type="MessagePartsType"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="MessagePartsType">
    <xsd:annotation>
        <xsd:documentation xml:lang="en-us">Zero or more parts of the message.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:any namespace="##any" processContents="skip"/>
    </xsd:sequence>
</xsd:complexType>

当然还有我的工作:

static void Job14(Args _args)
{
   XMLTextReader               xmlReader =    XMLTextReader::newFile(@"\\10.2.3.22\Test\XMLS\ax.xml");
   XMLSchema                   xmlSchema = XMLSchema::newFile(@"\\10.2.3.22\Test\XMLS\SOSchema.xsd");
   XmlSchemaValidationError    validationError;
   ;


   xmlReader.read();
   xmlSchema.compile();

   if (! xmlSchema.isCompiled())
       throw error(xmlSchema.validationError().message());

   validationError = xmlReader.validate(xmlSchema.writeToString());

   if (validationError)
       throw error(validationError.message());
}

我的问题是:我无法在架构中定义SalesOrder_AX。该命名空间http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder_AX是不可触及的。

所以,我想我需要进一步为SalesOrder_AX定义一个复杂的类型并保持这样,直到我到达SalesOrderHeaderAIF - &gt; SalesOrderLinesAIF和SalesOrderCustomersAIF。

直到现在它运作良好。但我真的需要更深入地检查前面提到的元素。

我可以得到一个暗示吗?

更新1:

如果我想添加此内容:

<xsd:complexType name="MessagePartsType">
    <xsd:annotation>
        <xsd:documentation xml:lang="en-us">Zero or more parts of the message.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:element name="SalesOrder_AX" type="SalesOrder_AXType"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="SalesOrder_AXType">
    <xsd:annotation>
        <xsd:documentation xml:lang="en-us">Body of the message.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:element name="SenderId" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

我得到的是:

Namespace 'http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder_AX' is not available to be referenced in this schema.

0 个答案:

没有答案