【问题标题】:XSL trying to get JSON to workXSL 试图让 JSON 工作
【发布时间】:2014-06-05 16:18:29
【问题描述】:

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

【问题讨论】:

  • 您可以在json.parser.online.fr上测试结果
  • 我在这里没有看到任何计算机程序。您能否展示您正在尝试改进的将 XML 转换为 JSON 的代码?
  • 使用这个 XSL github.com/iwyg/xml-to-json
  • 我无法使用您指向的样式表重现您的结果(我看到的实际输出比您报告的要糟糕得多 - xsltransform.net/eiQZDbv)。我建议您寻找更好的来源来复制您的代码。

标签: xml json xslt


【解决方案1】:

【讨论】:

    【解决方案2】:

    根据 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 结构。

    【讨论】:

    • 这不起作用,因为这是我需要支持的众多示例之一。我无法完成这样的自动化任务。
    • 这并不是否决这个答案的真正理由。那么是否能够使用自定义 xslt 而不是您链接的 xslt?
    • 这似乎不是一个答案。
    • 我希望修改xml-to-json以满足这个XML变成JSON的需要
    猜你喜欢
    • 2019-02-01
    • 2011-04-13
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2011-10-02
    • 2015-10-21
    相关资源
    最近更新 更多