Search

See http://stackoverflow.com/questions/24065275/xsl-trying-to-get-json-to-work

Trying to get the XML..

<user-types>
    <section id="11" handle="user-types">User types</section>
    <entry id="9">
        <name mode="unformatted" handle="home-owner" word-count="2" lang="en" handle-en="home-owner"><![CDATA[Home owner]]></name>
    </entry>
    <entry id="7">
        <name mode="unformatted" handle="contractor-residential" word-count="2" lang="en" handle-en="contractor-residential"><![CDATA[Contractor (residential)]]></name>
    </entry>
    <entry id="8">
        <name mode="unformatted" handle="contractor-non-residential" word-count="2" lang="en" handle-en="contractor-non-residential"><![CDATA[Contractor (non-residential)]]></name>
    </entry>
    <entry id="10">
        <name mode="unformatted" handle="commercial-property-owner" word-count="3" lang="en" handle-en="commercial-property-owner"><![CDATA[Commercial property owner]]></name>
    </entry>
</user-types>

To be JSON, but the tag entry doent get turned into an array, so JS can only read one of them :(

current broken JSON output

{
"user-types" : {
    "section" : {
        "@attributes" : {
            "id" : 11,
            "handle" : "user-types"
        },
        "value" : "User types"
    },
    "entry" : {
        "@attributes" : {
            "id" : 9
        },
        "name" : {
            "@attributes" : {
                "mode" : "unformatted",
                "handle" : "home-owner",
                "word-count" : 2,
                "lang" : "en",
                "handle-en" : "home-owner"
            },
            "value" : "Home owner"
        }
    },
    "entry" : {
        "@attributes" : {
            "id" : 7
        },
        "name" : {
            "@attributes" : {
                "mode" : "unformatted",
                "handle" : "contractor-residential",
                "word-count" : 2,
                "lang" : "en",
                "handle-en" : "contractor-residential"
            },
            "value" : "Contractor (residential)"
        }
    },
    "entry" : {
        "@attributes" : {
            "id" : 8
        },
        "name" : {
            "@attributes" : {
                "mode" : "unformatted",
                "handle" : "contractor-non-residential",
                "word-count" : 2,
                "lang" : "en",
                "handle-en" : "contractor-non-residential"
            },
            "value" : "Contractor (non-residential)"
        }
    },
    "entry" : {
        "@attributes" : {
            "id" : 10
        },
        "name" : {
            "@attributes" : {
                "mode" : "unformatted",
                "handle" : "commercial-property-owner",
                "word-count" : 3,
                "lang" : "en",
                "handle-en" : "commercial-property-owner"
            },
            "value" : "Commercial property owner"
        }
    }
}
}

Using this XSL https://github.com/iwyg/xml-to-json

1_ Do you really need information as array?

2_ Do you really need all that information in JSON?

Step #1: Convert to object literals instead of array.

Step #2: Transform to JSON only the information you need.

Call the XML to JSON formatter with this XML:

<user-types>
    <xsl:for-each select="/data/user-types/entry">
        <xsl:element name="entry-{@id}">
            <id>
                <xsl:value-of select="@id"/>
            </id>
            <name>
                <xsl:value-of select="name"/>
            </name>
        </xsl:element>
    </xsl:for-each>
</user-types>

Then use a for each loop in your JS.

;)

@into, you know well enough by now to not double post your problem. Please don't do it again.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details