XSLT function remove ()

XSLT has a function remove()for sequences. Given the sequence and position, it returns the sequence minus the element at the given position.

Question: How to use this function in a real XSLT file?

The only place I found mentioning an example that is not just breaking the spec function completely out 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 = 0CEQQ6AEwAf% 20 & ved = 0CEQQ6AEwAf% 20 & v = 0CEQQ6AEwAf% 20 &% = vCE = 0CEQQ6AEwAlf

Unfortunately, examples of styles are given on pages 777 and 778, which, of course, are not included. And I do not own this book.

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

Edit: Let me introduce a slightly more specific example: will we?

I have a sequence in XSLT. This sequence consists of all lines from a text file.

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

Each of these lines is a record ... except for one that gives me a record count. Therefore, I have the following code to find this 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>

, , , - "RECORD COUNT" $lines, . , , " , RECORD COUNT? , ?"

(2): , 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 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
2

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

seq2 1, 3, 4.

[]

, :

<?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>

, ,

foo
bar
RECORD COUNT = 3
baz

foo
bar
baz
count is: 3

[edit2] ,

  <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 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>

  <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>

position() lt 4 , .

, for-each select="$records[position()] , [position()] , for-each select="$records".

+1

, .

-, node . ( , " ", , . node, node.)

-, , , , , , , . XSLT XQuery : . "" .

, . remove - : remove($seq, 1). subsequence($seq, 2) $seq[position() gt 1], remove() . , , remove() , .

. " " - . , : " ". , , , , , . , : , remove() .

+1

All Articles