我使用Saxon将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"}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"}问题/问题
description字段的内容是转换的结果。所以with和withou失败了。,这将不能工作,
<string key="description"><xsl:apply-template select="description" /></string>这两件事都没有:
<string key="description"><![CDATA[<xsl:apply-template select="description" />]]></string>发布于 2017-03-05 00:08:34
使用 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>发布于 2017-03-05 00:10:14
我想出了下面的快速攻击。明天得再查一次,现在有点累了
编辑:,这是一个可怕的黑客!模板规则不处理特殊字符。请阅读下面的评论。
<?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><</xsl:text>
<xsl:value-of select="name()"/>
<!-- attribute nodes -->
<xsl:apply-templates select="@*"/>
<xsl:text>></xsl:text>
<xsl:apply-templates/>
<!-- closing tag -->
<xsl:text><</xsl:text>
<xsl:text>/</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></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"}https://stackoverflow.com/questions/42602321
复制相似问题