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

时间:2022-10-31 18:45:56

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页面读取它

4 个解决方案

#1


4  

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>

#2


4  

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 include command.

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

foo.shtml

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

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 Seth in 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 wenuxas has 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 Ajax may be what you are looking for.

如果您的Xml文档仅代表最终页面的一个片段,那么Ajax可能正是您所寻找的。

#3


1  

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内容,可以搜索并替换所有括号( <变为<和> 变为>),然后将结果粘贴到

和
 
 标记之间。

#4


0  

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内容,我对它并不熟悉。

#1


4  

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>

#2


4  

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 include command.

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

foo.shtml

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

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 Seth in 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 wenuxas has 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 Ajax may be what you are looking for.

如果您的Xml文档仅代表最终页面的一个片段,那么Ajax可能正是您所寻找的。

#3


1  

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内容,可以搜索并替换所有括号( <变为<和> 变为>),然后将结果粘贴到

和
 
 标记之间。

#4


0  

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内容,我对它并不熟悉。