skip to Main Content

Haven’t found this anywhere so I ask it here.
I have a xml document structured as follows:

<root>
    <row>Row 1</row>
    <row>Row 2</row>
    <row>Row 3</row>
    <row>Row 4</row>
    <row>Row 5</row>
    <row>Row 6</row>
    <row>Row 7</row>
    <row>Row 8</row>
    <row>Row 9</row>
    <row>Row 10</row>
</root>

In a xsl document,I want to display the rows in a range.For example,I want to display only the rows from Row 3 to Row 8,excluding the first and last 2 rows.
I know how to set the limit of the rows but that displays rows starting from Row 1 (the very first).
Edit:typos

2

Answers


  1. You can use a predicate based on position:

    select="row[position() >= 3 and position() &lt;= 8]"
    

    This could be in an xsl:apply-templates or in a xsl:for-each or even in a xsl:copy-of instruction (all from the context of root). You didn’t post your attempt or the expected result, so we can only guess.

    Login or Signup to reply.
  2. To display a specific range of rows in your XML using XSLT, you can utilize the position() function along with conditional statements. Here’s an example XSLT that displays rows from Row 3 to Row 8:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <!-- Identity template to copy everything as is -->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
      <!-- Template to match 'row' elements within a specified range -->
      <xsl:template match="root">
        <xsl:copy>
          <xsl:apply-templates select="row[position() &gt;= 3 and position() &lt;= 8]"/>
        </xsl:copy>
      </xsl:template>
    
      <!-- Output settings for indentation -->
      <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    
    </xsl:stylesheet>
    

    The Output:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search