[英]how to convert json to xml with saxonjs?
I need to convert a json to xml with saxonjs, I don't know how to match keys to xml nodes, I'm looking for some examples for none of them work for me, this is my code我需要使用 saxonjs 将 json 转换为 xml,我不知道如何将键与 xml 节点匹配,我正在寻找我的代码示例
const issue = {
id: 1,
details: {
type: 'urgent',
description: 'Description of issue comes here',
date: '2021-12-12',
}
};
saxonJS.transform({
stylesheetLocation: './issue.sef.json',
sourceType: 'json',
sourceText: issue,
destination: 'serialized',
}, 'async').then(data => {
fs.open('output.xml', 'w', function(err, fd) {
fs.write(fd, data.principalResult, (err2, bytes) => {
if(err2) {
console.log(err2);
}
});
});
res.status(200).send('Ok');
})
.catch(err => {
console.log(err);
res.status(500).send('error');
});
And this is the output I'm trying to achieve这就是我想要实现的 output
<xml>
<issue id="1">
<description>
<![CDATA[
Description of issue comes here
]]>
</description>
<type>urgent</type>
<date>2021-12-12</date>
</issue>
</xml>
Can you please help me with the xslt template?你能帮我看看 xslt 模板吗?
Your shown input is a JavaScript object, it is not JSON in the strict syntax rules of the JSON spec.你显示的输入是 JavaScript object,它不是 JSON 在严格的语法规则中的 Z0ECD11C14D7A28D74A3D
So I would think it is better to use JSON.stringify
to create JSON and pass that to the XPath 3.1 function parse-json
to create JSON or to make use of the Saxon-JS 2.3 feature to take JSON text, just make sure you have correctly JSON.stringify
ed that object. So I would think it is better to use JSON.stringify
to create JSON and pass that to the XPath 3.1 function parse-json
to create JSON or to make use of the Saxon-JS 2.3 feature to take JSON text, just make sure you have正确JSON.stringify
编辑了 object。
As for a sample XSLT, that looks easy, for readability of the XSLT the below sample just uses a JavaScript string with the XSLT source code and runs it through the Saxon API: As for a sample XSLT, that looks easy, for readability of the XSLT the below sample just uses a JavaScript string with the XSLT source code and runs it through the Saxon API:
const SaxonJS = require("saxon-js");
const issue = {
id: 1,
details: {
type: 'urgent',
description: 'Description of issue comes here',
date: '2021-12-12',
}
};
const xslt = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" expand-text="yes">
<xsl:output indent="yes" cdata-section-elements="description"/>
<xsl:template match=".">
<xml>
<issue id="{?id}">
<description>{?details?description}</description>
<type>{?details?type}</type>
<date>{?details?date}</date>
</issue>
</xml>
</xsl:template>
</xsl:stylesheet>`;
const result = SaxonJS.XPath.evaluate(`transform(map {
'stylesheet-text' : $xslt,
'initial-match-selection' : parse-json($json),
'delivery-format' : 'serialized'
})?output`,
[],
{ params :
{
json : JSON.stringify(issue),
xslt : xslt
}
});
Of course, in the end you can first compile the XSLT to SEF/JSON and then run it as you tried.当然,最后你可以先将 XSLT 编译成 SEF/JSON,然后按照你的尝试运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.