开发者

I want to link my XML file to my HTML page

开发者 https://www.devze.com 2023-02-03 21:38 出处:网络
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.

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 version="1.0"?>
<Family>
<Mom>Alison</Mom>
<age>44</age>
<son>Ian</son>
<age>8</age>
<son>Set开发者_开发问答h</son>
</Family>

I would like to read that from my html page


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

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

<?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:

<?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>


a) Simply linking your Xml file

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

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.

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.


b) Rendering your Xml file as Html

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


c) Embedding your Xml file into your Html page

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


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.


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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消