是否有jazxswpoi相当于JSON?允许我对XSON进行转换的事情,比如XSLT对XML做的转换。
有趣的想法。一些在Google上搜索产生了一些感兴趣的页面,包括:
希望这可以帮助。
现在有!我最近创建了一个库,json-transforms,正是为了这个目的:
Discount of #983: 20
它使用https://github.com/ColinEberhardt/json-transforms(一种基于XPath建模的DSL)和一种直接受XSLT启发的递归模式匹配方法的组合。
这是一个简单的例子。给定以下JSON对象:
JSPath
这是一个转变:
const json = {
"automobiles": [
{ "maker": "Nissan", "model": "Teana", "year": 2011 },
{ "maker": "Honda", "model": "Jazz", "year": 2010 },
{ "maker": "Honda", "model": "Civic", "year": 2007 },
{ "maker": "Toyota", "model": "Yaris", "year": 2008 },
{ "maker": "Honda", "model": "Accord", "year": 2011 }
]
};
哪个输出如下:
const jsont = require('json-transforms');
const rules = [
jsont.pathRule(
'.automobiles{.maker === "Honda"}', d => ({
Honda: d.runner()
})
),
jsont.pathRule(
'.{.maker}', d => ({
model: d.match.model,
year: d.match.year
})
),
jsont.identity
];
const transformed = jsont.transform(json, rules);
该变换由三个规则组成。第一个匹配任何由本田制造的汽车,发射具有{
"Honda": [
{ "model": "Jazz", "year": 2010 },
{ "model": "Civic", "year": 2007 },
{ "model": "Accord", "year": 2011 }
]
}
属性的物体,然后递归匹配。第二个规则匹配任何具有Honda
属性的对象,输出maker
和model
属性。最后是递归匹配的身份转换。
作为旧问题的另一个新答案,我建议看看year
。它不是JSON的XSLT等价物,它是JSON的XSLT。文档的“模板”部分包括以下示例:
DefiantJS
我已经厌倦了大量的JavaScript模板引擎,以及所有内联HTML模板,不同的标记样式等,并决定使用<!-- Defiant template -->
<script type="defiant/xsl-template">
<xsl:template name="books_template">
<xsl:for-each select="//movie">
<xsl:value-of select="title"/><br/>
</xsl:for-each>
</xsl:template>
</script>
<script type="text/javascript">
var data = {
"movie": [
{"title": "The Usual Suspects"},
{"title": "Pulp Fiction"},
{"title": "Independence Day"}
]
},
htm = Defiant.render('books_template', data);
console.log(htm);
// The Usual Suspects<br>
// Pulp Fiction<br>
// Independence Day<br>
为JSON数据结构启用XSLT格式化。这不是火箭科学 - 它只是将JSON解析为XML,然后使用XSLT文档进行格式化。它也很快,不像Chrome中的JavaScript模板引擎那么快,但在大多数其他浏览器中,它至少与大型数据结构的JS引擎替代方案一样快。
build a small library是一个标准,JSONiq是一个开源的C ++实现。通过将JSON添加为本机数据类型,JSONiq也可以被视为XQuery。
我正在使用Camel route unmarshal(xml json) - > to(xslt) - > marshal(xmljson)。足够高效(虽然不是100%完美),但很简单,如果你已经使用Camel。
使用XSLT转换JSON是非常可能的:您需要JSON2SAX反序列化器和SAX2JSON序列化器。
Java中的示例代码:Zorba
Yate(http://www.gerixsoft.com/blog/json/xslt4json)是在XSLT之后专门设计的,具有JPath(JS的自然XPath等价物),编译为JavaScript并且具有相当的生产历史。它实际上没有文档,但阅读样本和测试应该足够了。
https://github.com/pasaran/yate非常接近JSON等效的XSLT。它是一种转换语言,您可以使用JSON语法编写输出的固定部分,然后插入表达式以计算要在模板中插入的值。
一个例子:
JSLT
它是在Jackson上用Java实现的。
不太确定是否需要这个,而且我缺乏工具表明缺乏需求。 JSON最好作为对象处理(无论如何都是在JS中完成的),并且您通常使用对象本身的语言来进行转换(Java用于从JSON创建的Java对象,对于Perl,Python,Perl,c#,PHP等都是如此)上)。只需使用正常的赋值(或set,get),循环等等。
我的意思是,XSLT只是另一种语言,并且需要的一个原因是XML不是对象表示法,因此编程语言的对象不是精确匹配(分层xml模型和对象/结构之间的阻抗)。
为什么不使用{
"time": round(parse-time(.published, "yyyy-MM-dd'T'HH:mm:ssX") * 1000),
"device_manufacturer": .device.manufacturer,
"device_model": .device.model,
"language": .device.acceptLanguage
}
将JSON转换为XML,使用XSLT对其进行转换,然后使用相同的方法将其更改回JSON。
JSON的XSLT等价物 - 候选列表(工具和规格)
有关使用纯JavaScript以及XSLT匹配表达式和递归模板背后的熟悉和声明模式的方法的工作涂鸦/概念证明,请参阅Mr. Data Coverter
(JSON可能采用类似的方法。)
请注意,该演示还依赖于JavaScript 1.8表达式闭包,以便在Firefox中表达模板(至少在可以实现ES6的方法简介之前)。
免责声明:这是我自己的代码。
很久以前,我为基于杰克逊的json处理框架编写了一个dom适配器。它使用nu.xom库。生成的dom树与java xpath和xslt工具一起使用。我做了一些非常简单的实现选择。例如,根节点总是被称为“根”,数组进入具有li子元素的ol节点(如html中),而其他所有内容只是具有原始值的子节点或另一个对象节点。
尚未给出的一种方法是使用解析器生成器在XSLT中创建解析器,该解析器解析JSON并生成XML输出。
在XML会议上经常提到的一个选项是ReX解析器生成器(
JsonObject sampleJson = sampleJson();
org.w3c.dom.Document domNode = JsonXmlConverter.getW3cDocument(sampleJson, "root");
) - 虽然在网站上完全没有记录,但是搜索时可以使用配方。
可以将XSLT与JSON一起使用。 XPath(3.1)的第3版XSLT(3.0)和XQuery(3.1)以某种方式支持JSON。这似乎可以在Saxon的商业版本中获得,并且可能在某些时候包含在HE版本中。 http://www.bottlecaps.de/rex/
-
我对替代解决方案的期望是什么:
我希望能够输入JSON来获取匹配的数据集,并输出JSON或TEXT。
访问任意属性并评估值
支持条件逻辑
我希望转换脚本来自工具,基于文本,最好是常用语言。
潜在的选择?
我想知道SQL是否是一个合适的选择。 https://www.saxonica.com/html/documentation/functions/fn/parse-json.html
如果替代工具可以处理JSON和XML https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server会很好
我还没有尝试将我使用的XSLT脚本转换为SQL,或者已经完全评估了这个选项,但我希望能够更快地研究它。到目前为止只是一些想法。
试试JSONiq。它是用Java编写的JSON to JSON转换库。
它是专门创建的,因为我们不想播放“JSON - > XML - > XSLT - > XML - > JSON”游戏,并且使用模板进行任何足够复杂的转换都是不可维护的。
XSLT支持JSON,如JOLT所示
XML使用尖括号作为分隔符标记,JSON使用大括号,方括号,...... I. e。 XML更少的令牌识别比较意味着它针对声明性转换进行了优化,而更多的比较,如switch语句,出于速度原因,假设推测分支预测脚本语言中的命令性代码是有用的。直接结果是,对于半结构化数据的不同组合,您可能希望将XSLT和javascript引擎的性能作为响应式页面的一部分进行基准测试。对于可忽略的数据有效负载,转换可能与没有XML序列化的JSON一样好。 W3的决定应该基于更好的分析。
我最近找到了一个我喜欢的JSON样式工具:http://www.w3.org/TR/xslt-30/#json。非常容易使用的工具 - 在我看来,它比XSLT更容易使用 - 不需要XPATH查询。
https://github.com/twigkit/tempo
它不像XSLT那样基于模板,但更简洁。例如把jq - lightweight and flexible command-line JSON processor和name
字段提取成一个数组:address
[.name, .address]
介绍了转换Twitter的JSON API的例子(而且tutorial有很多例子)。
看看manual
说缺乏工具表明缺乏需求只是乞求这个问题。同样可以用于支持Linux中的X或Y(为什么要为这样的少数操作系统开发高质量的驱动程序和/或游戏?为什么要关注大型游戏和硬件公司不开发的操作系统?)。可能需要使用XSLT和JSON的人最终会使用一些简单的解决方法:将JSON转换为XML。但这不是最佳解决方案,是吗?
当你有一个原生的JSON格式,并且你想在浏览器中编辑它“wysywyg”时,XSLT对于这个问题来说是一个绰绰有余的解决方案。使用传统的javascript编程实现这一点可能会成为一种痛苦。
实际上,我已经对XSLT实现了“石器时代”的方法,使用子串解析来解释javascript的一些基本命令,比如调用模板,处理子进程等。当然,使用JSON对象实现转换引擎要比实现一个完整的XML解析器来解析XSLT。问题是,要使用XML模板转换JSON对象,需要解析模板的XML。
要使用XML(或HTML,或文本或其他)转换JSON对象,您需要仔细考虑语法以及需要使用哪些特殊字符来标识转换命令。否则,您最终将不得不为自己的自定义模板语言设计解析器。走过这条路,我可以告诉你,它并不漂亮。
更新(2010年11月12日):经过几周的解析器工作,我已经能够优化它。事先解析模板,并将命令存储为JSON对象。转换规则也是JSON对象,而模板代码是HTML和类似于shell代码的自制语法的混合。我已经能够将复杂的JSON文档转换为HTML以制作文档编辑器。代码大约是1K行的编辑器(它是私有项目所以我不能共享它)和大约990行的JSON转换代码(包括迭代命令,简单比较,模板调用,变量保存和评估)。我计划在MIT许可下发布它。如果你想参与,请给我发邮件。
我最近在这周围写了我自己的小型图书馆,试图保持尽可能接近
5.1处理模型(XSLT REC)jsonpath-object-transform
在可能的情况下(尽我所能),在几行JavaScript代码中。
以下是一些使用不完全无关的例子......
小提琴:https://www.w3.org/TR/xslt#section-Processing-Model
(灵感来自D.1文档示例(XSLT REC)https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10)
这个:
https://www.w3.org/TR/xslt#section-Document-Example
......给出:
var D1document = {
type: "document", title: [ "Document Title" ],
"": [
{ type: "chapter", title: [ "Chapter Title" ],
"": [
{ type: "section", title: [ "Section Title" ],
"": [
{ type: "para", "": [ "This is a test." ] },
{ type: "note", "": [ "This is a note." ] }
] },
{ type: "section", title: [ "Another Section Title" ],
"": [
{ type: "para", "": [ "This is ", { emph: "another" }, " test." ] },
{ type: "note", "": [ "This is another note." ] }
] }
] }
] };
var D1toHTML = { $: [
[ [ function(node) { return node.type === "document"; } ],
function(root) {
return "<html>\r\n\
<head>\r\n\
<title>\r\n\
{title}\r\n".of(root) + "\
</title>\r\n\
</head>\r\n\
<body>\r\n\
{*}".of(root[""].through(this)) + "\
</body>\r\n\
</html>";
}
],
[ [ function(node) { return node.type === "chapter"; } ],
function(chapter) {
return " <h2>{title}</h2>\r\n".of(chapter) + "{*}".of(chapter[""].through(this));
}
],
[ [ function(node) { return node.type === "section"; } ],
function(section) {
return " <h3>{title}</h3>\r\n".of(section) + "{*}".of(section[""].through(this));
}
],
[ [ function(node) { return node.type === "para"; } ],
function(para) {
return " <p>{*}</p>\r\n".of(para[""].through(this));
}
],
[ [ function(node) { return node.type === "note"; } ],
function(note) {
return ' <p class="note"><b>NOTE: </b>{*}</p>\r\n'.of(note[""].through(this));
}
],
[ [ function(node) { return node.emph; } ],
function(emph) {
return "<em>{emph}</em>".of(emph);
}
]
] };
console.log(D1document.through(D1toHTML));
和
小提琴:<html>
<head>
<title>
Document Title
</title>
</head>
<body>
<h2>Chapter Title</h2>
<h3>Section Title</h3>
<p>This is a test.</p>
<p class="note"><b>NOTE: </b>This is a note.</p>
<h3>Another Section Title</h3>
<p>This is <em>another</em> test.</p>
<p class="note"><b>NOTE: </b>This is another note.</p>
</body>
</html>
这个:
https://jsfiddle.net/YSharpLanguage/ppfmmu15/10
......给出:
// (A "Company" is just an object with a "Team")
function Company(obj) {
return obj.team && Team(obj.team);
}
// (A "Team" is just a non-empty array that contains at least one "Member")
function Team(obj) {
return ({ }.toString.call(obj) === "[object Array]") &&
obj.length &&
obj.find(function(item) { return Member(item); });
}
// (A "Member" must have first and last names, and a gender)
function Member(obj) {
return obj.first && obj.last && obj.sex;
}
function Dude(obj) {
return Member(obj) && (obj.sex === "Male");
}
function Girl(obj) {
return Member(obj) && (obj.sex === "Female");
}
var data = { team: [
{ first: "John", last: "Smith", sex: "Male" },
{ first: "Vaio", last: "Sony" },
{ first: "Anna", last: "Smith", sex: "Female" },
{ first: "Peter", last: "Olsen", sex: "Male" }
] };
var TO_SOMETHING_ELSE = { $: [
[ [ Company ],
function(company) {
return { some_virtual_dom: {
the_dudes: { ul: company.team.select(Dude).through(this) },
the_grrls: { ul: company.team.select(Girl).through(this) }
} }
} ],
[ [ Member ],
function(member) {
return { li: "{first} {last} ({sex})".of(member) };
} ]
] };
console.log(JSON.stringify(data.through(TO_SOMETHING_ELSE), null, 4));
相当于......的JavaScript
{
"some_virtual_dom": {
"the_dudes": {
"ul": [
{
"li": "John Smith (Male)"
},
{
"li": "Peter Olsen (Male)"
}
]
},
"the_grrls": {
"ul": [
{
"li": "Anna Smith (Female)"
}
]
}
}
}
(at:XSLT 3.0 REC Section 14.4 Example: Grouping Nodes based on Common Values)
参看http://jsfiddle.net/YSharpLanguage/8bqcd0ey/1
哪里...
https://www.w3.org/TR/xslt-30/#grouping-examples
......给出:
var cities = [
{ name: "Milano", country: "Italia", pop: 5 },
{ name: "Paris", country: "France", pop: 7 },
{ name: "München", country: "Deutschland", pop: 4 },
{ name: "Lyon", country: "France", pop: 2 },
{ name: "Venezia", country: "Italia", pop: 1 }
];
/*
Cf.
XSLT 3.0 REC Section 14.4
Example: Grouping Nodes based on Common Values
https://www.w3.org/TR/xslt-30/#grouping-examples
*/
var output = "<table>\r\n\
<tr>\r\n\
<th>Position</th>\r\n\
<th>Country</th>\r\n\
<th>City List</th>\r\n\
<th>Population</th>\r\n\
</tr>{*}\r\n\
</table>".of
(
cities.select().groupBy("country")(function(byCountry, index) {
var country = byCountry[0],
cities = byCountry[1].select().orderBy("name");
return "\r\n\
<tr>\r\n\
<td>{position}</td>\r\n\
<td>{country}</td>\r\n\
<td>{cities}</td>\r\n\
<td>{population}</td>\r\n\
</tr>".
of({ position: index + 1, country: country,
cities: cities.map(function(city) { return city.name; }).join(", "),
population: cities.reduce(function(sum, city) { return sum += city.pop; }, 0)
});
})
);
相当于......的JavaScript
<table>
<tr>
<th>Position</th>
<th>Country</th>
<th>City List</th>
<th>Population</th>
</tr>
<tr>
<td>1</td>
<td>Italia</td>
<td>Milano, Venezia</td>
<td>6</td>
</tr>
<tr>
<td>2</td>
<td>France</td>
<td>Lyon, Paris</td>
<td>9</td>
</tr>
<tr>
<td>3</td>
<td>Deutschland</td>
<td>München</td>
<td>4</td>
</tr>
</table>
(at:JSONiq Use Cases Section 1.1.2. Grouping Queries for JSON)
参看https://jsfiddle.net/YSharpLanguage/hvo24hmk/3
哪里...
http://jsoniq.org/docs/JSONiq-usecases/html-single/index.html#jsongrouping
......给出:
/*
1.1.2. Grouping Queries for JSON
http://jsoniq.org/docs/JSONiq-usecases/html-single/index.html#jsongrouping
*/
var sales = [
{ "product" : "broiler", "store number" : 1, "quantity" : 20 },
{ "product" : "toaster", "store number" : 2, "quantity" : 100 },
{ "product" : "toaster", "store number" : 2, "quantity" : 50 },
{ "product" : "toaster", "store number" : 3, "quantity" : 50 },
{ "product" : "blender", "store number" : 3, "quantity" : 100 },
{ "product" : "blender", "store number" : 3, "quantity" : 150 },
{ "product" : "socks", "store number" : 1, "quantity" : 500 },
{ "product" : "socks", "store number" : 2, "quantity" : 10 },
{ "product" : "shirt", "store number" : 3, "quantity" : 10 }
];
var products = [
{ "name" : "broiler", "category" : "kitchen", "price" : 100, "cost" : 70 },
{ "name" : "toaster", "category" : "kitchen", "price" : 30, "cost" : 10 },
{ "name" : "blender", "category" : "kitchen", "price" : 50, "cost" : 25 },
{ "name" : "socks", "category" : "clothes", "price" : 5, "cost" : 2 },
{ "name" : "shirt", "category" : "clothes", "price" : 10, "cost" : 3 }
];
var stores = [
{ "store number" : 1, "state" : "CA" },
{ "store number" : 2, "state" : "CA" },
{ "store number" : 3, "state" : "MA" },
{ "store number" : 4, "state" : "MA" }
];
var nestedGroupingAndAggregate = stores.select().orderBy("state").groupBy("state")
( function(byState) {
var state = byState[0],
stateStores = byState[1];
byState = { };
return (
(
byState[state] =
products.select().orderBy("category").groupBy("category")
( function(byCategory) {
var category = byCategory[0],
categoryProducts = byCategory[1],
categorySales = sales.filter(function(sale) {
return stateStores.find(function(store) { return sale["store number"] === store["store number"]; }) &&
categoryProducts.find(function(product) { return sale.product === product.name; });
});
byCategory = { };
return (
(
byCategory[category] =
categorySales.select().orderBy("product").groupBy("product")
( function(byProduct) {
var soldProduct = byProduct[0],
soldQuantities = byProduct[1];
byProduct = { };
return (
(
byProduct[soldProduct] =
soldQuantities.reduce(function(sum, sale) { return sum += sale.quantity; }, 0)
),
byProduct
);
} ) // byProduct()
),
byCategory
);
} ) // byCategory()
),
byState
);
} ); // byState()
克服JSONPath wrt的局限性也很有用。查询祖先轴,由[
{
"CA": [
{
"clothes": [
{
"socks": 510
}
]
},
{
"kitchen": [
{
"broiler": 20
},
{
"toaster": 150
}
]
}
]
},
{
"MA": [
{
"clothes": [
{
"shirt": 10
}
]
},
{
"kitchen": [
{
"blender": 250
},
{
"toaster": 50
}
]
}
]
}
]
(当然还有其他人)提出。
例如,如何获得知道其品牌ID的杂货商品的折扣
this SO question
?
可能的解决方案是:
{
"prods": [
{
"info": {
"rate": 85
},
"grocery": [
{
"brand": "C",
"brand_id": "984"
},
{
"brand": "D",
"brand_id": "254"
}
],
"discount": "15"
},
{
"info": {
"rate": 100
},
"grocery": [
{
"brand": "A",
"brand_id": "983"
},
{
"brand": "B",
"brand_id": "253"
}
],
"discount": "20"
}
]
}
... 这使:
var products = {
"prods": [
{
"info": {
"rate": 85
},
"grocery": [
{
"brand": "C",
"brand_id": "984"
},
{
"brand": "D",
"brand_id": "254"
}
],
"discount": "15"
},
{
"info": {
"rate": 100
},
"grocery": [
{
"brand": "A",
"brand_id": "983"
},
{
"brand": "B",
"brand_id": "253"
}
],
"discount": "20"
}
]
};
function GroceryItem(obj) {
return (typeof obj.brand === "string") && (typeof obj.brand_id === "string");
}
// last parameter set to "true", to grab all the "GroceryItem" instances
// at any depth:
var itemsAndDiscounts = [ products ].nodeset(GroceryItem, true).
map(
function(node) {
var item = node.value, // node.value: the current "GroceryItem" (aka "$.prods[*].grocery[*]")
discount = node.parent. // node.parent: the array of "GroceryItem" (aka "$.prods[*].grocery")
parent. // node.parent.parent: the product (aka "$.prods[*]")
discount; // node.parent.parent.discount: the product discount
// finally, project into an easy-to-filter form:
return { id: item.brand_id, discount: discount };
}
),
discountOfItem983;
discountOfItem983 = itemsAndDiscounts.
filter
(
function(mapped) {
return mapped.id === "983";
}
)
[0].discount;
console.log("Discount of #983: " + discountOfItem983);
“HTH,