Project

Profile

Help

Nest headlines wih xsl

Added by Olli Ver over 8 years ago

I have tried to nest my headlines like this It did not work. Does anyone have an idea or solution? This is source @

<title>Text</title>

H1

H1 Text

H2

H2 Text

H2

H2 Text

H2

H2 Text

H3

H3 Text

H3

H3 Text

H2

H2 Text

@ The result should look like this @ <title>Text</title>

<title>H1</title>

H1 Text

<title>H2</title>

H2 Text

<title>H2</title>

H2 Text

<title>H2</title>

H2 Text

<title>H3</title>

H3 Text

<title>H3</title>

H3 Text

@ I tried this but it did not work @
<xsl:template match="body">
    <xsl:apply-templates select="title|h1" />
</xsl:template>

<xsl:template match="*[starts-with(local-name(), 'h')]">
    <topic>
      <xsl:attribute name="id">
        <xsl:attribute name="id" select="generate-id()" />
      </xsl:attribute>
    
    <xsl:copy-of select="title/@id"/><xsl:attribute name="outputclass">konzept</xsl:attribute>
        <title>
          <!-- <xsl:copy-of select="title/@*"/> -->
          <xsl:value-of select="title"/>
        </title>
        <body>
          <!-- <xsl:value-of select="@*|node()" /> -->
          <!-- <xsl:apply-templates select="@*|node()|comment()"/> -->
          <xsl:copy-of select="@*|node()"/>
          <!-- <xsl:apply-templates select="*|text()|comment()"/> -->
        </body>
          <xsl:apply-templates select="key('group', generate-id())" />
        
    </topic>
</xsl:template>      

<xsl:template match="@*|node()">
  <xsl:apply-templates select="*|text()|comment()"/>
</xsl:template>@

Many thanks for your help


Replies (1)

RE: Nest headlines wih xsl - Added by Michael Kay over 8 years ago

This list is intended for Saxon-specific questions - for general XSLT coding questions you might be better off on StackOverflow or on the mulberrytech xsl-list.

This is the classic use case for xsl:for-each-group/@group-starting-with. If we simplify the problem so you are turning


  

1

1.1

1.2

2

2.1

2.2

into


   
      
      
   
   
      
      
   

Then you can do

  
    
       
        
            
        
       
    
  

You can generalize this to an arbitrary number of levels by making it recursive: see my paper here:

http://www.saxonica.com/papers/ideadb-1.1/mhk-paper.xml

    (1-1/1)

    Please register to reply