bojanbjelic / xml2json Goto Github PK
View Code? Open in Web Editor NEWXml To Json
License: MIT License
Xml To Json
License: MIT License
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"
}
}
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
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::*) > 0">, </xsl:if> </xsl:when> </xsl:choose> </xsl:template> ...
This is a basic idea, but in this way you can output typed data too.
No escaping of required characters by JSON according to json.org
escape
'"'
''
'/'
'b'
'n'
'r'
't'
'u' hex hex hex hex
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.