Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - "Embedded" XML in contains() and string-before() / string-after() functions. [Thread Next] Re: "Embedded" XML in contains() and string-before() / string-after() functions.To: NULL Date: 4/15/2008 3:13:00 AM <riderchap@g...> wrote in message news:e70d57c8-210f-4bc4-96ee-0f83589ed4e0@l...... > Hi group, > I am trying to write an XSL transform to take an XML as input and > replace all single quotes ( ' ) with two single quotes ( '' ). The > situation is I am only sure about the name of the root tag of my XML. > Within the root tags I may have another tree of XML and I want to > replace all single quotes with two single quotes. > > My input XML will be like <TheRootTag>Some XML string</TheRootTag>. > > For an example that causing me problem > The input XML : > <TheRootTag><InnerTag ID="12'3"> <Tag1>This 's a test</Tag1></ > InnerTag></TheRootTag> > > Note that there are two single quote in the string above "12'3" and > "'s". > > In my XSL I take the the full XML "<InnerTag ID="12'3"> <Tag1>This 's > a test</Tag1></InnerTag>" > and uses the contains() function to search for the single quote. > > Issue #1: The contains() function doesn't finds the single quote in > "12'3". > > Then I use substring-before() function to get the part of the string > before the single quote. > > Issue #2: The substring-before() function strips-off all the XML tags > and just returns only the value parts. (so as substring-after() ). > > For example if pass "<InnerTag ID="12'3"> <Tag1>This 's a test</Tag1></ > InnerTag>" (call it variable $thestring) to substring-before() > function like > <xsl:copy-of select="substring-before($thestring, ')"/> > > it will output only "This ", note that all the tags are gone. (See the > bottom of the mail if you like to see an outline of my XSL program.) > > > My questions are > 1. Is there a way to match the characters in the attribute value in an > XML string passed to contains() and substring-before() / substring- > after() functions. > 2. Is there a way to get the substring with all the XML tags from an > XML string passed to substring-before() and substring-after() > functions. > > If its not possible I think I have to programaticaly parse into the > XML and its attributes and do the replace of characters. > > Thank you for your time. Any helpful reply greatly appreciated. > > -Kannan > > Outline of my XSL. (I know this sample will only replace only one > single quote). > > <xsl:template match="TheRootTag"> > <xsl:variable name="theapos">'</xsl:variable> > <xsl:variable name="thestring" select="."/> > <xsl:choose> > <xsl:when test="contains($thestring, $theapos)"> > <xsl:copy-of select="substring-before($thestring, $theapos)"/ >> > <xsl:text>''</xsl:text> > <xsl:copy-of select="substring-after($thestring, $theapos)"/ >> > </xsl:when> > <xsl:otherwise> > <xsl:copy-of select="$thestring"/> > </xsl:otherwise> > </xsl:choose> > </xsl:template> I would use a version of the identity template and a recursive template to replace the quotes or better yet use XSLT 2.0 and the replace function. You can find a number of pre-written string replacement templates online. -- Joe Fawcett (MVP - XML) http://joe.fawcett.name | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
