Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3319268
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:48:17+00:00 2026-05-17T22:48:17+00:00

I’d like to apologize for the poor title – I really didn’t know how

  • 0

I’d like to apologize for the poor title – I really didn’t know how to phrase it any better. I’m currently working on an XSLT 1.0 script (using xsltproc) which transforms a simple XML format into a text representation suitable for consumption by an PDF generator.

In my XML format, there are just three elements: <book>, <chapter> and <section>. However, due to some nasty feature of the DTD, I have a hard time writing a proper XSLT script to transform the document. Here’s the DTD which describes their relation:

<!ELEMENT book ((chapter|section)*)>
<!ELEMENT chapter (section*)>
<!ELEMENT section (#PCDATA)>

Here’s the my XSLT stylesheet which performs the translation:

<?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="iso-8859-1"/>
<xsl:strip-space elements="*"/>

<xsl:template match="section">
  <xsl:if test="not(preceding-sibling::section)">@BeginSections&#xa;</xsl:if>
  <xsl:text>@Section @Begin&#xa;</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>@End @Section&#xa;</xsl:text>
  <xsl:if test="not(following-sibling::section)">@EndSections&#xa;</xsl:if>
</xsl:template>

<xsl:template match="chapter">
  <xsl:if test="not(preceding-sibling::chapter)">@BeginChapters&#xa;</xsl:if>
  <xsl:text>@Chapter @Begin&#xa;</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>@End @Chapter&#xa;</xsl:text>
  <xsl:if test="not(following-sibling::chapter)">@EndChapters&#xa;</xsl:if>
</xsl:template>

<xsl:template match="/book">
  <xsl:text>@Book @Begin&#xa;</xsl:text>
  <xsl:apply-templates/>
  <xsl:text>@End @Book&#xa;</xsl:text>
</xsl:template>
</xsl:stylesheet>

Now, here comes the tricky part and my question: the DTD makes it possible to have <section> elements as the direct children of <book>. However, I still have to yield the same output as if that /book/section element was actually /book/chapter/section.

So e.g.: <book><section/><chapter/></book> becomes (I indented the output for better readability)

@Book @Begin
  @BeginChapters
  @Chapter @Begin
    @BeginSections
    @Section @Begin
    @End @Section
    @EndSections
  @End @Chapter
  @Chapter @Begin
  @End @Chapter
  @EndChapters
@End @Book

So what I’d like to do is to adjust my XSLT script so that the ‘section’ template somehow also calls the ‘chapter’ template in case the given <section> element is not within a <chapter>. How could I do this?

For what it’s worth, here’s another example. Multiple <section> elements which are not already in a <chapter> should get merged into one. Hence,<book><section/><section/><section/><chapter/><section/></book> yields output for three chapters (the first of which has three sections, the second has none, the third has one section).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:48:17+00:00Added an answer on May 17, 2026 at 10:48 pm

    This stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" encoding="iso-8859-1"/>
        <xsl:strip-space elements="*"/>
        <xsl:template match="chapter|section" mode="chapter" name="makeChapter">
            <xsl:text>  @Chapter @Begin&#xa;</xsl:text>
            <xsl:apply-templates select="self::chapter/node()[1]|
                                         self::section"
                                 mode="section"/>
            <xsl:text>  @End @Chapter&#xa;</xsl:text>
            <xsl:apply-templates 
                 select="self::chapter/following-sibling::node()[1]|
                         self::section/following-sibling::chapter[1] "
                 mode="chapter"/>
        </xsl:template>
        <xsl:template match="section" mode="makeSection" name="makeSection">
            <xsl:text>    @Section @Begin&#xa;</xsl:text>
            <xsl:apply-templates/>
            <xsl:text>    @End @Section&#xa;</xsl:text>
            <xsl:apply-templates select="following-sibling::node()[1]/self::section"
                                 mode="makeSection"/>
        </xsl:template>
        <xsl:template match="section" mode="section">
            <xsl:text>    @BeginSections&#xa;</xsl:text>
            <xsl:call-template name="makeSection"/>
            <xsl:text>    @EndSections&#xa;</xsl:text>
        </xsl:template>
        <xsl:template match="book/chapter|book/section">
            <xsl:text>  @BeginChapters&#xa;</xsl:text>
            <xsl:call-template name="makeChapter"/>
            <xsl:text>  @EndChapters&#xa;</xsl:text>
        </xsl:template>
        <xsl:template match="book">
            <xsl:text>@Book @Begin&#xa;</xsl:text>
            <xsl:apply-templates select="node()[1]"/>
            <xsl:text>@End @Book&#xa;</xsl:text>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    @Book @Begin
      @BeginChapters
      @Chapter @Begin
        @BeginSections
        @Section @Begin
        @End @Section
        @EndSections
      @End @Chapter
      @Chapter @Begin
      @End @Chapter
      @EndChapters
    @End @Book
    

    Note: Fine grained traversal, grouping adjacents book/sections into one Chapter.

    Edit: Corrected following sibling process for Chapters.

    With this input:

    <book>
        <section/>
        <section/>
        <section/>
        <chapter/>
        <section/>
    </book>
    

    Output:

    @Book @Begin
      @BeginChapters
      @Chapter @Begin
        @BeginSections
        @Section @Begin
        @End @Section
        @Section @Begin
        @End @Section
        @Section @Begin
        @End @Section
        @EndSections
      @End @Chapter
      @Chapter @Begin
      @End @Chapter
      @Chapter @Begin
        @BeginSections
        @Section @Begin
        @End @Section
        @EndSections
      @End @Chapter
      @EndChapters
    @End @Book
    

    Edit: Better named templates to understand.

    Note: Five rules: book rule “opens” a book and process first child; book/section|book/chapter rule (always the first because the fine grained transversal) “opens” book chapters, calls makeChapter; makeChapter rule, “opens” a chapter, process first child if context is chapter or self if context is section both in section mode, process next sibling if context is chapter or following chapter if context is section in chapter mode (meaning next chapter); section rule in section mode (because the node by node process, it will always match the first sections for adjacents sections) “opens” chapter sections an calls makeSection rule; makeSection rule “opens” a section an process childs, then process next sibling section in makeSection mode (this rule).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.