以表格格式将XML转换为HTML [英] To transform XML to HTML in table format

查看:104
本文介绍了以表格格式将XML转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将此xml内容更改为HTML表格

 < SSI> 
< data>
< expanded> Chemical Research< / expanded><缩> Chem。 RES< / abbre>
< expanded> Materials Journal< / expanded>< abbre> Mater。 J.< / abbre>
< expanded>化学生物学< / expanded><缩> Chem。生物学< / abbre>
< expanded> Symposium Series< / expanded>< abbre> Symp。丝氨酸< / abbre>
< expanded> Biochimica Polonica< / expanded><缩> Biochim。 。POL< / abbre>
Chemica Scandinavica< / expanded><缩> Chem。 。SCAND< / abbre>
< \data>
< data>
< expanded>植物学< / expanded><缩> Bot。< /缩>
< expanded> Chemical Engineering< / expanded><缩> Chem。主机< / abbre>
< expanded> Chemistry< / expanded>< abbre> Chem。< / abbre>
< expanded>地球科学< / expanded><缩>地球科学< / abbre>
<扩展>微生物学< /扩展><缩>微生物学< /缩写>
< \data>
< \ SSI>

尝试了以下XSL

 <?xml version =1.0?> 
< xsl:stylesheet xmlns:xsl =http://www.w3.org/1999/XSL/Transformversion =1.0>
< xsl:template match =/>
< html>
< head>< title> Abbreviate< / title>< / head>
< body>
< table border =1>
< tr>
< th>展开< / th>
< th> Abbre< th>
< / tr>
< xsl:for-each select =SSI / data>
< tr>
< td>< xsl:value-of select =expanded/>< / td>
< td>< xsl:value-of select =abbre/>< / td>
< / tr>
< / xsl:for-each>
< / table>
< / body>< / html>
< / xsl:template>
< / xsl:stylesheet>

我只以HTML表格格式获得数据标记的第一项

  Expanded Abbre 
----------- ----------------- ---
化学研究化学。 Res
Botany Bot。

如何获得HTMl中的所有值???

xsl:apply-templates 而不是 xsl :for-each ,生活会变得更简单。几乎没有理由使用 xsl:for-each 。试试这个:

 <?xml version =1.0?> 
< xsl:stylesheet xmlns:xsl =http://www.w3.org/1999/XSL/Transformversion =1.0>
< xsl:template match =/>
< html>
< head>< title> Abbreviate< / title>< / head>
< body>
< table border =1>
< tr>
< th>展开< / th>
< th> Abbre< th>
< / tr>
< xsl:apply-templates select ='SSI / data / expanded'/>
< / table>
< / body>< / html>
< / xsl:template>

< xsl:template match =expanded>
< tr>
< td>< xsl:apply-templates />< / td>
< / tr>
< / xsl:template>

< xsl:template match =abbre>
< td>< xsl:apply-templates />< / td>
< / xsl:template>

< / xsl:stylesheet>

通过使用应用的小模板,可以简化样式表。此外,在这里没有真正的理由使用 xsl:value-of - 内置模板将做正确的事情。您将得到更简单易懂的模板。


I want to change this xml content to HTML table

    <SSI>
        <data>
            <expanded>Chemical Research</expanded><abbre>Chem. Res.</abbre>
            <expanded>Materials Journal</expanded><abbre>Mater. J.</abbre>
            <expanded>Chemical Biology</expanded><abbre>Chem. Biol.</abbre>
            <expanded>Symposium Series</expanded><abbre>Symp. Ser.</abbre>
            <expanded>Biochimica Polonica</expanded><abbre>Biochim. Pol.</abbre>
            <expanded>Chemica Scandinavica</expanded><abbre>Chem. Scand.</abbre>
        <\data>
        <data>
            <expanded>Botany</expanded><abbre>Bot.</abbre>
            <expanded>Chemical Engineering</expanded><abbre>Chem. Eng.</abbre>
            <expanded>Chemistry</expanded><abbre>Chem.</abbre>
            <expanded>Earth Sciences</expanded><abbre>Earth Sci.</abbre>
            <expanded>Microbiology</expanded><abbre>Microbiol.</abbre>
        <\data>
    <\SSI>

Tried with following XSL

      <?xml version="1.0"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="/">
      <html>
      <head><title>Abbreviate</title></head>
      <body>
      <table border="1">
      <tr>
      <th>Expanded</th>
      <th>Abbre</th>
      </tr>
       <xsl:for-each select="SSI/data">
       <tr>
        <td><xsl:value-of select="expanded"/></td>
        <td><xsl:value-of select="abbre"/></td>
       </tr>
       </xsl:for-each>
      </table>
      </body></html>
      </xsl:template>
      </xsl:stylesheet>

I got only the first entry of data tag in HTML Table format

    Expanded               Abbre  
    -----------           --------------------  
    Chemical Research     Chem. Res  
    Botany                Bot.

how can get all the values in HTMl???

解决方案

If you clean up your XSLT and use xsl:apply-templates rather than xsl:for-each, life will become simpler. There is almost never a reason to use xsl:for-each. Try this:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <head><title>Abbreviate</title></head>
            <body>
                <table border="1">
                    <tr>
                        <th>Expanded</th>
                        <th>Abbre</th>
                    </tr>
                    <xsl:apply-templates select='SSI/data/expanded'/>
                </table>
            </body></html>
    </xsl:template>

    <xsl:template match="expanded">
        <tr>
            <td><xsl:apply-templates/></td>
            <xsl:apply-templates select='following-sibling::abbre[1]'/>
        </tr>
    </xsl:template>

    <xsl:template match="abbre">
        <td><xsl:apply-templates/></td>
    </xsl:template>

</xsl:stylesheet>

By using small templates that are applied, you simplify your stylesheet. Additionally, there is no real reason to use xsl:value-of here - the built-in templates will do the right thing. You will end up with simpler templates that are easier to understand.

这篇关于以表格格式将XML转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆