使用 xml-to-json。 "Inner XML/XHTML" 应该进入 json 字符串值字段

Using xml-to-json. "Inner XML/XHTML" should go in a json string value field

我正在使用撒克逊家庭版将 XML 转换为 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"}

Problem/Question

描述字段的内容是转换的结果。所以有和没有 CDATA 都会失败。 这行不通:

<string key="description"><xsl:apply-template select="description" /></string>

这都不是:

<string key="description"><![CDATA[<xsl:apply-template select="description" />]]></string>

使用 serialize() function 将转义的 XML 标记生成为文本。

<xsl:variable name="options" as="element()">
  <output:serialization-parameters xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization">
    <output:omit-xml-declaration value="yes"/>
  </output:serialization-parameters>
</xsl:variable>

<xsl:variable name="xmljson" as="element()">
  <map xmlns="http://www.w3.org/2005/xpath-functions">
    <string key="name">Some name</string>
    <string key="description"><xsl:value-of select="serialize(description, $options)"/></string>
  </map>
</xsl:variable>

我想到了以下快速技巧。明天再看,有点累了...t


编辑:这是一个可怕的 hack! 模板规则不处理特殊字符。请阅读下面的评论。


<?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="doc">
            <description>A <strong class="red">nice</strong> description</description>
        </xsl:variable>
        <xsl:variable name="xmljson">
            <map xmlns="http://www.w3.org/2005/xpath-functions">
                <string key="description"><xsl:apply-templates select="$doc/description/text()|$doc/description/*" /></string>
            </map>
        </xsl:variable>
        <xsl:value-of select="xml-to-json($xmljson)" />
    </xsl:template>

    <xsl:template match="*">
        <!-- opening tag -->
        <xsl:text>&lt;</xsl:text>
        <xsl:value-of select="name()"/>
        <!-- attribute nodes -->
        <xsl:apply-templates select="@*"/>
        <xsl:text>&gt;</xsl:text>
        <xsl:apply-templates/>
        <!-- closing tag -->
        <xsl:text>&lt;</xsl:text>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text>&gt;</xsl:text>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:text> </xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text>="</xsl:text>
        <xsl:value-of select="." />
        <xsl:text>"</xsl:text>
    </xsl:template>

</xsl:stylesheet>

生成所需的输出:

{"description":"A <strong class=\"red\">nice<\/strong> description"}