2
XSLT를 사용하여 사소한 XML을 JSON으로 변환하려고합니다.XSLT를 사용하여이 XML을 JSON으로 변환
내 XML처럼 보이는 다음
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" />
<xsl:template match="/some_xml">
{
"my_c": [
<xsl:for-each select="a/b/c">
{
"c": {
"foo_id": <xsl:value-of select="@foo">,
"listing_1": <xsl:value-of select="current()/listing[@n='1']" />,
"listing_2": <xsl:value-of select="current()/listing[@n='2']" />,
"listing_3": <xsl:value-of select="current()/listing[@n='3']" />,
"listing_4": <xsl:value-of select="current()/listing[@n='4']" />
}
},
</xsl:for-each>
],
}
</xsl:template>
</xsl:stylesheet>
: 작업이 번역을 얻기 위해 시도하는
{
"my_c": [
{
"c": {
"foo_id": "bar1",
"listing_1": "a",
"listing_2": "b",
"listing_3": "c",
"listing_4": "d"
}
},
{
"c": {
"foo_id": "bar2",
"listing_1": "e",
"listing_2": "b",
"listing_3": "n",
"listing_4": "d"
}
}
],
}
내 XSLT :
는<some_xml>
<a>
<b>
<c foo="bar1">
<listing n="1">a</listing>
<listing n="2">b</listing>
<listing n="3">c</listing>
<listing n="4">d</listing>
</c>
<c foo="bar2">
<listing n="1">e</listing>
<listing n="2">b</listing>
<listing n="3">n</listing>
<listing n="4">d</listing>
</c>
</b>
</a>
</some_xml>
출력은 다음과 같이 보일 것이다
다음과 같은 결과가 나타납니다.
{
"my_c": [
{
"c": {
"foo_id": "bar1"
],
}
}
{
"c": {
"foo_id": "bar2"
],
}
}
어디에서 XSLT가 잘못 되었습니까?
놀라운. 이 정도의 피해를 입을 수 있다는 것을 몰랐습니다. 이 경우 파서 오류가 발생하기를 바랬습니다. 잘 했어, 대니얼. 고맙습니다. – randombits
끝에 여분의 쉼표를 제거 할 수있는 방법이 있습니까? 즉, 쉼표 뒤 22 행 다음]에서 게시 한 출력의 24 행에 있습니다. 이러한 추가 쉼표 때문에 JSON 평가 오류가 발생합니다. – CCoder
질문에 대한 답변이 여기에 있습니다. http://stackoverflow.com/questions/20658307/problems-converting-xml-to-json-using-xslt –