2017-03-04 40 views
1

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

回答

1

使用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> 
+0

这是一个基于XSLT 3.0/3.0的XPath/3.1我的应用程序的解决方案。谢谢@Mads Hansen。只是想知道他们是否存在XSLT 2.0的类似解决方案。 XSLT 2.0不支持xml-to-json函数,但可以提出类似的序列化需求,与xml-json转换无关。 – Stefan

+1

是的,请看看Evan Lenz XML到字符串转换器样式表:http://lenzconsulting.com/xml-to-string/ –

0

我想出了以下快速入门。必须再次检查这个明天,现在有点累......牛逼


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


<?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"} 
+0

可怕的黑客攻击。你不会试图逃避特殊字符。这是因为人们试图像这样尝试自制序列化,所以我们在这个网站上看到了很多破碎的XML。 –

+0

我认为@Mads Hansen接受的答案显然是正确的。也许不是。那么,如何处理“可怕的黑客”迈克尔?删除它?我可能不需要深入研究XML/XSLT。所以永远不会学习它。但只是为了好奇。哪些特殊字符必须被转义?请原谅XML“非常基本的水平”。 – Stefan

+0

在文本节点中,必须将'<'和'&'转义为'<'和'&'。如果它出现在序列']]>'中,你还需要确保'''逃脱为'>',尽管大多数人在任何地方转义'>'以节省工作量。在属性节点中,属性定界符(''''或'''')必须被转义。我很抱歉,但我发现很难原谅“XML非常基本的级别”。人们在涉足技术方面涉猎很大,他们没有努力去理解,如果水管工或电工这样做了,我们现在都会死的 –

相关问题