[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XSL stylesheet issue - part 2 (newbie)

Subject: Re: XSL stylesheet issue - part 2 (newbie)
From: "Piez, Wendell A. (Fed) wendell.piez@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 21 Oct 2024 22:06:03 -0000
Re:  XSL stylesheet issue - part 2 (newbie)
Hello Frank,

Your XSLT appears to assume that it can find elements marked with @id
attributes, for which it produces links (list items with anchors). But I am
guessing there are no such attributes in the input document, because another
template in the same XSLT is adding them, and why would it do this if they are
already there?

To make the links, select the elements you want to be linked to by the same
rule you first used to find them, then for each of these, add a link that
points to the new, generated id for the element (being assigned in the other
template) - i.e. href="#tocref-{ generate-id(.) }".

Alternatively, separate the marking of targets from the generating of links to
the new targets, in two passes. You could use two template modes, or more than
one XSLT for that.

NB: since we can't see your input, I am guessing.

Good luck,
Wendell

-----Original Message-----
From: Frank Dissinger frank.dissinger@xxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Monday, October 21, 2024 12:05 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  XSL stylesheet issue - part 2 (newbie)

Hi again,

I am stuck again. The next and last step in my XSL script is to create the
hyperlinked TOC entries for all elements with an ID attribute that starts with
the characters "tocref". With the help of AI, I added some XSL templates. The
script is supposed to create a TOC like this:

<p class="toclev1"><a href="#tocref-cx34g">Heading on level 1</a></p> <p
class="toclev2"><a href="#tocref-df38d">Heading on level 2</a></p> <p
class="toclev3"><a href="#tocref-f8op7">Heading on level 3</a></p> etc.

What the script actually does is adding the unique "tocref-..." IDs to the
headings (as before), but it does not create the hyperlinked TOC entries.

Below is the script. I have attached an HTML file which shows an example of
the HTML file output by the script.

Again a namespace issue?

Any help is greatly appreciated!

---------------------------- BEGIN SCRIPT
-------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xpath-default-namespace="http://www.w3.org/1999/xhtml"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  version="3.0">

  <!-- Output settings for XHTML -->
  <xsl:output
    method="html"
    encoding="UTF-8"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    indent="yes"/>

  <!-- Identity template to copy all elements and attributes -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- Adding a unique ID attribute using generate-id() to headings to create
hyperlink targets for the TOC -->
  <xsl:template
    match="*[@id='rn_release_notes']//h2 | *[@id='rn_release_notes']//h3 |
*[@id='rn_release_notes']//p[@class='rn_heading']">
    <xsl:copy>
      <!-- Generating a unique ID for each element -->
      <xsl:attribute name="id">
        <xsl:text>tocref-</xsl:text>
        <xsl:value-of select="generate-id()"/>
      </xsl:attribute>
      <!-- Copy the remaining attributes except "id", and apply templates to
children -->
      <xsl:apply-templates select="@*[name() != 'id']"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Template to generate the TOC -->
  <xsl:template match="div[@id='rn_toc']">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="h1"/>

      <!-- Insert the TOC after the <h1> element -->
      <xsl:for-each select="//*[@id[starts-with(., 'tocref')]]">

        <xsl:variable name="level">
          <xsl:choose>
            <xsl:when test="self::h2">1</xsl:when>
            <xsl:when test="self::h3">2</xsl:when>
            <xsl:when test="self::p[@class='rn_heading']">3</xsl:when>
          </xsl:choose>
        </xsl:variable>

        <xhtml:p class="toclev{level}">
          <xhtml:a href="mailto:#{./@id}";>
            <xsl:value-of select="."/>
          </xhtml:a>
        </xhtml:p>

      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

---------------------------- END SCRIPT
-------------------------------------------------

Frank Dissinger
Documentation Manager
..........................................
CGS ORIS GmbH
frank.dissinger@xxxxxxxxxxxx | http://www.cgs-oris.com/
Kettelerstrasse 24, 63512 Hainburg (Germany)
Phone: +49 6182 9626-0 | Fax: +49 6182 9626-99 Commercial register: Offenbach,
HRB no. 21495 Managing directors: Bernd R\xFCckert, Christoph Thommessen

=============================================================================
======

Von: Frank Dissinger <mailto:frank.dissinger@xxxxxxxxxxxx>
Gesendet: Montag, 21. Oktober 2024 13:12
An: mailto:xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Betreff: XSL stylesheet issue (newbie)

Hi Martin,

I have added the "xpath-default-namespace" attribute and now the script indeed
produces the expected result.

Thank you very much for your great help!

The XSL element looks like this now:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xpath-default-namespace="http://www.w3.org/1999/xhtml" >

BTW: I had to change the XSL version from 1.0 to 2.0 in the XSL element,
otherwise Oxygen would use Saxon 6.5.5 for validation, which does not support
the xpath-default-namespace atttribute. Now it uses "Saxon-PE 12.3" and the
validation error message disappears.

I found out that I can also change this to "version=3.0". The script would
still run without any problems and validation issues.

-----------------------------------------------------------------------------
-----------------------------------

On 17/10/2024 19:41, Martin Honnen mailto:martin.honnen@xxxxxx wrote:
The stylesheet below is supposed to add a unique 'id' attribute named
'tocref001', 'tocref002' etc. to all <h2>, <h3> and <p class="rn_heading">
elements which are descendants of an element with an 'id' attribute named
'rn_release_notes'. The script runs without any errors, but does not add any
IDs.
The sample input has e.g.
  html xmlns=http://www.w3.org/1999/xhtml
meaning it has elements in the XHTML namespace, if you use an XSLT 2 or 3
processor like Saxon Java, SaxonJS, SaxonC or Saxon.NET you can add
  xpath-default-namespace=http://www.w3.org/1999/xhtml
on your xsl:stylesheet element in the XSLT code and your match
<!-- Adding id=toc001 attributes to headings to create hyperlink targets for
the TOC --> <xsl:template match="*[@id='rn_release_notes']//h2 |
*[@id='rn_release_notes']//h3 |
*[@id='rn_release_notes']//p[@class='rn_heading']">
will match XHTML elements.
With more recent version of Saxon (11, 12 at least, I think) you could also
use your original stylesheet code but add the command line option
  -ns:##html5
for execution

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.