qbektrix / xml2json-xslt

Automatically exported from code.google.com/p/xml2json-xslt
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

JSON invalid for nested XML with null elements #1

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Given the following XML snippet:

<productRating>
   <rating>
     <ratingCode>PG-13</ratingCode>
   </rating>
   <explaination/>
</productRating>

Execute the transform.  

What is the expected output? What do you see instead?
The resultant JSON looks like this:

{"productRatingId":{"rating":{"ratingCode":"NR-
18"},"ratingExplanation":null}

Which is incorrect.  

The correct JSON looks like:

{"productRatingId":{"rating":{"ratingCode":"NR-
18"},"ratingExplanation":null}}

I believe that the fix for this is as simple as adding the line:

<xsl:if test="not(following-sibling::*)">}</xsl:if>

Under line 142 of the transform.  Seems to me that if the null template is 
adding the comma, it should also have the ability to end the object.  But 
i'm not sure what side effects this might have.

I should also say that the transform is correct with the above snippet has 
a value for "explaination".

Original issue reported on code.google.com by madhac...@gmail.com on 17 Apr 2007 at 7:46

Attachments:

GoogleCodeExporter commented 9 years ago
Although it should work in main cases, I don't like madHacker solution. So here 
is my
solution :
I delete the "item:null" template and I add a condition in the "object" 
template to
match null child nodes :

--code--

  <!-- item:null : DELETED
  <xsl:template match="*[count(child::node())=0]">
    <xsl:call-template name="escape-string">
      <xsl:with-param name="s" select="local-name()"/>
    </xsl:call-template>
    <xsl:text>:null</xsl:text>
    <xsl:if test="following-sibling::*">,</xsl:if>
  </xsl:template>
  -->

  <!-- object -->
  <xsl:template match="*" name="base">
    <xsl:if test="not(preceding-sibling::*)">{</xsl:if>
    <xsl:call-template name="escape-string">
      <xsl:with-param name="s" select="name()"/>
    </xsl:call-template>
    <xsl:text>:</xsl:text>
    <!-- check type of node -->
    <xsl:choose>
      <!-- null nodes -->
      <xsl:when test="count(child::node())=0">null</xsl:when>
      <!-- other nodes -->
      <xsl:otherwise>
        <xsl:apply-templates select="child::node()"/>
      </xsl:otherwise>
    </xsl:choose>
    <!-- end of type check -->
    <xsl:if test="following-sibling::*">,</xsl:if>
    <xsl:if test="not(following-sibling::*)">}</xsl:if>
  </xsl:template>

--end of code--

I performed some tests and it seems to be ok.

Original comment by mat.per...@gmail.com on 21 May 2007 at 2:08

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks, this appears to fix a problem I was experiencing where invalid 
javascript was
being generated by some tags.
<slideshow>
  <item id="0" type="WebLink">
    <title><![CDATA[Pay The Devil]]></title>
    <other><![CDATA[New Release]]></other>
    <asset-subtypes>
      <asset-subtype/>
    </asset-subtypes>
...
was generating code that looked like:
{"slideshow":[{"title":"Pay The Devil","other":"New
Release","asset-subtypes":"asset-subtype":null,

the part:
"asset-subtypes":"asset-subtype":null
obviously was invalid.

The new xslt fixes it  :-)
WooHoo!
thanks

Original comment by pauleda...@gmail.com on 5 Dec 2007 at 6:53

GoogleCodeExporter commented 9 years ago
just add the last line

    <xsl:if test="not(following-sibling::*)">}</xsl:if>

that was missing in the item:null template

  <!-- item:null -->
  <xsl:template match="*[count(child::node())=0]">
    <xsl:call-template name="escape-string">
      <xsl:with-param name="s" select="local-name()"/>
    </xsl:call-template>
    <xsl:text>:null</xsl:text>
    <xsl:if test="following-sibling::*">,</xsl:if>
    <xsl:if test="not(following-sibling::*)">}</xsl:if>
  </xsl:template>

Original comment by deviants...@gmail.com on 6 Mar 2008 at 10:55

GoogleCodeExporter commented 9 years ago
Fixed in r22.

Original comment by docw...@gmail.com on 31 Mar 2008 at 4:43