Translating this XML to JSON using XSLT -
i attempting translate trivial xml json using xslt.
my xml looks following:
<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>
the output should following:
{ "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" } } ], }
my xslt attempt translation work:
<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>
and following broken output results:
{ "my_c": [ { "c": { "foo_id": "bar1" ], } } { "c": { "foo_id": "bar2" ], } }
where did go wrong in xslt?
try closing first xsl:value-of
.
this: <xsl:value-of select="@foo">
should be: <xsl:value-of select="@foo"/>
if change it, output (which close desired output, still have little bit of work left):
{ "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 } }, ], }
also, shouldn't need current()
.
Comments
Post a Comment