如何解决JSon Schema to XSD - 不保留 OneOf 的模式
我目前正在尝试“即时”将 Json Schema 转换为 XSD 文件,我刚刚注意到......一个不太理想的错误。
我注意到由以下模式(以及其他模式)生成的字符串模式没有被转移,并且不确定错误的根源。
Java 代码片段:
function ALTERNATIVE(){
// Get all the static HTML text of the website
const res = UrlFetchApp.fetch('https://finviz.com/quote.ashx?t=AAPL',{muteHttpExceptions: true}).getContentText();
// Find the index of the string of the parameter we are searching for
index = res.search("Forward P/E");
// create a substring to only get the right number values ignoring all the HTML tags and classes
sub = res.substring(index+68,index+73);
Logger.log(sub);
return sub;
}
Json 架构片段:
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;
}
编辑 1
这使用 Config 块的 Jsons2XSD 代码。其他一切都是基本的 Java/ORG Xml 代码
生成的 XSD
{"$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}$"}]}}}
编辑 2:
为了参考示例,时间戳模式不会传递到 XSD,只是 String 的 Object 类型。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。