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 8205069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:06:05+00:00 2026-06-07T08:06:05+00:00

I’m having some trouble determining the correct context for a node set. I have

  • 0

I’m having some trouble determining the correct context for a node set. I have a template match that looks a bit like this (using XSL 2.0):

<xsl:template match="//chapter/body/*[matches(name(), '^toc')][crossref][not(crossref/@idref='cip' or crossref/@idref='copy')]">
  <xsl:variable name="curr_id" select="crossref/@idref"/>
  <xsl:element name="location">
    <xsl:attribute name="id"><xsl:value-of select="$curr_id"/></xsl:attribute>
    <xsl:attribute name="order"><xsl:value-of select="position()"/></xsl:attribute>
    <xsl:element name="label">
        <text><xsl:value-of select="."/></text>
    </xsl:element>
  </xsl:element>
</xsl:template>

The XML looks a bit like this:

<chapter id="toc">
  <body>
    <title>Contents</title>
    <tocfm><crossref idref="cip">Catalog</crossref></tocfm>
    <tocfm><crossref idref="copy">Copyright</crossref></tocfm>
    <tocfm><crossref idref="ded">Dedication</crossref></tocfm>
    <toc><crossref idref="prologue">Prologue</crossref></toc>
    <toc><crossref idref="pt1">Book One</crossref></toc>
    <toc><crossref idref="pt2">Book Two</crossref></toc>
    <toc><crossref idref="pt3">Book Three</crossref></toc>
  </body>
</chapter>

My expectation is that the predicate will generate a node set that contains:

<tocfm><crossref idref="ded">Dedication</crossref></tocfm>
<toc><crossref idref="prologue">Prologue</crossref></toc>
<toc><crossref idref="pt1">Book One</crossref></toc>
<toc><crossref idref="pt2">Book Two</crossref></toc>
<toc><crossref idref="pt3">Book Three</crossref></toc>

In other words, all of the toc-like elements that contain a crossref whose idref isn’t cip or copy. The template does this in terms of output, but the position function doesn’t seem to be working on that node set. Instead, it generates a position of ‘3’ for Dedication. However, if I output the value of the node found by following the predicate with [1], I do get Dedication as that value. So, I’m stumped as to what position is working on. Can anyone enlighten me?

  • 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-07T08:06:07+00:00Added an answer on June 7, 2026 at 8:06 am

    You expectation is wrong because it is based on a common misconception about how the context sequence is defined. The context sequence is NOT determined by the template match pattern, but rather by something outside of the template.

    At some point xsl:apply-templates is invoked with a select expression. This expression is evaluated to produces an item sequence. The current context sequences is pushed onto a stack. This item sequence becomes the context sequence. The context sequence is iterated through in sequence order. At each item is visited, this item becomes the context item, and the position() function reflects the position of the context item within the context sequence. At this point the appropriate template is found based on the match expression and priorities. Match expressions DO NOT return sequences. They are simply a boolean test on the the context item – either the context item matches the template rule or it does not. If matched and highest priority, then the template sequence constructor is entered. At this point context sequence, context item and position() have all been defined by the client xsl:apply-templates, NOT by the match condition. After exiting the sequence constructor, control returns back to the client xsl:apply-templates. It will increment the position() number, update the context item, and again find the appropriate template. This may be the same template as the previous item or a different one.

    Illustration

    This style-sheet illustrates that it is the client instruction (apply-templates, for-each etc) that determines the context sequence and position(), not the template match pattern.

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/">
     <t>
       <note message ="apply-templates on EVEN items">
        <xsl:apply-templates select="*/item[ (@id mod 2) = 0]"/>
       </note>  
       <note message ="apply-templates on ODD items">
        <xsl:apply-templates select="*/item[ (@id mod 2) = 1]">
         <xsl:sort select="position()" data-type="number" order="descending"/>
        </xsl:apply-templates>
       </note>  
     </t>
    </xsl:template>
          
    <xsl:template match="item[@colour='blue']">
     <note message='Brought to you by the Blue template!' positon="{position()}"> 
      <xsl:copy-of select='self::*'/>
      </note>  
    </xsl:template>
          
    <xsl:template match="item[@colour='red']">
     <note message='Brought to you by the Red template!' positon="{position()}"> 
      <xsl:copy-of select='self::*'/>
      </note>  
    </xsl:template>
          
    </xsl:stylesheet>
        
    

    … applied on this document …

    <t>
     <item id="1" colour="blue" /> 
     <item id="2" colour="blue" /> 
     <item id="3" colour="blue" /> 
     <item id="4" colour="red" /> 
     <item id="5" colour="red" /> 
     <item id="6" colour="red" /> 
    </t>
    

    … yields this output …

    <t>
      <note message="apply-templates on EVEN items">
        <note message="Brought to you by the Blue template!" positon="1">
          <item id="2" colour="blue" />
        </note>
        <note message="Brought to you by the Red template!" positon="2">
          <item id="4" colour="red" />
        </note>
        <note message="Brought to you by the Red template!" positon="3">
          <item id="6" colour="red" />
        </note>
      </note>
      <note message="apply-templates on ODD items">
        <note message="Brought to you by the Red template!" positon="1">
          <item id="5" colour="red" />
        </note>
        <note message="Brought to you by the Blue template!" positon="2">
          <item id="3" colour="blue" />
        </note>
        <note message="Brought to you by the Blue template!" positon="3">
          <item id="1" colour="blue" />
        </note>
      </note>
    </t>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.