RAML 文件中的命名空间

Namespace in RAML file

我在尝试创建 RAML unsing 库来定义 XML 的类型时遇到了一些问题。看起来正在将前缀传播到所有属性。

图书馆是这样的:

#%RAML 1.0 Library

types:  
  book:
    type: object
    properties:
      id:
        type: integer
      title:
        type: string
      author: 
        type: string
    xml:
      prefix: 'smp'
      namespace: 'http://example.com/schema'
      name: 'book'

RAML 是这样的:

#%RAML 1.0

title: book test

uses:
  myLib: /libraries/types.raml

/book:
  description: book
  post:    
    body: 
      application/xml:
        type: myLib.book

这是为 API 发送的 XML:

<?xml version="1.0" encoding="utf-8"?>
<smp:book xmlns:smp="http://example.com/schema">
    <id>0</id>
    <title>string</title>
    <author>string</author>
</smp:book>

我收到这个错误:

{
    "code": "REQUEST_VALIDATION_ERROR",
    "message": "Invalid schema for content type application/xml. Errors: cvc-complex-type.2.4.b: The content of element 'smp:book' is not complete. One of '{\"http://example.com/schema\":id, \"http://example.com/schema\":title, \"http://example.com/schema\":author}' is expected.. "
}

我想我知道这里发生了什么。在您的 XML 文档中,根标签 'book' 位于命名空间 'http://example.com/schema'. The child tags of 'book' are not qualified with a namespace. I have not tried this myself, but I suspect that RAML is automatically propagating the namespace of 'book' onto its properties. However, the RAML specification does allow the xml node to be used on Properties as well as Types: XML Serialization of Type instances 因此,您可以按如下方式修改 'book' 的定义:

#%RAML 1.0 Library

types:  
  book:
    type: object
    xml:
      prefix: 'smp'
      namespace: 'http://example.com/schema'
      name: 'book'
    properties:
      id:
        type: integer
        xml:
          namespace: ''
      title:
        type: string
        xml:
          namespace: ''
      author: 
        type: string
        xml:
          namespace: ''

如果这不起作用,您可以用(手动创建的)文件 'book.xsd' 替换 'book' 类型的整个定义。确保 xs:schema 标签上的 'elementFormDefault' 属性设置为 'unqualified'。然后从您的 RAML 中引用 book.xsd。

即使在尝试使用 XSD 时,我也会收到错误消息:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="id" type="xs:byte"/>
  <xs:element name="title" type="xs:string"/>
  <xs:element name="author" type="xs:string"/>
</xs:schema>

错误:

{
    "code": "REQUEST_VALIDATION_ERROR",
    "message": "Invalid schema for content type application/xml. Errors: cvc-elt.1.a: Cannot find the declaration of element 'smp:book'.. "
}

添加另一个答案,以清理混乱的线程并让对话继续而不会造成混乱...

你的xsd有两个绝对的大错。

  • 您还没有声明全局元素'book'。错误信息很清楚,所以你错过了。
  • 您还没有为这个 XSD
  • 中的全局元素声明一个目标命名空间

我已经在下面更改了您的 XSD,它比您发布的那个更有可能工作。请试一试。

<schema 
    attributeFormDefault="unqualified" 
    elementFormDefault="unqualified" 
    xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://example.com/schema"
>

  <element name="book">
    <complexType>
      <sequence>
        <element name="id" type="byte"/>
        <element name="title" type="string"/>
        <element name="author" type="string"/>
      </sequence>
    </complexType>
  </element>
</schema>