3

レストランのメニューを表すXMLファイルがあり、そのコンテンツをHTMLファイルのdiv要素にレンダリングしたいと思います。HTML5とCSS3のみを使用する必要があります。これどうやってするの?

4

2 に答える 2

4

これを試して:

<embed src="file.xml" />
于 2013-01-02T19:50:31.450 に答える
3

Expanding on Praveen's answer (mostly for the sake of providing a more complete code example), it is in fact possible to display XML content and style it with CSS using the following incantations:

foo.html

<head>
    <title>test</title>
</head>

<body>
    <object data="foo.xml" />
</body>

</html>

foo.xml

<?xml-stylesheet type="text/css" href="foo.css"?>
<foo>
    abc
    <bar>def</bar>
    ghi
</foo>

foo.css

bar {
    color: red;
}

You might have to live with this not working across all browsers, because there's no standard way I'm aware of transcluding a XML document from a HTML one without using a programming language. (Although the above works in FF, Chrome, and IE.)

That is, unless copy-pasting the XML into the body of the HTML document counts. (Which is technically not including XML, strictly speaking, but a bunch of "invalid" HTML tags that just happen to look a lot like XML. This is possible because HTML explicitly allows unknown tags for the sake of forward compatibility, and as of HTML5 allows some XMLisms like self-closed tags for compatibility with XHTML.) I.e. the following is perfectly legal, and should accomplish the same as the above example, although it still makes me vaguely uncomfortable:

foo2.html

<html>

<head>
    <title>test</title>
    <style type="text/css">
    bar {
        color: red;
    }
    </style>
</head>

<body>
    <!-- Contents of foo.xml go here -->
    <foo>
        abc
        <bar>def</bar>
        ghi
    </foo>
</body>

</html>

That said, both of these are IMO less dumb than using XSLT, in that it's actually possible to figure out what's going on.

于 2013-01-02T20:08:43.060 に答える