To transform XML to HTML in table format

2019-08-21 21:10发布

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

标签: html xml xslt
1条回答
淡お忘
2楼-- · 2019-08-21 21:57

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.

查看更多
登录 后发表回答