Using the html2simplehtml.xsl stylesheet the module is apparently set to grab the
field and use that for the page title. I'd like it to use the field instead. How do I rewrite the xsl code to make it do that?

Comments

teschek’s picture

Sorry, should have previewed that before sending. Adding the H1 html tag in the body of my question wasn't a good idea! My question again:

Using the html2simplehtml.xsl stylesheet the module is apparently set to grab the H1 field and use that for the page title. I'd like it to use the html Title field instead. How do I rewrite the xsl code to make it do that?

teschek’s picture

Is there answer to this question? Is it not something that import_html can do?

dman’s picture

Status: Active » Fixed

Yes, the HI is the default. title is supported as a fallback.
The code is (comparatively) a really easy small XSL tweak.

The following code is very generic and tries to make some guesses, depending on IDs or other ways a page-title may occur in a document.

  <xsl:template name="get-title">
    <!-- 
      Need to deal with the possible difference between 
      document title and H1 display title .
      Look for H1 or anything id=pagetitle first
      If that fails, use the meta title.
      Some exceptions to do so are defined later.
    -->
      <xsl:choose>
  
        <xsl:when test="descendant::*[local-name()='h1']">
            <xsl:value-of select="normalize-space(descendant::*[local-name()='h1'])" />
        </xsl:when>
        
        <xsl:when test="//*[@id='pagetitle']">
          <!-- 
            Nice sites set the id of their title.
            May add other #ids here as I find them
          -->
            <xsl:value-of select="normalize-space(descendant::*[@id='pagetitle'])" />
        </xsl:when>

        <xsl:when test="count(descendant::h2|descendant::xhtml:h2)=1">
          <!-- 
            Maybe it used an h2 because h1 was ugly.
            If there is ONLY ONE h2, use that
          -->
            <xsl:value-of select="normalize-space(descendant::h2|descendant::xhtml:h2)" />
        </xsl:when>
        
        <xsl:otherwise>
          <!-- 
            In practice, the html head title is often more verbose than we want
            and too often it's the same across whole sections, but it'll have to do.
          -->
            <xsl:value-of select="normalize-space(descendant::*[name() = 'title'])" />
        </xsl:otherwise>
        <!--
          Any of these steps may fail if the heading contains only html, eg
          <h1><img src='header.gif' /></h1>
        -->
      </xsl:choose>
  </xsl:template>

(You'll see why I don't use <title> by default in the comments above)

In practice, if you actually know that all you want is the <title> then all you need is a line like:

  <xsl:template name="get-title">
      <xsl:value-of select="//title" />
  </xsl:template>

Life is MUCH easier when you don't have to rely on all that guesswork above.

But due to namespaces going funny sometimes, and possible newlines in HTML that we don't want, see the the line from the middle in the xsl:

  <xsl:template name="get-title">
      <xsl:value-of select="normalize-space(descendant::*[name() = 'title'])" />
  </xsl:template>
teschek’s picture

I followed this instruction:

In practice, if you actually know that all you want is the then
all you need is a line like:  

<xsl:template name="get-title">      
<xsl:value-of select="//title" />   </xsl:template>

Life is MUCH easier
when you don't have to rely on all that guesswork above.

but when I do it grabs the file name rather than the html title field

So instead of <xsl:value-of select="//title" />
I tried
<xsl:value-of select="normalize-space(//xhtml:title)" />
That seemed to work.

dman’s picture

That'll work then!

Yeah, I can never remember when we need the xhtml: namespace or not. There were issues with different parsers and different input, which is why I ended up using name() everywhere.
I just couldn't find the XML function to set the default namespace on a doc. There is probably a proper answer now I don't have to think about PHP4 any more. I should revisit that and tidy things up.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.