[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: parsing a long string into chunks
This is done easily with FXSL: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vendor="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="vendor" > <xsl:import href="strSplit-to-Words.xsl"/> <xsl:output indent="yes" omit-xml-declaration="yes"/> <xsl:template match="/"> <xsl:variable name="vrtfPoints"> <xsl:call-template name="str-split-to-words"> <xsl:with-param name="pStr" select="/*/*"/> <xsl:with-param name="pDelimiters" select="' '"/> </xsl:call-template> </xsl:variable> <polyline> <xsl:apply-templates select="vendor:node-set($vrtfPoints)/*"/> </polyline> </xsl:template> <xsl:template match="word"> <xsl:if test="string(.)"> <point x="{substring-before(., ',')}" y="{substring-after(., ',')}"/> </xsl:if> </xsl:template> </xsl:stylesheet> When applied to your source.xml: <poly> <coordinates>10,10 10,20 20,20</coordinates> </poly> the wanted result is produced: <polyline> <point x="10" y="10" /> <point x="10" y="20" /> <point x="20" y="20" /> </polyline> Hope this helped. ===== Cheers, Dimitre Novatchev. http://fxsl.sourceforge.net/ -- the home of FXSL "Tim Wilkins" <Tim.Wilkins@xxxxxxxxxxxxxxxx> wrote in message news:000301c2ff7e$bb720850$1600040a@xxxxxxxxxxxxxxxxxxx > I have input data of the form > <coordinates>x1,y1 x2,y2 x3,y3</coordinates> > e.g. > <coordinates>10,10 10,20 20,20</coordinates> > There can be any number of space-delimited substrings. At present this is > parsed using substring-before and substring-after with a recursive template, > to generate a sequence of elements <Point X="10" Y="10"/> etc. > Unfortunately this can sometimes be slow for long lists (by which I mean > 5000 or so x,y pairs). In addition I have to divide up the resultant list > of elements into chunks (to avoid exceeding a limit on the number of points > in a line), although this can easily be done on a second pass (it's then a > similar problem to splitting up for a table with a given number of columns). > > i.e. I want > > <coordinates>x1,x2 ... > > to become someething like (ignoring attributes for simplicity!) > > <Polyline> > <Point><Point><Point><Point> > </Polyline> > <Polyline> > <Point><Point><Point><Point> > </Polyline> > > Can anyone think of a better way, especially a way of splitting up without > the need for a second pass? What I'd really like is a trivial way to make > xsl treat the string as a node list split at whitespace, but I suspect that > that isn't possible! > > Tim > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
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
|