2017-03-04 45 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非常基本的級別」。人們在涉足技術方面涉獵很大,他們沒有努力去理解,如果水管工或電工這樣做了,我們現在都會死的 –

相關問題