Link to home
Start Free TrialLog in
Avatar of sri1209
sri1209

asked on

find and replace in xml through xslt

Hi,

I need to replace the characters "\b." and "\b0." in the xml using xslt with <b> and <\b>, i came through the following code from one of the resolved topics in the forum, the code works for most of the part but its giving me an error saying :

"The value of attribute "select" associated with an element type "xsl:with-param" must not contain the '<' character.

the following is the code from the experts on the forum, but i just wanted to make it work for my issue.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="ISO-8859-1"/>
    <xsl:template match="/">
        <html>
            <xsl:apply-templates/>
        </html>
    </xsl:template>
    <xsl:template match="cash/Footer">
            <xsl:variable name="step1">
                <xsl:call-template name="replace-str">
                    <xsl:with-param name="str" select="."/>
                    <xsl:with-param name="find" select="'\b.'"/>
                    <xsl:with-param name="replace" select="'<b>'"></xsl:with-param>
                </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="replace-str">
                <xsl:with-param name="str" select="$step1"/>
                <xsl:with-param name="find" select="'\b0.'"/>
                <xsl:with-param name="replace" select="'</b>'"></xsl:with-param>
            </xsl:call-template>
        </xsl:template>
        <xsl:template name="replace-str">
            <xsl:param name="str"/>
            <xsl:param name="find"/>
            <xsl:param name="replace"/>
            <xsl:choose>
                <xsl:when test="contains($str, $find)">
                    <xsl:value-of select="substring-before($str, $find)"/>
                    <xsl:value-of select="$replace"/>
                    <xsl:call-template name="replace-str">
                        <xsl:with-param name="str" select="substring-after($str, $find)"/>
                        <xsl:with-param name="find" select="$find"/>
                        <xsl:with-param name="replace" select="$replace"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$str"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
</xsl:stylesheet>


Also it removes the tags <Footer> from the xml
Thanks in advance.
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

XSLT is a bit tricky
In order to make an output tree, it needs to be wellformed
so you can't simply add a <b> without adding a </b>

the above code is replacing a string with a string
you could do like this
<xsl:with-param name="replace" select="'&lt;/b>'"></xsl:with-param>

Open in new window

but that will likely not help you
so you want to replace them in pairs... always

Now you need to tell me whether you need a generic solution (and as such you will need the recursion)
or the solution I gave earlier would suffice
(be it that earlier on we remove the \b0 instead of adding a <b>

For not removing the footer tag... add a <xsl:copy>...</xsl:copy> as a first and only child of the template
and put what is in the template now at the ...
Avatar of sri1209
sri1209

ASKER

Hi Geert Bormans,

Thanks, i would request a  recursion solution for my problem to  manipulate the  "\b." and "\b0." with <b> and </b> and \n with <br/>

my xml:

<Cash>
                                    <TitleLogo> cash.gif</TitleLogo>
                                    <Title>Your Cash Assistance Benefits</Title>
                                    <Approval>
                                                <Title>Who qualifies?</Title>
                                                <TitleLogo> eligible.gif</TitleLogo>
                                                <Header1>Who qualifies?</Header1>
                                                <Header2>When?</Header2>
                                                <Header3>How Much?</Header3>
                                                <Qualified>
                                                            <Person>
                                                                        <Names>\b. PAT, PEN, TAM, TON\b0.\n\n\n\n\n.</Names>
                                                                        <When>Dec 26, 2014</When>
                                                                        <HowMuch>$158.00 twice per month</HowMuch>
                                                            </Person>
                                                </Qualified>
                                                <Footer>\b.  PAT, PEN, TAM, TON: \b0.Effective December 26, 2014, you no longer qualify for the $50 monthly work expenses payment because no one receiving has income from work that we use to determine your monthly benefit. >\b.To get this benefit, \b0. you and all members of your family must qualify for, and you or someone in your family must be working and have earned income that we use to determine your monthly benefit.\n\n.
                                                </Footer>
                                    </Approval>
                        </Cash>

Current xsl :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
    <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
           
            <xsl:template match="/">
        <xsl:copy>
            <xsl:apply-templates/>
       </xsl:copy>
    </xsl:template>
 
    <xsl:template match="Correspondance/Ledger/Section[@type='Income']">
        <Income><xsl:apply-templates select="@*|node()" /></Income>
    </xsl:template>
   <xsl:template match="Correspondance/Ledger/Section[@type='Expense']">
        <Expense><xsl:apply-templates select="@*|node()" /></Expense>
    </xsl:template>
<xsl:template match="Correspondance/Ledger/Section[@type='Deduction']">
        <Deduction><xsl:apply-templates select="@*|node()" /></Deduction>
    </xsl:template>
<xsl:template match="Correspondance/BenefitsDetails/Category[@type='others']">
        <others><xsl:apply-templates select="@*|node()" /></others>
</xsl:template>
  <xsl:template match="Correspondance/BenefitsDetails/Category[@type='rejection']">
        <rejection><xsl:apply-templates select="@*|node()" /></rejection>
</xsl:template>
 
  <xsl:template match="Person/Name">
        <xsl:copy>
            <xsl:call-template name="strip-name">
                <xsl:with-param name="str" select="."></xsl:with-param>
            </xsl:call-template>
        </xsl:copy>
    </xsl:template>
           
 
 
    
    <xsl:template match="Amount/@Name">
        <xsl:attribute name="Name">
            <xsl:call-template name="strip-name">
                <xsl:with-param name="str" select="."></xsl:with-param>
            </xsl:call-template>
        </xsl:attribute>
    </xsl:template>
           
 
     
<xsl:template match="Cash/Approval/Footer">
 
            <xsl:variable name="step1">
   <xsl:copy>
                <xsl:call-template name="replace-str">
  
                    <xsl:with-param name="str" select="."/>
                    <xsl:with-param name="find" select="'\b.'"/>
                    <xsl:with-param name="replace" select="'&lt;b>'"></xsl:with-param>
                </xsl:call-template>
</xsl:copy>
            </xsl:variable>
            <xsl:call-template name="replace-str">
                <xsl:with-param name="str" select="$step1"/>
                <xsl:with-param name="find" select="'\b0.'"/>
                <xsl:with-param name="replace" select="'&lt;/b>'"></xsl:with-param>
            </xsl:call-template>
        </xsl:template>
        <xsl:template name="replace-str">
            <xsl:param name="str"/>
            <xsl:param name="find"/>
            <xsl:param name="replace"/>
            <xsl:choose>
                <xsl:when test="contains($str, $find)">
                    <xsl:value-of select="substring-before($str, $find)"/>
                    <xsl:value-of select="$replace"/>
                    <xsl:call-template name="replace-str">
                        <xsl:with-param name="str" select="substring-after($str, $find)"/>
                        <xsl:with-param name="find" select="$find"/>
                        <xsl:with-param name="replace" select="$replace"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$str"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
 
           
                       
            <xsl:template name="strip-name">
            <xsl:param name="str"/>
            <xsl:choose>
            <xsl:when test="starts-with(normalize-space($str), '\b.') and contains($str, '\b0.') ">
            <xsl:value-of select="substring-before(substring-after($str, '\b.'), '\b0.')"/>
            </xsl:when>
            <xsl:otherwise>
            <xsl:value-of select="$str"/>
            </xsl:otherwise>
            </xsl:choose>
            </xsl:template>
 
           
            <xsl:template name="split-lines">
            <xsl:param name="str"/>
              <xsl:choose>
                  <xsl:when test="contains($str, '\n')">
                      <xsl:value-of select="substring-before($str,'\n')"/>
                                          <xsl:call-template name="split-lines">
                        <xsl:with-param name="str"
            select="substring-after($str, '\n.')"/>
      </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$str"/>
   </xsl:otherwise>
   </xsl:choose>
   </xsl:template>
  
   
</xsl:stylesheet>

Open in new window

also i am not getting the <footer> section even though i have placed the <xsl:copy> as you said,
thank you for looking in to it.
The footer will be removed because you placed them differently from what I suggested.

I will hack an example for fixing this.
However, since you are using XSLT1, my hack will not allow nested <B/>
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
See how I also simplified the @type to element name processing
Avatar of sri1209

ASKER

Thank you Geert Bormans., excellent solution.