我使用 XSLT 来转换它:
1)
<fruit>
<apple count="2"/>
<banana count="3"/>
</fruit>
进入这个:
2)
Apple: 2
Banana: 3
是否有一个库作为文本数据的解析器,允许对预期标记进行描述性声明(考虑扩展巴科斯-诺尔形式 |EBNF| 和 Lex/Yacc 基于正则表达式的提示)并从中构建 XML DOM ?
最佳答案
Is there a library that serves as a parser for text data which allows descriptive declaration of the expected tokens (think both Extended Backus–Naur Form |EBNF| and Lex/Yacc regex-based hints) and builds an XML DOM from it?
是的。 FXSL 2.0有一个函数 f:lr-parse()
正是这样做的。这是用纯 XSLT 2.0 编写的,实现了一个通用的(表驱动的) LR-1 parser ,它接受一个包含解析表的 XML 文件和一个包含要解析的“句子”的文本文件作为输入。
我已经将这个函数用于许多解析器,从玩具算术表达式到中型 - JSON,再到非常大的 - XPath 2.0 - 语言。
例如,请参阅我博客中的这篇文章: Transforming JSON
关于java - "Reverse"XSLT(解析和构建 XML DOM 的语言),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11784219/