我使用撒克逊家庭版,以XML转换成JSON:使用xml-to-json。 “内部XML/XHTML” 应该在JSON字符串值字段
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />
<xsl:template match="/">
<xsl:variable name="xmljson">
<map xmlns="http://www.w3.org/2005/xpath-functions">
<string key="name">Some name</string>
<string key="description">A nice description</string>
</map>
</xsl:variable>
<xsl:value-of select="xml-to-json($xmljson)" />
</xsl:template>
</xsl:stylesheet>
产生所需的输出:
{"name":"Some name","description":"A nice description"}
说明字段包含任意复杂的xhtml。
<string key="description">A <strong>nice</strong> description</string>
错误消息:模板规则不与下面的描述领域的工作
xml-to-json: unknown element <strong>
括在CDATA节的描述不工作:
<string key="description"><![CDATA[A <strong>nice</strong> description]]></string>
所需的输出:
{"name":"Some name","description":"A <strong>nice<\/strong> description"}
问题
说明字段的内容是变换的结果。所以,随着CDATA的失败。 这样可不行:
<string key="description"><xsl:apply-template select="description" /></string>
无论是这样的:
<string key="description"><![CDATA[<xsl:apply-template select="description" />]]></string>
这是一个基于XSLT 3.0/3.0的XPath/3.1我的应用程序的解决方案。谢谢@Mads Hansen。只是想知道他们是否存在XSLT 2.0的类似解决方案。 XSLT 2.0不支持xml-to-json函数,但可以提出类似的序列化需求,与xml-json转换无关。 – Stefan
是的,请看看Evan Lenz XML到字符串转换器样式表:http://lenzconsulting.com/xml-to-string/ –