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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:53:32+00:00 2026-05-20T04:53:32+00:00

Quite new to xslt so forgive me if this is a basic question –

  • 0

Quite new to xslt so forgive me if this is a basic question – I can’t find the answer either on SO or by searching on Google.

What I am trying to do is return a filtered set of nodes and then have a template match on the first 1 or 2 items in that set and another template match the remainder. However I don’t seem to be able to do this without a <xsl:for-each /> loop (which is highly undesirable as I may be matching 3000 nodes and only treating 1 differently).

Using position() doesn’t work as this is unaffected by the filtering. I’ve tried sorting the result set but this doesn’t seem to take effect early enough to affect the template match. The <xsl:number /> outputs the correct numbers but I can’t use these in a match statement.

I’ve put some example code below. I’m using the unsuitable position() method below to illustrate the problem.

Thanks in advance!

XML:

<?xml version="1.0" encoding="utf-8"?>
<news>
    <newsItem id="1">
        <title>Title 1</title>
    </newsItem>
    <newsItem id="2">
        <title>Title 2</title>
    </newsItem>
    <newsItem id="3">
        <title></title>
    </newsItem>
    <newsItem id="4">
        <title></title>
    </newsItem>
    <newsItem id="5">
        <title>Title 5</title>
    </newsItem>
</news>

XSL:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <ol>
            <xsl:apply-templates select="/news/newsItem [string(title)]" />
        </ol>
    </xsl:template>

    <xsl:template match="newsItem [position() &lt; 4]">
        <li>
            <xsl:value-of select="title"/>
        </li>
    </xsl:template>

    <xsl:template match="*" />
</xsl:stylesheet>

Desired Result:

  1. Title 1
  2. Title 2
  3. Title 5
  • 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-20T04:53:33+00:00Added an answer on May 20, 2026 at 4:53 am

    This one’s actually simpler than you might think. Do:

     <xsl:template match="newsItem[string(title)][position() &lt; 4]">
    

    And drop the [string(title)] predicate from your <xsl:apply-templates select.

    Like this:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="/">
            <ol>
                <xsl:apply-templates select="/news/newsItem" />
            </ol>
        </xsl:template>
    
        <xsl:template match="newsItem[string(title)][position() &lt; 4]">
            <li><xsl:value-of select="position()" />
                <xsl:value-of select="title"/>
            </li>
        </xsl:template>
    
        <xsl:template match="*" />
    </xsl:stylesheet>
    

    What you’re effectively doing here is applying a second filter ([position() &lt; 4]) after your [string(title)] filter, which results in the position() being applied to the filtered list.

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

Sidebar

Related Questions

Im quite new to stackoverflow so I dont know if this question has been
I run into this quite often where a new page is supposedly tested and
I've seen this quite a few times while using Office Interop classes this.CustomXMLParts.Add(MyResources.Data, new
I am quite new all this iPhone stuff. I have no such clear IDea
I'm quite new to programming Google Earth with KML but I've been asked to
Quite new to this but i have created a SQL script that groups by
did a search for this and didn't find an answer, so here goes... I
I'm quite new to Javascript and I was just reading following article. you can
I am quite new trying msgpack. I need to serialize an object (instance of
OK, I am quite new to this Objective-C and JSON thing, so please bare

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.