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

  • SEARCH
  • Home
  • 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 7786969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:31:20+00:00 2026-06-01T20:31:20+00:00

This is not so much ‘how do I do xxx’ but ‘how do I

  • 0

This is not so much ‘how do I do xxx’ but ‘how do I do xxx optimally?’ (really hoping the challenge floats Dimitre’s boat…)

All of the following is complicated by the restriction of the XSL processor (msxsl – basically XSLT 1.0 with a node-set(), replaces() and matches() set of extension functions).

I am generating some metadata from certain elements in a book – let’s say chapters and div[title] elements (to simplify our data model quite a bit).

Page numbers in the book are given by processing instructions in mixed text nodes that might look like this:

<?Page pageId="256"?>

The page number that my element needs to be associated with will either be the first descendant (in the case where the page break is essentially the first piece of content within, say, a chapter (i.e. the chapter starts with a new page)), or else the first preceding::processing-instruction(‘Page’).

Let’s make up a sample document:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <chapter>
        <title><?Page pageId="1"?>Chapter I</title>
        <div>
            <p>Introduction to Chapter</p>
            <p>Second paragraph <?Page pageId="2"?>of introduction</p>
        </div>
        <div>
            <title>Section I</title>
            <p>A paragraph</p>
            <p>Another paragraph<?Page pageID="3"?></p>
        </div>
    </chapter>
    <chapter>
        <title><?Page pageId="4"?>Chapter II</title>
        <div>
            <p>Introduction to Chapter</p>
            <p>...</p>
        </div>
    </chapter>
</book>

(note that although each chapter here starts on a new page, we can’t generally guarantee that as a rule. There’s a blank page at the end of chapter 1, something we see commonly).

I want to get out some information like this (I am fine with XSLT basics, we’re interested in choosing the page numbers):

<meta>
    <meta>
        <field type="title">Chapter I</field>
        <field type="page">1</field>
        <meta>
            <field type="title">Section I</field>
            <field type="page">2</field>
        </meta>
    </meta>
    <meta>
        <field type="title">Chapter II</field>
        <field type="page">4</field>
    </meta>
</meta>

I can do various things using xsl:when statements and the descendant axis to decide which page number is appropriate, but I would much prefer to do something clever matching on processing-instructions, as currently using the descendant axis on large books is making things way too slow to be usable. Keys would be nice, but things are further complicated by being able to use neither variables nor other keys in the @use or @match attributes (and not being able to use sequence constructors, similarly).

Currently the elements I’m interested in finding page numbers for are defined in a key (real world data is much more complex) like the following:

<xsl:key name="auth" match="chapter|div[title]" use="generate-id()"/>

Any suggestions or pointers gratefully received!

  • 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-06-01T20:31:21+00:00Added an answer on June 1, 2026 at 8:31 pm

    Here is a solution using keys, which may be efficient:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kPage"
       match="chapter/title/processing-instruction('Page')"
       use="generate-id(..)"/>
    
     <xsl:key name="kPage"
       match="processing-instruction('Page')"
       use="generate-id(following::div[title][1]/title)"/>
    
     <xsl:template match="*">
      <xsl:apply-templates select=
       "*[1]|following-sibling::*[1]"/>
     </xsl:template>
    
     <xsl:template match="chapter/title[1] | div/title[1]">
      <meta>
        <field type="title"><xsl:value-of select="."/></field>
        <field type="page">
          <xsl:variable name="vPiText"
               select="key('kPage', generate-id())[last()]"/>
          <xsl:value-of select=
          "translate($vPiText,
                     translate($vPiText, '01234567890', ''),
                     ''
                     )"/>
        </field>
    
        <xsl:apply-templates select="*[1]|following-sibling::*[1]"/>
      </meta>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <book>
        <chapter>
            <title>
                <?Page pageId="1"?>Chapter I</title>
            <div>
                <p>Introduction to Chapter</p>
                <p>Second paragraph 
                    <?Page pageId="2"?>of introduction</p>
            </div>
            <div>
                <title>Section I</title>
                <p>A paragraph</p>
                <p>Another paragraph
                    <?Page pageID="3"?></p>
            </div>
        </chapter>
        <chapter>
            <title>
                <?Page pageId="4"?>Chapter II</title>
            <div>
                <p>Introduction to Chapter</p>
                <p>...</p>
            </div>
        </chapter>
    </book>
    

    the wanted, correct result is produced:

    <meta>
       <field type="title">Chapter I</field>
       <field type="page">1</field>
       <meta>
          <field type="title">Section I</field>
          <field type="page">2</field>
       </meta>
    </meta>
    <meta>
       <field type="title">Chapter II</field>
       <field type="page">4</field>
    </meta>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This feels like it should be pretty simple, but not much seems to be
I know this isn't a unique issue but I've not had much luck finding
I did see this question but not much on .NET... I want to write
This is not so much a problem as advice on best practice really. I
This code does not have much context, but will illustrate my question. It's a
This is not so much a technical question but still part of the development
I know this is not much programming related, but I would appreciate if someone
I've read other posts related to this but not get much for myself so
I've been trying to research this but not having much luck. I seem to
This question is not so much programming related as it is deployment related. I

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.