2016-11-30 2 views
0

Ich möchte, dass das Element der Bestellliste wie die XML-Eingabedatei in der JSON-Ausgabedatei erscheint. Ich habe versucht, mit Hilfe von XSLT, aber es funktioniert nichtDas Tagging-Element kommt nicht in JSON Ausgabe

Meine Eingabe-XML-Datei ist:

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

Die XSL die ich benutze ist:

<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> 

Output XML, die ich habe ist:

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

Ich erwarte Ausgabe JSON als:

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

Ist dies in XML zu JSON Konvertierung möglich?
Bitte überprüfen Sie und geben Sie mir den richtigen XSLT-Code. Danke im Voraus.

+0

Ihre Eingabe xml und Ausgang ist passend nicht. Wie hat sich der Text geändert? – ScanQR

Antwort

0

abgeschlossen ich Ihre 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> 

Der Trick - den Unterschied zwischen Ihnen beiden Ausgänge liegt im method Attribut des <xsl:output ...> Element am Anfang.

Wenn Sie

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

Sie

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

erhalten wird, und wenn Sie

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

verwenden, werden Sie

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

wie gewünscht.

+0

Danke @ ZX485. Es funktioniert – User501

0

Sie brauchen nur match Elemente oder Knoten, auf die Sie gerade für Ihre Anforderung verarbeitet werden und müssen den copy-of Knoten ol wie folgt zu tun,

<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>