首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xml到json。“内部XML/XHTML”应该放在json字符串值字段中。

使用xml到json。“内部XML/XHTML”应该放在json字符串值字段中。
EN

Stack Overflow用户
提问于 2017-03-04 22:52:53
回答 2查看 325关注 0票数 1

我使用Saxon将XML转换为JSON:

代码语言:javascript
复制
<?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>

产生所需的输出:

代码语言:javascript
复制
{"name":"Some name","description":"A nice description"}

description字段包含任意复杂的xhtml。模板规则不适用于以下描述字段的

代码语言:javascript
复制
<string key="description">A <strong>nice</strong> description</string>

错误消息:

代码语言:javascript
复制
xml-to-json: unknown element <strong>

将CDATA部分中的描述封装起来确实有效:

代码语言:javascript
复制
<string key="description"><![CDATA[A <strong>nice</strong> description]]></string>

期望产出:

代码语言:javascript
复制
{"name":"Some name","description":"A <strong>nice<\/strong> description"}

问题/问题

description字段的内容是转换的结果。所以with和withou失败了。,这将不能工作,

代码语言:javascript
复制
<string key="description"><xsl:apply-template select="description" /></string>

这两件事都没有:

代码语言:javascript
复制
<string key="description"><![CDATA[<xsl:apply-template select="description" />]]></string>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-05 00:08:34

使用 function作为文本生成转义的XML标记。

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2017-03-05 00:10:14

我想出了下面的快速攻击。明天得再查一次,现在有点累了

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

代码语言:javascript
复制
<?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>

产生所需的输出:

代码语言:javascript
复制
{"description":"A <strong class=\"red\">nice<\/strong> description"}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42602321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档