Transforming XML data to HTML using XSL and Php: only printing xml tags without any formatting

前端 未结 2 1582
遥遥无期
遥遥无期 2021-01-23 03:05

I have to transform my xml output to html. I am following a tutorial at link text My code is outputting xml tags in a single line without any formatting with text value.I want

相关标签:
2条回答
  • 2021-01-23 03:13

    Your stylesheet does not output HTML but rather an XHTML fragment, and that in a way (with qualified names) that you need to serve it as application/xml to a browser (like Mozilla, Opera, Safari, IE 9, but not IE 6-8) that understands that content type.

    So make sure you do something like

      header('Content-Type: application/xml');
    

    before sending the content to the browser. Or drop any XHTML namespace and any prefixes from the result elements, then the XSLT stylesheet outputs an HTML fragment many more browsers can parse and understand as text/html and render it as you want.

    0 讨论(0)
  • 2021-01-23 03:25

    To be crossbrowser, I would use this stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes"
                    doctype-public="-//W3C//DTD HTML 4.0//EN"
                    doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
        <xsl:template match="messages">
            <html>
                <body>
                    <xsl:apply-templates select="*[1]"/>
                </body>
            </html>
        </xsl:template>
        <xsl:template match="message[1]">
            <ul>
                <xsl:call-template name="makeListItem"/>
            </ul>
        </xsl:template>
        <xsl:template match="message" name="makeListItem">
            <li>
                <xsl:value-of select="concat('message ',@msg_id)" />
                <xsl:apply-templates select="*[1]"/>
            </li>
            <xsl:apply-templates select="following-sibling::*[1]"/>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <body>
    <ul>
     <li>message 1
      <ul>
       <li>message 2</li>
       <li>message 3
        <ul>
         <li>message 4</li>
         <li>message 5
          <ul>
           <li>message 6
            <ul>
             <li>message 7</li>
             <li>message 8</li>
            </ul>
           </li>
          </ul>
         </li>
        </ul>
       </li>
      </ul>
     </li>
    </ul>
    </body>
    </html>
    

    Render as:

    • message 1
      • message 2
      • message 3
        • message 4
        • message 5
          • message 6
            • message 7
            • message 8

    Note: HTML DOCTYPE, no namespace. Also other approach to hierarchie processing with fine grained traversal.

    0 讨论(0)
提交回复
热议问题