有没有用 XPath 扩展 XML 文档模板的工具?
Is there a tool for expanding XML document templates with XPath?
我喜欢写作 HTML 并且倾向于编写很少 JavaScript 的扁平化网站。我一直在考虑编写一个新的 "language" 类型的想法,它使用 XPath 和 XMLNS 来引用其他文档。
我一直在考虑如何编写我的 HTML,然后我会 运行 一个构建工具来扩展和构建完整的文档。
有没有这样的工具?我宁愿重新使用或从已经存在的工具中学习。我怀疑我错过了一个明显的或常用的工具,它已经做了类似的事情。
例子
这通常是我想要编写 HTML(或 HTMPL
,因为它是模板,所以我一直这样称呼它)。
index.htmpl
<html xmlns:custom="card.htmpl">
<custom:card img="/path/to/image.jpg">
<title>Title content here</title>
This is the card
</custom:card>
</html>
card.htmpl
<div class="card">
<img src="[[//card@img]]" />
<div class="title">The title is: [[//card/title/text()]]</div>
<div class="content">[[//card/text()]]</div>
</div>
结果
删除了多余的空格
<html>
<div>
<img src="/path/to/image.jpg" />
<div class="title">The title is: Title content here</div>
<div class="content">This is the card</div>
</div>
</html>
XSLT 就是为完成这项工作而设计的。
XSLT 有一个名为 "simplified stylesheets" 的概念,专为您想要的是非常简单的模板但很少使用简化样式表的情况而设计,因为大多数人发现他们最终需要完整的语言。
您的示例可以像这样用 XSLT 3.0(使用简化的样式表)编写:
data.xml
<custom:card img="/path/to/image.jpg">
<title>Title content here</title>
This is the card
</custom:card>
template.xsl
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsl:version="3.0" expand-text="yes">
<div class="card">
<img src="{//card/@img}" />
<div class="title">The title is: {//card/title/text()}</div>
<div class="content">{//card/text()}</div>
</div>
</html>
但正如我所说,这种使用 XSLT 的方式并没有证明很流行。
还有其他(非标准化的)模板语言可用,例如 Velocity 和 Freemarker。在 https://en.wikipedia.org/wiki/Comparison_of_web_template_engines 中进行选择,或者如果您觉得需要在列表中添加另一个,请自行设计。
我喜欢写作 HTML 并且倾向于编写很少 JavaScript 的扁平化网站。我一直在考虑编写一个新的 "language" 类型的想法,它使用 XPath 和 XMLNS 来引用其他文档。
我一直在考虑如何编写我的 HTML,然后我会 运行 一个构建工具来扩展和构建完整的文档。
有没有这样的工具?我宁愿重新使用或从已经存在的工具中学习。我怀疑我错过了一个明显的或常用的工具,它已经做了类似的事情。
例子
这通常是我想要编写 HTML(或 HTMPL
,因为它是模板,所以我一直这样称呼它)。
index.htmpl
<html xmlns:custom="card.htmpl">
<custom:card img="/path/to/image.jpg">
<title>Title content here</title>
This is the card
</custom:card>
</html>
card.htmpl
<div class="card">
<img src="[[//card@img]]" />
<div class="title">The title is: [[//card/title/text()]]</div>
<div class="content">[[//card/text()]]</div>
</div>
结果
删除了多余的空格
<html>
<div>
<img src="/path/to/image.jpg" />
<div class="title">The title is: Title content here</div>
<div class="content">This is the card</div>
</div>
</html>
XSLT 就是为完成这项工作而设计的。
XSLT 有一个名为 "simplified stylesheets" 的概念,专为您想要的是非常简单的模板但很少使用简化样式表的情况而设计,因为大多数人发现他们最终需要完整的语言。
您的示例可以像这样用 XSLT 3.0(使用简化的样式表)编写:
data.xml
<custom:card img="/path/to/image.jpg">
<title>Title content here</title>
This is the card
</custom:card>
template.xsl
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsl:version="3.0" expand-text="yes">
<div class="card">
<img src="{//card/@img}" />
<div class="title">The title is: {//card/title/text()}</div>
<div class="content">{//card/text()}</div>
</div>
</html>
但正如我所说,这种使用 XSLT 的方式并没有证明很流行。
还有其他(非标准化的)模板语言可用,例如 Velocity 和 Freemarker。在 https://en.wikipedia.org/wiki/Comparison_of_web_template_engines 中进行选择,或者如果您觉得需要在列表中添加另一个,请自行设计。