我想将我的 XML 文件链接到我的 HTML 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4682904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:08:36  来源:igfitidea点击:

I want to link my XML file to my HTML page

htmlxml

提问by user770022

I have created a XML file I would like for it to be displayed in my HTML page that I also created. Can someone tell me how to do this.

我创建了一个 XML 文件,我希望它显示在我也创建的 HTML 页面中。有人可以告诉我如何做到这一点。

<?xml version="1.0"?>
<Family>
<Mom>Alison</Mom>
<age>44</age>
<son>Ian</son>
<age>8</age>
<son>Seth</son>
</Family>

I would like to read that from my html page

我想从我的 html 页面阅读

回答by Robertas

You can use XSLT- language for transforming XML documents. Maybe this would fit your needs.

您可以使用XSLT- 用于转换 XML 文档的语言。也许这会满足您的需求。

I have modified your provided XML a little bit, because I think it is not structured well. So if we have such document:

我稍微修改了您提供的 XML,因为我认为它的结构不好。所以如果我们有这样的文件:

<?xml version="1.0"?>
<?xml-stylesheet href="bla.xsl" type="text/xsl" ?>
<family>
   <person>
      <role>Mom</role>
      <name>Alison</name>
      <age>44</age>
   </person>
   <person>
      <role>Father</role>
      <name>Ben</name>
      <age>45</age>
   </person>
   <person>
      <role>Son</role>
      <name>Ian</name>
      <age>8</age>
   </person>
</family>

The XSLT file would look something like this:

XSLT 文件看起来像这样:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>Family</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
    <th>Role</th>
        <th>Name</th>
        <th>Age</th>
      </tr>
      <xsl:for-each select="family/person">
          <tr>
        <td><xsl:value-of select="role"/></td>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="age"/></td>
          </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

回答by Filburt

a) Simply linking your Xml file

a) 只需链接您的 Xml 文件

You can link to your Xml file from a Html page by using Server Side Includes.

您可以使用服务器端包含从 Html 页面链接到您的 Xml 文件。

If your Webserver is configured to allow this feature (this is usually disabled for security reasons) all you need to do is to rename your Html page to .shtml and add the server side includecommand.

如果您的 Web 服务器配置为允许此功能(出于安全原因,这通常被禁用),您需要做的就是将您的 Html 页面重命名为 .shtml 并添加服务器端include命令。

foo.shtml

foo.shtml

<html>
    <head/>
    <body>
    <!--#include file="bar.xml" -->
    </body>
</html>

bar.xml

bar.xml

<?xml version="1.0"?>
<Family>
    <Mom>Alison</Mom>
    <age>44</age>
    <son>Ian</son>
    <age>8</age>
    <son>Seth</son>
</Family>

This will show the text Alison 44 Ian 8 Sethin your browser.

这将Alison 44 Ian 8 Seth在您的浏览器中显示文本。



b) Rendering your Xml file as Html

b) 将您的 Xml 文件呈现为 Html

If you want to render your complete Xml file as a Html page wenuxashas the correct answer for you.

如果您想将完整的 Xml 文件呈现为 Html 页面,wenuxas为您提供了正确的答案。



c) Embedding your Xml file into your Html page

c) 将您的 Xml 文件嵌入您的 Html 页面

If your Xml document represents only a fragment of your final page Ajaxmay be what you are looking for.

如果您的 Xml 文档仅代表最终页面的一个片段,那么Ajax可能就是您要查找的内容。

回答by Filburt

If you just want to display the XML contents as they look in the file, you can search and replace all brackets (< becomes &lt; and > becomes &gt;), then paste the result in between <pre> and </pre> tags.

如果您只想显示文件中的 XML 内容,您可以搜索并替换所有括号(< 变为 < 和 > 变为 >),然后将结果粘贴到 <pre> 和 </pre> 标记之间.

回答by Jonathan Wood

I would say the most common way is to use a server-side development platform such as ASP.NET to read the XML file and then format it into the page markup.

我会说最常见的方法是使用服务器端开发平台(例如 ASP.NET)读取 XML 文件,然后将其格式化为页面标记。

If there's a more direct way to include XML content in an HTML page, I'm not familiar with it.

如果有更直接的方式在 HTML 页面中包含 XML 内容,我不熟悉它。