问题
I'm facing some problems trying to create a RAML unsing Library to define the types for a XML. Looks like is propagating the prefix to all the atributes.
the library is like this:
#%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'
The RAML is this:
#%RAML 1.0
title: book test
uses:
myLib: /libraries/types.raml
/book:
description: book
post:
body:
application/xml:
type: myLib.book
This is the XML that is send for the API:
<?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>
And I'm getting this error:
{
"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.. "
}
回答1:
Adding another answer, to clean up the messy thread and allow the conversation to continue without causing confusion...
Your xsd has two absolutely huge faults.
- You have not declared the global element 'book'. The error message was pretty clear, so that's a miss by you.
- You have not declared a target namespace for the global elements in this XSD
I have changed your XSD below, and it is much more likely to work than the one that you posted. Please give it a try.
<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>
回答2:
I think I know what is happening here. In your XML document, the root tag 'book' is in namespace '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 So you could modify your definition of 'book' as follows:
#%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: ''
If that does not work, you can replace the entire defintion of the 'book' type with a (manually created) file 'book.xsd'. Make sure that the 'elementFormDefault' attribute on the xs:schema tag is set to 'unqualified'. Then reference book.xsd from your RAML.
even when trying to use a XSD I get a error:
<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>
the error:
{
"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'.. "
}
来源:https://stackoverflow.com/questions/59195335/namespace-in-raml-file