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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:50:02+00:00 2026-06-04T07:50:02+00:00

In XSLT, there is a remove() function for sequences. Given a sequence and a

  • 0

In XSLT, there is a remove() function for sequences. Given a sequence and a position, it returns the sequence minus the item at the given position.

The question is: How do I employ this function in an actual XSLT file?

The only place I’ve found mention of an example that isn’t just a regurgitation of the function spec completely devoid of context is here: http://books.google.com/books?id=W6SpffnfEPoC&pg=PA776&lpg=PA776&dq=xslt+%22remove+function%22&source=bl&ots=DQQrnXF_nB&sig=nrJtpEvYjBaZU0K8iAtdPTGUIbI&hl=en&sa=X&ei=QOq8T7aPDOyI6AHh-JBP&ved=0CEQQ6AEwAQ#v=onepage&q=xslt%20%22remove%20function%22&f=false

Unfortunately, the stylesheet examples are on pages 777 and 778, which are, of course, not included. And I don’t own that book.

So, does anyone have an example of using the remove() XSLT function in an actual stylesheet?

Edit: Let’s provide a slightly more concrete example, shall we?

I have a sequence in an XSLT. This sequence is comprised of all of the lines from a text file.

<xsl:variable name="lines" select="tokenize(unparsed-text($filePath), '\r?\n')" />

Every one of these lines is a record…except for one, which gives me the record count. So I have the following code for finding that line:

<xsl:variable name="recordCount">
  <xsl:for-each select="$lines[position()]">
    <xsl:variable name="i" select="position()" />
    <xsl:analyze-string select="$lines[$i]" regex="RECORD COUNT = \d+">
      <xsl:matching-substring>
        <xsl:value-of select="replace($lines[$i], '[^0-9]', '')" />
      </xsl:matching-substring>
    </xsl:analyze-string>
  </xsl:for-each>
</xsl:variable>

I do the above before I start looping through the lines to get all the actual records, so my goal here is to remove the “RECORD COUNT” line from the $lines sequence when I find it. That way when I’m looping through grabbing records I don’t have to do a check every time asking “Is this actually not a record, but in fact the RECORD COUNT line? You know, that thing I looked for and found already?”

Edit (2): Based on Martin Honnen’s answer(s), my final XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <!-- I want to produce an XML document. -->
  <xsl:output method="xml" indent="yes" />

  <!-- Path to input text file. -->
  <xsl:param name="filePath" select="TestFile.txt" />

  <!-- Regex in replace() removes leading and trailing blank space. -->
  <xsl:variable name="text" select="replace(unparsed-text($filePath), '(^[\r\n]*\s*[\r\n]+)|([\r\n]+\s*[\r\n]*$)', '')" />

  <!-- Regex in tokenize() sets the delimiter to be any blank space between record lines. -->
  <!-- This effectively removes any blank lines. -->
  <xsl:variable name="lines" select="tokenize($text, '[\r\n]+\s*[\r\n]*')" />

  <!-- This finds the "RECORD COUNT = ?" line. -->
  <xsl:variable name="recordCountIndex"
    select="for $pos in 1 to count($lines) return $pos[matches($lines[$pos], 'RECORD COUNT = \d+')]" />

  <!-- Regex in replace() strips everything that's not a number, leaving only the numeric count. -->
  <!-- Example: "RECORD COUNT = 25" -> "25" -->
  <xsl:variable name="recordCount" select="replace($lines[$recordCountIndex], '[^0-9]', '')" />

  <xsl:template name="main">
    <root>
      <recordCount>
        <!-- The record count value being inserted. -->
        <xsl:value-of select="$recordCount" />
      </recordCount>
      <records>
        <!-- Iterate over the $lines minus the line containing the record count. -->
        <xsl:for-each select="remove($lines, $recordCountIndex)">
          <!-- Items in each record, split by blank space. -->
          <!-- Example: "a b c" -> "[a, b, c]" -->
          <xsl:variable name="record" select="tokenize(., ' ')[position()]" />
          <record>
            <aThing>
              <xsl:value-of select="$record[1]" />
            </aThing>
            <aDifferentThing>
              <xsl:value-of select="$record[2]" />
            </aDifferentThing>
            <someStuff>
              <xsl:value-of select="$record[3]" />
            </someStuff>
          </record>
        </xsl:for-each>
      </records>
    </root>
  </xsl:template>
</xsl:stylesheet>
  • 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-04T07:50:03+00:00Added an answer on June 4, 2026 at 7:50 am

    Well

    <xsl:variable name="seq1" select="1, 2, 3, 4"/>
    <xsl:variable name="seq2" select="remove($seq1, 2)"/>
    

    makes the value of the variable seq2 a sequence of three number values 1, 3, 4.

    [edit]

    Here is an example based on your edited problem description:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
    
      <xsl:output method="text"/>
    
      <xsl:param name="filePath" select="'test2012052301.txt'"/>
    
      <xsl:variable name="lines" select="tokenize(unparsed-text($filePath), '\r?\n')" />
    
      <xsl:variable name="index" as="xs:integer"
        select="for $pos in 1 to count($lines) return $pos[matches($lines[$pos], 'RECORD COUNT = [0-9]+')]"/>
    
      <xsl:variable name="recordCount" as="xs:integer"
        select="xs:integer(replace($lines[$index], '[^0-9]', ''))"/>
    
      <xsl:template name="main">
        <xsl:value-of select="remove($lines, $index)" separator="&#10;"/>
        <xsl:text>count is: </xsl:text>
        <xsl:value-of select="$recordCount"/>
      </xsl:template>
    
    </xsl:stylesheet>
    

    With the text file being for instance

    foo
    bar
    RECORD COUNT = 3
    baz
    

    the stylesheet outputs

    foo
    bar
    baz
    count is: 3
    

    [edit2]
    I think you can shorten the section

      <records>
        <!-- The $lines sequence trimmed down to only consist of valid records. -->
        <!-- (I have found no way around having this intermediate variable.) -->
        <xsl:variable name="records" select="remove($lines, $recordCountIndex)" />
        <xsl:for-each select="$records[position()]">
          <!-- Variable for iteration. Perhaps there's a more elegant way to do this. -->
          <xsl:variable name="i" select="position()" />
          <!-- Items in each record, split by blank space. -->
          <!-- Example: "a b c" -> "[a, b, c]" -->
          <xsl:variable name="recordItems" select="tokenize($records[$i], ' ')" />
          <record>
            <item1>
              <xsl:value-of select="$recordItems[1]" />
            </item1>
            <item2>
              <xsl:value-of select="$recordItems[2]" />
            </item2>
            <item3>
              <xsl:value-of select="$recordItems[3]" />
            </item3>
          </record>
        </xsl:for-each>
      </records>
    

    of your stylesheet to

      <records>
        <xsl:for-each select="remove($lines, $recordCountIndex)">
          <record>
            <xsl:for-each select="tokenize(., ' ')[position() lt 4]">
              <xsl:element name="item{position()}">
                <xsl:value-of select="."/>
              </xsl:element>
            </xsl:for-each>
          </record>
        </xsl:for-each>
      </records>
    

    Actually the predicate position() lt 4 is only needed if a line can contain more than three tokens.

    And as a note, I have now seen a construct like for-each select="$records[position()] two times in your post, that predicate with [position()] is complete useless, you can simply use for-each select="$records".

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

Sidebar

Related Questions

Is there a URL encoding function in XSLT version 1? I need something similar
Is there a way of restricting a section of XSLT to a single node,
What advantages are there for using either XSLT or Linq to XML for HTML
Is there any replacement for saxon:if and saxon:before functions in XSLT 2.0 / XPath
What XSLT processor should I use for Java transformation? There are SAXON, Xalan and
In my web application, I display the search results using XSLT. There are some
I have some complex XSLT 2.0 transformations. I'm trying to find out if there
Is there any trick to match two XML by one XSLT? I mean the
Is there any limit for XML size for which XSLT is applied by XslCompiledTransform
There is large website all data comes from database , I want to remove

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.