Giter VIP home page Giter VIP logo

xml2json's People

Contributors

cikabo avatar ilyar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

xml2json's Issues

Handle scalar arrays

XSLT doesn't handle scalar arrays well, shown actual and expected outputs.

Input xml:

<test>
	<child1>1</child1>
	<twin>2</twin>
	<twin>3</twin>
	<child2>4</child2>
</test>

Output Json::

{
  "test": {
    "child1": "1",
    "twin": [
      {
        "twin": "2"
      },
      {
        "twin": "3"
      }
    ],
    "child2": "4"
  }
}

Expected Json::

{
  "test": {
    "child1": "1",
    "twin": [
      "2",
      "3"
    ],
    "child2": "4"
  }
}

Single element XML

A very simple single element XML document does not work, for example:
<test a="one" />
This XML does not get matched by the main match
match="/*[node()]"
However if I remove the node test from that so the main match is simply
match="/*"
then the XML gets transformed as expected to
{"test" : {"a" : "one"}}
I tried it with more complex XML and it seems to still work, can you please let me know why the node test is there and what the negative impact of removing it will be?

Thanks

Allow for data types

This project is great, but only generates quoted output when sometimes we also want doubles, integers, etc. I propose figuring out how to load an XSD for type checking during the transform. The below is really simple, but worked for my needs ๐Ÿ‘

Modify xml2json as follows:

<!-- Load the XSD so we can use it to look up data types during conversion --> <xsl:variable name="xsd" select="document('YOUR_SCHEMA.xsd')"/>

<xsl:template match="*" mode="detect"> ... <xsl:when test="count(./child::*) = 0"> <!-- This is a niave type check, but works for my simple case --> <xsl:variable name="pName" select="name(..)" /> <xsl:variable name="eltName" select="local-name()" /> <xsl:variable name="optionType" select="$xsd//xs:element[@name = $pName]//xs:element[@name = $eltName]/@type" /> <!-- Use optionType to determine to quote or not --> <xsl:choose> <xsl:when test="$optionType = 'xs:double' or $optionType = 'xs:int'"> <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : <xsl:apply-templates select="."/> </xsl:when> <xsl:otherwise> <xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:apply-templates select="."/><xsl:text>"</xsl:text> </xsl:otherwise> </xsl:choose> <xsl:if test="count(following-sibling::*) &gt; 0">, </xsl:if> </xsl:when> </xsl:choose> </xsl:template> ...

This is a basic idea, but in this way you can output typed data too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.