Subject: [xsl] Sorting substitution instructions by max. length of matches From: "Yves Forkl (SRZ)" <Y.Forkl@xxxxxx> Date: Fri, 05 Oct 2007 16:27:15 +0200 |
<substitution> <old>regex</old> <new>replacement</new> </substitution>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="http://xmlns.srz.de/yforkl/xslt/functions" exclude-result-prefixes="my xs">
<xsl:function name="my:annex-max-match-length-to-instruction" as="node()*"> <xsl:param name="inputtext" as="xs:string"/> <xsl:param name="substitution_instruction" as="node()"/>
<xsl:variable name="regex" select="string($substitution_instruction/old)"/> <xsl:variable name="max_match_length" select="max(my:lengths-of-remaining-matches($inputtext, $regex))"/>
<!-- Add max. match length to subst. instruction, drop those without matches --> <xsl:if test="$max_match_length ne 0"> <xsl:element name="substitution"> <xsl:element name="max_match_length"> <xsl:sequence select="$max_match_length"/> </xsl:element> <xsl:copy-of select="$substitution_instruction/old"/> <xsl:copy-of select="$substitution_instruction/new"/> </xsl:element> </xsl:if> </xsl:function>
<xsl:function name="my:lengths-of-remaining-matches" as="xs:integer*"> <xsl:param name="inputtext" as="xs:string*"/> <xsl:param name="regex" as="xs:string"/>
<xsl:variable name="length_of_next_match" select="if (not (matches($inputtext, $regex))) then 0 else string-length(replace($inputtext, concat('^.*?(', $regex, ').*'), '$1'))"/>
<!-- Return 0 if no match (zero-length regexes can't occur) --> <xsl:sequence select="$length_of_next_match"/>
<!-- Examine further matches by recursive call --> <xsl:if test="$length_of_next_match ne 0"> <xsl:sequence select="my:lengths-of-remaining-matches( replace($inputtext, concat('^.*?', $regex), ''), $regex)"/> </xsl:if> </xsl:function>
<!-- sample data --> <xsl:variable name="input" select="'abcddddxxxxxxxyyyabcxxxxxabcdxabc'"/>
<!-- sample data --> <xsl:variable name="substitution_instructions"> <xsl:element name="substitution"> <xsl:element name="old">[a-e]+</xsl:element> <xsl:element name="new">***</xsl:element> </xsl:element> <xsl:element name="substitution"> <xsl:element name="old">d</xsl:element> <xsl:element name="new">#</xsl:element> </xsl:element> <xsl:element name="substitution"> <xsl:element name="old">123</xsl:element> <xsl:element name="new">...</xsl:element> </xsl:element> <xsl:element name="substitution"> <xsl:element name="old">c+</xsl:element> <xsl:element name="new">#</xsl:element> </xsl:element> <xsl:element name="substitution"> <xsl:element name="old">x+y*</xsl:element> <xsl:element name="new">+++</xsl:element> </xsl:element> </xsl:variable>
<xsl:variable name="substitution_instructions_sorted"> <xsl:perform-sort> <xsl:sort select="max_match_length" data-type="number" order="descending"/> <xsl:sort select="old" order="ascending"/> <xsl:for-each select="$substitution_instructions/substitution"> <xsl:sequence select="my:annex-max-match-length-to-instruction($input, .)"/> </xsl:for-each> </xsl:perform-sort> </xsl:variable>
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] DOE an attribute to preve, Phill | Thread | Re: [xsl] Sorting substitution inst, David Carlisle |
RE: [xsl] DOE an attribute to preve, Michael Kay | Date | Re: [xsl] Sorting substitution inst, David Carlisle |
Month |