我使用撒克遜家庭版,以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/ –