Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format numbers to JSON format. Fixes issue #15. #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions xml2json.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
-->

<xsl:output indent="no" omit-xml-declaration="yes" method="text" encoding="UTF-8" media-type="text/x-json"/>
<xsl:strip-space elements="*"/>
<xsl:strip-space elements="*"/>
<!--contant-->
<xsl:variable name="d">0123456789</xsl:variable>

Expand Down Expand Up @@ -121,9 +121,23 @@
</xsl:template>

<!-- number (no support for javascript mantissa) -->
<xsl:template match="text()[not(string(number())='NaN' or
(starts-with(.,'0' ) and . != '0'))]">
<xsl:value-of select="."/>
<xsl:template match="text()[not(string(number())='NaN' or
(starts-with(.,'0' ) and . != '0' and not(starts-with(.,'0.' ))) or
(starts-with(.,'-0' ) and . != '-0' and not(starts-with(.,'-0.' ))))]">
<xsl:choose>
<xsl:when test="starts-with(.,'.')">
<xsl:value-of select="concat('0',.)"/>
</xsl:when>
<xsl:when test="starts-with(.,'-.')">
<xsl:value-of select="concat('-0.', substring(.,3))"/>
</xsl:when>
<xsl:when test="substring(., string-length(.))='.'">
<xsl:value-of select="concat(.,0)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- boolean, case-insensitive -->
Expand All @@ -143,7 +157,7 @@
<xsl:when test="count(child::node())=0">null</xsl:when>
<!-- other nodes -->
<xsl:otherwise>
<xsl:apply-templates select="child::node()"/>
<xsl:apply-templates select="child::node()"/>
</xsl:otherwise>
</xsl:choose>
<!-- end of type check -->
Expand Down