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">
<xsl:output method="xml" indent="yes" />
<xsl:param name="filePath" select="TestFile.txt" />
<xsl:variable name="text" select="replace(unparsed-text($filePath), '(^[\r\n]*\s*[\r\n]+)|([\r\n]+\s*[\r\n]*$)', '')" />
<xsl:variable name="lines" select="tokenize($text, '[\r\n]+\s*[\r\n]*')" />
<xsl:variable name="recordCountIndex"
select="for $pos in 1 to count($lines) return $pos[matches($lines[$pos], 'RECORD COUNT = \d+')]" />
<xsl:variable name="recordCount" select="replace($lines[$recordCountIndex], '[^0-9]', '')" />
<xsl:template name="main">
<root>
<recordCount>
<xsl:value-of select="$recordCount" />
</recordCount>
<records>
<xsl:for-each select="remove($lines, $recordCountIndex)">
<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>