Arpegius
Arpegius

Reputation: 5887

Using XSLT as xhtml link extractor

I'm starting using XSLT and write this scipt:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="span[@class='thumb']" >
    Link: <xsl:value-of select="$base" /><xsl:value-of select="a/@href" />
</xsl:template>

<xsl:template match="/">
    Base href: <xsl:value-of select="$base" />
    <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

And using this command:

xsltproc --html --param base "'http://example.com'" lista.xslt test.html

I need to get list of Links, but I get whole page on output. What's wrong? How can I get it works?

Upvotes: 1

Views: 264

Answers (2)

Carl Smotricz
Carl Smotricz

Reputation: 67760

There's a default template that matches essentially everything if you let it. Your 4th last line calls that template.

That's part of the problem. The rest can probably be taking care of by matching just the stuff you're looking for, directly in the top-level template.

Upvotes: 1

Derek Slager
Derek Slager

Reputation: 13841

There are some default templates which are unseen here. The really easy way to resolve it is to just explicitly limit to the span elements you're matching as below. Otherwise, you can override the default templates.

<xsl:template match="/">
  Base href: <xsl:value-of select="$base" />
  <xsl:apply-templates select="//span[@class='thumb']" />
</xsl:template>

Upvotes: 3

Related Questions