3

I am attempting to translate some trivial XML to JSON using XSLT.

My XML looks like the 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 look something like the 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 to attempt to get this translation to 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 the following broken output is what results:

{
"my_c": [

            {
              "c": {
                "foo_id": "bar1"
        ],
      }
    }

            {
              "c": {
                "foo_id": "bar2"
        ],
      }
}

Where did I go wrong in my XSLT?

randombits
  • 47,058
  • 76
  • 251
  • 433

1 Answers1

2

Try properly closing your first xsl:value-of.

This: <xsl:value-of select="@foo">

Should be: <xsl:value-of select="@foo"/>

If I change it, I get this output (which is close to your desired output, but you still have a 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, you shouldn't need current().

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • Amazing. Didn't know this much damage can be done. I was hoping there would be a parser error in this case. Nice catch, Daniel. Thank you. – randombits Aug 19 '13 at 22:12
  • Is there a way i can get rid of the extra commas in the end. i.e. comma after } in line 22 and after ] in line 24 of the output that you posted. I am getting JSON eval error because of these extra commas. – CCoder Dec 18 '13 at 12:24
  • Looks like your question was answered here: http://stackoverflow.com/questions/20658307/problems-converting-xml-to-json-using-xslt – Daniel Haley Dec 18 '13 at 17:32