[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: parsing parens in the park
At 2008-09-28 12:29 -0400, Syd Bauman wrote:
In an XSLT 2.0 stylesheet I have a string (call it $park) that is of the format name1 (value1) name2 (value2) name3 (value3) ... where the whitespace is optional, but the parentheses are not. Every name is guaranteed to be an unqualified XML Name. Every name is guaranteed to have an associated value, which could be a string of most anything, including the null string or one that contains *matched pairs of parentheses*. (A previous validation stage has, in theory, guaranteed that there are no unmatched single parentheses in a value.) True, but a key table always contains tree nodes and is indexed by values ... you cannot load up a key table with strings. b) there's gotta be a better way. I would have used analyze-string, as below, except that I haven't got the time right now to figure out how to check for and preserve a value that contains nested parentheses. So the answer below is incomplete. I hope this helps. . . . . . . . . . . . Ken t:\ftemp>type syd.xml <?xml version="1.0" encoding="US-ASCII"?> <tests> <test>name1 (value1) name2 (value2) name3 (value3)</test> <test>name1 (value1)name2 () name3(value3)</test> <test>name1(value1)name2()name3()</test> </tests> t:\ftemp>type syd.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xsd" version="2.0"> <xsl:output method="text"/> <xsl:template match="test"> With: <xsl:value-of select="."/> <xsl:text> </xsl:text> <xsl:analyze-string select="." regex="\s*(\i\c*)\s*\((.*?)\)\s*"> <xsl:matching-substring> <xsl:choose> <xsl:when test="regex-group(1)='name-check'"> <!--do something--> </xsl:when> <xsl:when test="regex-group(2)='value-check'"> <!--do something--> </xsl:when> <xsl:otherwise> <xsl:value-of select="'[Got',regex-group(1), 'with',regex-group(2),']'"/> <xsl:text> </xsl:text> </xsl:otherwise> </xsl:choose> </xsl:matching-substring> <xsl:non-matching-substring> <xsl:message> <xsl:text>Whoops! What happened here: </xsl:text> <xsl:value-of select="."/> </xsl:message> </xsl:non-matching-substring> </xsl:analyze-string> </xsl:template> </xsl:stylesheet> t:\ftemp>xslt2 syd.xml syd.xsl con
t:\ftemp>
|
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
|