Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: "Embedded" XML in contains() and string-before() / string-after() functions.

From: "Joe Fawcett" <joefawcett@---------.------>
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, &apos;)"/>
>
> 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">&apos;</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>&apos;&apos;</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 



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent