简体   繁体   中英

Parse HTML using XSLT

I have a html file like this:

<html lang="en,us">
    <head>
        <title>Welcome to the Oracle</title>
    </head>
    <body>
        <table width="100%" cellspacing="5" cellpadding="5" border="0">
            <tr>
                <td>
                    <h2>Welcome to the Oracle</h2>
                    <p>The following processes</p>
                    <ol>
                        <li>Process1</li>
                        <li>Process2</li>
                        <li>Process3</li>
                    </ol>
                </td>
            </tr>
        </table>
    </body>
</html>

If I apply this xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="/html/body/table/tr/td/ol/li">
                <xsl:value-of select="li"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

I am only getting nothing.

If I try this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="/html/body/table/tr/td/ol/li">
                <xsl:value-of select="/html/body/table/tr/td/ol/li"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Then getting this:

Process1 Process1 Process1

How do I get this?

Process1 Process2 Process3

Instead of

    <xsl:for-each select="/html/body/table/tr/td/ol/li">
            <xsl:value-of select="li"/>
    </xsl:for-each>

you want

    <xsl:for-each select="/html/body/table/tr/td/ol/li">
            <xsl:value-of select="."/>
    </xsl:for-each>

as inside the for-each the context is the li element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM