Jump to content

add a new character at beginning of string


rabbit
 Share

Recommended Posts

Hi,I am quite new to XSLT and intending to convert GML (Geographic Markup Language) to SVG. I have

<gml:coordinates>536039.194,105890.715 536037.361,105897.363 536031.151,105931.173</gml:coordinates>

and I need to put an 'M' in front of the first character and then 'L's where there are spaces. I think I have replaced the spaces with L's using

 <xsl:value-of select="translate(osgb:FeatureCollection /osgb:NetworkMember/RoadLink/osgb:polyline/gml:LineString/gml:coordinates, ' ', ' L ')"/>

but I'm not sure how to stick in the M in front of everything. Can anyone give me some pointers please?Thanks very much.

Link to comment
Share on other sites

What XSLT processor are you using? How (i.e. on the browser, or on the server)?Can you by any chance be using a processor that supports EXSLT's tokenize() function? If so, you can use that function to split all of the coordinates by space, and then loop through them, adding "M" before "value-of"-ing them.Something like:

<xsl:for-each select="str:tokenize(gml:coordinates,' ')/token">M<xsl:value-of select="."/></xsl:for-each>

(again, this will NOT work unless your XSLT processor supports it, and you have something like:

<xsl:stylesheet version="1.0"				xmlns:xsl="http://www.w3.org/1999/XSL/Transform"				xmlns:str="http://exslt.org/strings"				extension-element-prefixes="str">

as your root element.If you have an XSLT 2.0 processor, a similar function is available natively (i.e. any XSLT 2.0 processor would support it).

Link to comment
Share on other sites

What XSLT processor are you using? How (i.e. on the browser, or on the server)?Can you by any chance be using a processor that supports EXSLT's tokenize() function? If so, you can use that function to split all of the coordinates by space, and then loop through them, adding "M" before "value-of"-ing them.Something like:
<xsl:for-each select="str:tokenize(gml:coordinates,' ')/token">M<xsl:value-of select="."/></xsl:for-each>

(again, this will NOT work unless your XSLT processor supports it, and you have something like:

<xsl:stylesheet version="1.0"				xmlns:xsl="http://www.w3.org/1999/XSL/Transform"				xmlns:str="http://exslt.org/strings"				extension-element-prefixes="str">

as your root element.If you have an XSLT 2.0 processor, a similar function is available natively (i.e. any XSLT 2.0 processor would support it).

Thanks for your reply.... as for the XSLT processor, i'm not sure which one I will be using... I intend to convert the GML to SVG only once, it won't be done through a browser, I am only after the finished svg image files. I think I made a mistake in my OP... I don't want an M in front of every coordinate, just in front of the very first coordinate, I don't expect tokenize() would work for this, would it?
Link to comment
Share on other sites

Oh. Only in front of the first coordinate? Well, you can simply include it outside the value-of, and it will be there in the output. Like:

M<xsl:value-of select="translate(osgb:FeatureCollection /osgb:NetworkMember/RoadLink/osgb:polyline/gml:LineString/gml:coordinates, ' ', ' L ')"/>

Link to comment
Share on other sites

Oh. Only in front of the first coordinate? Well, you can simply include it outside the value-of, and it will be there in the output. Like:
M<xsl:value-of select="translate(osgb:FeatureCollection /osgb:NetworkMember/RoadLink/osgb:polyline/gml:LineString/gml:coordinates, ' ', ' L ')"/>

haha, how trivial... I did say I was new to it! Easy when you know how. Thanks!
Link to comment
Share on other sites

Now that I've transformed it I see that my translate function didn't work afterall... the spaces are just removed, not replaced with L's and there is before the M. I tried using normalize-space but it had no effect. Here is my code:

			<xsl:for-each select="/osgb:FeatureCollection/osgb:networkMember/osgb:RoadLink/osgb:polyline/gml:LineString/gml:coordinates">	  								<xsl:variable name="coords">					<xsl:value-of select="/osgb:FeatureCollection/osgb:networkMember/osgb:RoadLink/osgb:polyline/gml:LineString/gml:coordinates"/>				</xsl:variable>					<xsl:variable name="path">		   						M<xsl:value-of select="translate($coords, ' ', ' L ')"/>				</xsl:variable>							 			  <path d ="{$path}" style="stroke:black; fill:none;"/>		 </xsl:for-each>

If anyone could give me any advice, it'd be much appreciated, thanks.

Link to comment
Share on other sites

do not confuse translate with replace. The translate function will substitute characters positionally, not strings. You're example:<xsl:value-of select="translate($coords, ' ', ' L ')"/>says: for each space replace with a spacetry this<xsl:value-of select="translate($coords, ' ', 'L')"/>

Link to comment
Share on other sites

do not confuse translate with replace. The translate function will substitute characters positionally, not strings. You're example:<xsl:value-of select="translate($coords, ' ', ' L ')"/>says: for each space replace with a spacetry this<xsl:value-of select="translate($coords, ' ', 'L')"/>
Thanks for your reply. I ended up using replace because I needed spaces around the L...and instead of using the variable I did
<xsl:value-of select="replace(., ' ', '  L ')"/>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...