Groups > Design > Microsoft xsl > Re: "Embedded" XML in contains() and string-before() / string-after()




"Embedded" XML in contains() and string-before() /
string-after()

"Embedded" XML in contains() and string-before() / string-after()
Mon, 14 Apr 2008 18:36:30 -070
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>
Post Reply
Re: "Embedded" XML in contains() and string-before() / string-after() functions.
Tue, 15 Apr 2008 03:12:55 -070

<riderchap@gmail.com> wrote in message 
news:e70d57c8-210f-4bc4-96ee-0f83589ed4e0@l42g2000hsc.googlegroups.com...
> 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 
Post Reply
Re: "Embedded" XML in contains() and string-before() / string-after()
Tue, 15 Apr 2008 13:22:54 +020
riderchap@gmail.com wrote:

> 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".

Here is an XSLT 1.0 stylesheet:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

   <xsl:template match="*">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="@*">
     <xsl:attribute name="{name()}">
       <xsl:call-template name="double-string">
         <xsl:with-param name="text" select="."/>
       </xsl:call-template>
     </xsl:attribute>
   </xsl:template>

   <xsl:template match="text()">
     <xsl:call-template name="double-string">
       <xsl:with-param name="text" select="."/>
     </xsl:call-template>
   </xsl:template>

   <xsl:template match="comment() | processing-instruction()">
     <xsl:copy/>
   </xsl:template>

   <xsl:template name="double-string">
     <xsl:param name="text"/>
     <xsl:param name="string"
select="&quot;'&quot;"/>
     <xsl:choose>
       <xsl:when test="contains($text, $string)">
         <xsl:value-of select="concat(substring-before($text, $string),

$string, $string)"/>
         <xsl:call-template name="double-string">
           <xsl:with-param name="text"
select="substring-after($text, 
$string)"/>
           <xsl:with-param name="string"
select="$string"/>
         </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
         <xsl:value-of select="$text"/>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>

</xsl:stylesheet>


-- 

	Martin Honnen --- MVP XML
Post Reply
Re: "Embedded" XML in contains() and string-before() / string-after()
Wed, 16 Apr 2008 10:45:48 -070
Thank you Joe Fawcett and Martin Honnen.

That was really helpfull.

Post Reply
about | contact