[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: recursive replacing strings with nodes
James Cummings wrote:
Ok, I've been beating my brains out against this one for awhile trying recursive analyze-string and replace() techniques in XSLT2 but just can't get it to do what I want. Here is a stylesheet trying to solve that <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:mf="http://example.com/2010/mf" xmlns:functx="http://www.functx.com" exclude-result-prefixes="xsd mf functx"> <xsl:function name="functx:escape-for-regex" as="xsd:string" > <xsl:param name="arg" as="xsd:string?"/> <xsl:sequence select=" replace($arg, '(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1') "/> </xsl:function> <xsl:param name="abbr-url" as="xsd:string" select="'test2010021902.xml'"/> <xsl:variable name="abbr" as="element(abbr)*" select="doc($abbr-url)/root/choice/abbr"/> <xsl:function name="mf:replace" as="node()*"> <xsl:param name="str" as="xsd:string"/> <xsl:param name="abbr" as="element(abbr)*"/> <xsl:choose> <xsl:when test="$abbr"> <xsl:analyze-string select="$str" regex="{functx:escape-for-regex($abbr[1])}"> <xsl:matching-substring> <xsl:copy-of select="$abbr[1]/../expan/w"/> </xsl:matching-substring> <xsl:non-matching-substring> <xsl:sequence select="mf:replace(., $abbr[position() gt 1])"/> </xsl:non-matching-substring> </xsl:analyze-string> </xsl:when> <xsl:otherwise> <xsl:value-of select="$str"/> </xsl:otherwise> </xsl:choose> </xsl:function> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@*, node()"/> </xsl:copy> </xsl:template> <xsl:template match="seg"> <xsl:copy> <xsl:sequence select="mf:replace(., $abbr)"/> </xsl:copy> </xsl:template> </xsl:stylesheet> -- Martin Honnen http://msmvps.com/blogs/martin_honnen/
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|