有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

未保留其中一个的java JSon模式到XSD模式

我目前正在尝试“动态地”将Json模式转换为XSD文件,我刚刚注意到。。。。一个不太理想的错误

我注意到下面的模式(以及其他模式)产生的字符串模式不会被传输,并且不确定错误的根源

Java代码片段:

public String convertJsonToXsd(String json, String name) throws IOException, TransformerException {
    final Config cfg = new Config.Builder().targetNamespace("http://example.com/myschema.xsd")
            .rootElement(name)
            .name(name)
            .ignoreUnknownFormats(true).build();
    Reader jsonReader = new StringReader(json);
    final Document doc = Jsons2Xsd.convert(jsonReader, cfg);
    jsonReader.close();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    DOMSource domSource = new DOMSource(doc);
    transformer.transform(domSource, sr);
    String constructed = sw.toString();
    return constructed;
}

Json模式片段:

{"$schema":"http://json-schema.org/draft-04/schema#","title":"Example using definitions and references","type":"object","properties":{"from":{"$ref":"#/definitions/timestampDef"},"to":{"$ref":"#/definitions/timestampDef"}},"definitions":{"timestampDef":{"type":"string","oneOf":[{"pattern":"^now([+-][0-9]+[smhdMy])?$"},{"pattern":"^[0-9]{12,14}$"}]}}}

编辑1

这将为配置块使用Jsons2XSD代码。其他一切都是基本的Java/OrgXML代码

生产XSD

<?xml version="1.0" encoding="UTF-8"?><schema xmlns:x="http://example.com/myschema.xsd" elementFormDefault="qualified" targetNamespace="http://example.com/myschema.xsd" xmlns="http://www.w3.org/2001/XMLSchema"><element name="JsonToXSDSchema" type="x:JsonToXSDSchema"/><complexType name="JsonToXSDSchema"><sequence><element minOccurs="0" name="from" type="x:timestampDef"/><element minOccurs="0" name="to" type="x:timestampDef"/></sequence></complexType><simpleType name="timestampDef"><restriction base="string"/></simpleType></schema>

编辑2:

作为示例的参考,时间戳模式不会传递到XSD,只是字符串的对象类型


共 (0) 个答案