XSL试图让JSON工作

时间:2014-06-05 16:18:29

标签: xml json xslt

尝试获取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>

要成为JSON,但标签entry会变成一个数组,所以JS只能读取其中一个:(

当前损坏的JSON输出

{
"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"
        }
    }
}
}

使用此XSL https://github.com/iwyg/xml-to-json

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:-1)

根据xml-to-json的文档,这似乎并不打算以这种方式使用。

使用此文档会产生一个数组:

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

结果看起来像你想要的那样吗?你能用这种方式修改XML吗?否则我担心你必须对xml-to-json进行更改才能处理你的xml结构。