【发布时间】:2016-11-30 11:13:03
【问题描述】:
我的输入 XML 包含页眉、内容和页脚部分。使用 XSLT 从 XML 到 JSON 的转换效果很好。但我需要将输出作为页眉、内容和页脚三部分:
我的输入 XML 文件是:
<header>
<trackingSettings>
<urlcode>W3333</urlcode>
<apiurl>http://mlucenter.com/like/api</apiurl>
</trackingSettings>
</header>
<mlu3_body>
<columnsCount>2</columnsCount>
<lineBackground>linear-gradient(to right, rgba(94, 172, 192, 0) 0%, c4cccf 50%, rgba(94, 172, 192, 0) 100%)</lineBackground>
</mlu3_body>
<footer>
<buttons>
<button/>
</buttons>
<banner/>
</footer>
我的 XSLT 使用:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />
<xsl:template match="*">
<xsl:value-of select="name()"/> : <xsl:call-template name="Properties"/>
</xsl:template>
<xsl:template match="*" mode="ArrayElement">
<xsl:call-template name="Properties"/>
</xsl:template>
<xsl:template name="Properties">
<xsl:variable name="childName" select="name(*[1])"/>
<xsl:choose>
<xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
<xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
<xsl:otherwise>{
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="*"/>
}</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>
<xsl:template match="@*">"<xsl:value-of select="name()"/>" : '<xsl:value-of select="."/>',
</xsl:template>
</xsl:stylesheet>
我在氧气中使用 Saxon PE:
我希望在输出中将此 XML 转换为 3 个名为 header.json、content.json(mlu3_body) 和 footer.json 的 JSON 文件。
这是否可以通过使用 XSLT 或者我想单独保留所有输入文件。请提供一些想法。
【问题讨论】:
-
如果您有“使用 XSLT 从 XML 到 JSON 的转换效果很好”,那么请显示该代码。一般来说,由 oXygen 和 Saxon 9 支持的 XSLT 2.0 和 3.0 当然可以使用
xsl:result-documentw3.org/TR/xslt20/#creating-result-trees 编写多个结果文档。 -
使用 XSLT 编辑,请查看此@Martin