2016-11-30 52 views
0

我希望訂單列表元素與輸出JSON文件中的輸入XML文件相同。我嘗試使用XSLT,但它不工作標記元素未進入JSON輸出

我輸入XML文件是:

<description> 
    <p>This medicine is classified as a GLP-1 receptor agonist.</p> 
    <ol> 
    <li>Use this medicine once a week</li> 
    </ol> 
</description> 

我使用的XSL是:

<xsl:template match="description"> 
    description: 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="p"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="ol"> 
    <ol><xsl:apply-templates/></ol> 
</xsl:template> 

<xsl:template match="li"> 
    <li><xsl:apply-templates/></li> 
</xsl:template> 

輸出XML,我得到的是:

description: 'This medicine is classified as a b : "GLP-1 receptor agonist."' 
Use this medicine once a week 

我期待輸出JSON爲:

description: 'This medicine is classified as a b : "GLP-1 receptor agonist."' 
    <ol><li>Use this medicine once a week</li></ol> 

這是可能的XML到JSON轉換?
請檢查並提供給我正確的XSLT代碼。提前致謝。

+0

您輸入XML和輸出不匹配。文本如何改變? – ScanQR

回答

0

我完成你的XSLT來

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" /> 
<xsl:strip-space elements="*" /> 

    <xsl:template match="description"> 
    description: 
    <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="p"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="ol"> 
    <ol><xsl:apply-templates/></ol> 
    </xsl:template> 

    <xsl:template match="li"> 
    <li><xsl:apply-templates/></li> 
    </xsl:template> 

</xsl:stylesheet> 

訣竅 - 無論你輸出之間的不同之處在於在開始<xsl:output ...>元素的method屬性。

如果使用

<xsl:output omit-xml-declaration="yes" indent="yes" method="text" /> 

你會得到

description: 
This medicine is classified as a GLP-1 receptor agonist.Use this medicine once a [email protected]:~/Do 

,如果你使用

<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" /> 

你會得到

description: 
    This medicine is classified as a GLP-1 receptor agonist.<ol> 
    <li>Use this medicine once a week</li> 
</ol> 
根據需要

+0

謝謝@ ZX485。它的工作 – User501

0

你只需要match要素或將被處理節點和您的要求,你只需要做好copy-of節點ol如下,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" /> 
<xsl:strip-space elements="*" /> 

    <xsl:template match="description"> 
    description: 
    <xsl:apply-templates/> 
    </xsl:template> 


    <xsl:template match="ol"> 
    <xsl:copy-of select="." /> 
    </xsl:template> 

</xsl:stylesheet>