开发者

How to convert XML to something readable?

开发者 https://www.devze.com 2023-03-17 06:41 出处:网络
I have a huge xml file and i want to convert it to a readable format. This is how my xml file looks like:

I have a huge xml file and i want to convert it to a readable format.

This is how my xml file looks like:

<entries>
<entry title="earth" id="9424127" date="2006-04-19T08:22:16.140开发者_StackOverflow中文版">
<![CDATA[earth is the place where we live.]]>
</entry>
</entries>

So i have more than 5000 entries like this and i want to put them online so i can read them easily. How can i convert it?

This is the output that i want to have:

Earth

earth is the place where we live. (2006-04-19T08:22:16.140)


You could use an XSLT stylesheet to create a simple html table.

For example, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/entries">
    <html>
      <body>
        <table border="1">
          <xsl:apply-templates/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="entry">
    <tr>
      <td><xsl:value-of select="@title"/></td>
      <td>
        <xsl:apply-templates/>
      </td>
      <td>(<xsl:value-of select="@date"/>)</td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

would create:

<html>
  <body>
    <table border="1">
      <tr>
        <td>earth</td>
        <td> earth is the place where we live. </td>
        <td>(2006-04-19T08:22:16.140)</td>
      </tr>
    </table>
  </body>
</html>


I have used CSS for this kind of job a couple of times. This is a good guide: http://www.w3schools.com/xml/xml_display.asp


You can very well use XSLT, so called XML Stylesheets for this.

Read about them and take a tour here: http://www.w3schools.com/xsl/

In your specific case, a rather simple solution could look something like:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
      <xsl:for-each select="entry">
          <br />
          <!-- Process CDATA somehow --> (<xsl:value-of select="@date"/>)
      </xsl:for-each>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
0

精彩评论

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