Skip to content

Commit e97b7fa

Browse files
committed
json fix
1 parent de91754 commit e97b7fa

File tree

2 files changed

+66
-32
lines changed

2 files changed

+66
-32
lines changed

src/class-xml-file.php

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -853,44 +853,77 @@ private function jsonToDomDocumentItem($el, $parent, $doc)
853853

854854
public static function saveJsonXsltStandard() {
855855
return <<<'EOC'
856-
<?xml version="1.0"?>
856+
<?xml version="1.0" encoding="UTF-8" ?>
857857
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
858-
<xsl:output method="text"/>
858+
<xsl:output method="text" encoding="utf-8"/>
859859
<xsl:variable name='q'>"</xsl:variable>
860-
861-
<xsl:template match="/">{
862-
<xsl:apply-templates select="*"/>}
860+
861+
<xsl:template match="/*[node()]">
862+
<xsl:text>{</xsl:text>
863+
<xsl:apply-templates select="." mode="detect" />
864+
<xsl:text>}</xsl:text>
863865
</xsl:template>
864-
865-
<!-- Object or Element Property-->
866-
<xsl:template match="*">
867-
<xsl:value-of select='concat($q, name(), $q, ": ")' /><xsl:call-template name="Properties"/>
866+
867+
<xsl:template match="*" mode="detect">
868+
<xsl:choose>
869+
<xsl:when test="name(preceding-sibling::*[1]) = name(current()) and name(following-sibling::*[1]) != name(current())">
870+
<xsl:apply-templates select="." mode="obj-content" />
871+
<xsl:text>]</xsl:text>
872+
<xsl:if test="count(following-sibling::*[name() != name(current())]) &gt; 0">, </xsl:if>
873+
</xsl:when>
874+
<xsl:when test="name(preceding-sibling::*[1]) = name(current())">
875+
<xsl:apply-templates select="." mode="obj-content" />
876+
<xsl:if test="name(following-sibling::*) = name(current())">, </xsl:if>
877+
</xsl:when>
878+
<xsl:when test="following-sibling::*[1][name() = name(current())]">
879+
<xsl:text>"</xsl:text><xsl:value-of select="name()"/><xsl:text>" : [</xsl:text>
880+
<xsl:apply-templates select="." mode="obj-content" /><xsl:text>, </xsl:text>
881+
</xsl:when>
882+
<xsl:when test="count(./child::*) > 0 or count(@*) > 0">
883+
<xsl:value-of select='concat($q, name(), $q, ": ")' /><xsl:apply-templates select="." mode="obj-content" />
884+
<xsl:if test="count(following-sibling::*) &gt; 0">, </xsl:if>
885+
</xsl:when>
886+
<xsl:when test="count(./child::*) = 0">
887+
<xsl:value-of select='concat($q, name(), $q, ": ", $q, ., $q)' />
888+
<xsl:if test="count(following-sibling::*) &gt; 0">, </xsl:if>
889+
</xsl:when>
890+
</xsl:choose>
868891
</xsl:template>
892+
893+
<xsl:template match="*" mode="obj-content">
894+
<xsl:text>{</xsl:text>
895+
<xsl:apply-templates select="@*" mode="attr" />
896+
<xsl:if test="count(@*) &gt; 0 and (count(child::*) &gt; 0 or text())">, </xsl:if>
897+
<xsl:apply-templates select="./*" mode="detect" />
898+
<xsl:if test="count(child::*) = 0 and text() and not(@*)">
899+
<xsl:value-of select='concat($q, name(), $q, ": ", $q, text(), $q)' />
900+
</xsl:if>
901+
<xsl:if test="count(child::*) = 0 and text() and @*">
902+
<xsl:value-of select='concat($q, "text", $q, ": ", $q, text(), $q)' />
903+
</xsl:if>
904+
<xsl:text>}</xsl:text>
905+
<xsl:if test="not(position() &gt;= last())">, </xsl:if>
869906
870-
<!-- Array Element -->
871-
<xsl:template match="*" mode="ArrayElement">
872-
<xsl:call-template name="Properties"/>
873907
</xsl:template>
874-
875-
<!-- Object Properties -->
876-
<xsl:template name="Properties">
877-
<xsl:variable name="childName" select="name(*[1])"/>
908+
909+
<xsl:template match="@*" mode="attr">
910+
<xsl:value-of select='concat($q, name(), $q, ": ", $q, ., $q)' />
911+
<xsl:if test="not(position() &gt;= last())">,</xsl:if>
912+
</xsl:template>
913+
914+
<xsl:template match="node/@TEXT | text()" name="removeBreaks">
915+
<xsl:param name="pText" select="normalize-space(.)"/>
878916
<xsl:choose>
879-
<xsl:when test="not(*|@*)"><xsl:value-of select='concat($q, ., $q)' /></xsl:when>
880-
<xsl:when test="count(*[name()=$childName]) > 1"><xsl:value-of select='concat("{ ", $q, $childName, $q, ": ")' /> [<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
881-
<xsl:otherwise>{
882-
<xsl:apply-templates select="@*"/>
883-
<xsl:apply-templates select="*"/>
884-
}</xsl:otherwise>
917+
<xsl:when test="not(contains($pText, '&#xA;'))"><xsl:copy-of select="$pText"/></xsl:when>
918+
<xsl:otherwise>
919+
<xsl:value-of select="concat(substring-before($pText, '&#xD;&#xA;'), ' ')"/>
920+
<xsl:call-template name="removeBreaks">
921+
<xsl:with-param name="pText" select="substring-after($pText, '&#xD;&#xA;')"/>
922+
</xsl:call-template>
923+
</xsl:otherwise>
885924
</xsl:choose>
886-
<xsl:if test="following-sibling::*">,</xsl:if>
887-
</xsl:template>
888-
889-
<!-- Attribute Property -->
890-
<xsl:template match="@*">
891-
<xsl:value-of select='concat($q, "@", name(), $q, ": ", $q, ., $q)' />
892-
<xsl:if test="following-sibling::*">,</xsl:if>
893925
</xsl:template>
926+
894927
</xsl:stylesheet>
895928
EOC;
896929
}
@@ -929,7 +962,7 @@ public static function saveJsonStylesheet($mode) {
929962
public function saveJson($mode = '') {
930963
$str = self::docXml(self::transformXMLXSL_static($this->saveXML(), self::saveJsonStylesheet($mode)));
931964
$str = str_replace('<?xml version="1.0"?>', '', $str);
932-
$str = str_replace("\n", '', $str);
965+
// $str = str_replace("\n", '', $str);
933966
$result = self::tidyJson_string($str);
934967
return $result !== false ? $result : $str; // in case tidy fails
935968
}

tests/json-save-test.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ public function testSaveXMLRecordsetToJson()
5151
{
5252
$subject = new xml_file(__DIR__ . "/resources/recordset.xml");
5353
$result = $subject->saveJson();
54+
// print xml_file::tidyJson_string($result);
5455

5556
$this->assertNotNull($result);
56-
$this->assertTrue(false !== strpos($result, '"@id": "option1"'));
57+
$this->assertTrue(false !== strpos($result, '"id": "option1"'));
5758
}
5859

5960
public function testSaveXMLRecordsetToJsonRecordset()
6061
{
6162
$subject = new xml_file(__DIR__ . "/resources/recordset.xml");
6263
$result = $subject->saveJson('recordset');
6364

64-
print($result);
65+
// print($result);
6566
$this->assertNotNull($result);
6667
}
6768
}

0 commit comments

Comments
 (0)