2016-12-15 10 views
2

入力XMLファイルをJSON出力に変換している間に、一重引用符の属性は二重引用符に変換されます。XSLTを使用してJSON出力の一重引用符変換(XMLからJSON)を作成する

上記の問題を解決するには誰もガイドしてください。

私の入力XMLファイルには、次のとおりです。私が使用している

<items> 
<mlu1_item> 
<title> 
<strong>Creatinine</strong> 
</title> 
<content> 
<p>Creatinine is a normal waste product</p> 
<ul> 
<li>males</li> 
<li>females</li> 
</ul> 
<p>If your creatinine level kidneys.</p> 
</content> 
<mlu1_button/> 
<button_class/> 
<link/> 
<image>icon.png</image> 
</mlu1_item> 
</items> 

XSL:

items: 
[ 
{ 
"title": "Creatinine" 
"content": "<h4>Creatinine</h4> 
**<div class="text-left">** 
<p>Creatinine is a normal waste product</p> 
<ul> 
<li>males</li> 
<li>females</li> 
</ul> 
<p>If your creatinine level kidneys.</p> 
</div>", 
"link": "", 
"image": "img/icon.png", 
"top": "5%", 
"left": "52%", 
"size": "", 
"color": "", 
"borderColor": "#00", 
"bgInfoColor": "#fff", 
"borderWidth": "0px", 
}, 
] 
}; 

しかし、私のように出力を期待:私はようだ

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:json="http://json.org/" exclude-result-prefixes="#all"> 

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

<xsl:template match="my-node"> 
<xsl:value-of select="json:generate(.)"/> 
</xsl:template> 

<xsl:template match="items"> 
items: 
[ 
<xsl:for-each select="mlu1_item"> 
{ 
"title": "<xsl:value-of select="title/strong"/>" 
"content": "<h4><xsl:value-of select="title/strong"/></h4>**<div class='text-left'>**<xsl:apply-templates select="content"/></div>", 
"link": "", 
"image": "img/<xsl:value-of select="image"/>", 
"top": "5%", 
"left": "52%", 
"size": "", 
"color": "", 
"borderColor": "#00", 
"bgInfoColor": "#fff", 
"borderWidth": "0px", 
}, 
</xsl:for-each> 
] 
</xsl:template> 

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

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

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

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

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

</xsl:stylesheet> 

出力JSON:

items: 
[ 
{ 
"title": "Creatinine" 
"content": "<h4>Creatinine</h4> 
**<div class='text-left'>** 
<p>Creatinine is a normal waste product</p> 
<ul> 
<li>males</li> 
<li>females</li> 
</ul> 
<p>If your creatinine level kidneys.</p> 
</div>", 
"link": "", 
"image": "img/icon.png", 
"top": "5%", 
"left": "52%", 
"size": "", 
"color": "", 
"borderColor": "#00", 
"bgInfoColor": "#fff", 
"borderWidth": "0px", 
}, 
] 
}; 

JSON出力のdiv属性宣言の一重引用符を残したい

+0

どのXSLTプロセッサを使用しますか?最近のSaxonやAltovaのリリースでは、XMLをシリアル化してJSONを作成するXPath関数がサポートされているため、適切なフォーマットやバランスの取れた引用符やエスケープされた引用符を作成するためのテンプレートよりも優れています。 –

+0

Saxon-PE 9.6.0.7バージョン変換に使用しました –

+0

oXygenの中でSaxonを使用していますか?出力については、json.orgで指定されているようなJSONになっていますか? –

答えて

2

本当にXML出力メソッドを使用してXML以外のものを作成することは望ましくありません。 XML出力メソッドでは、属性値を一重引用符で囲むか二重引用符で囲むかを制御することはできません。これは、XMLを記述していることを前提としているため、XMLでは違いがありません。

XMLのフラグメントをXML以外のフォーマットにしたい場合、これはXPath 3.0/3.1のfn:serialize()関数が使用するものです。

これを使用してXMLの部分を作成し、JSON構造に組み込んでJSON出力メソッドを使用してシリアル化することができます。 JSON出力メソッドは、文字列の内容の二重引用符を\"としてエスケープします。

関連する問題