Solved

find and replace in xml through xslt

Posted on 2015-01-21
7
170 Views
Last Modified: 2015-01-22
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.
0
Comment
Question by:sri1209
  • 5
  • 2
7 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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 ...
0
 

Author Comment

by:sri1209
Comment Utility
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.
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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/>
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
Comment Utility
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
	<xsl:strip-space elements="*"/>

	<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/BenefitsDetails/Category[normalize-space(@type)] | Correspondance/Ledger/Section[normalize-space(@type)]">
		<xsl:element name="{@type}">
			<xsl:apply-templates select="@*|node()"/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="Cash/Approval/Footer | Person/Names">
		<xsl:copy>
			<xsl:call-template name="filter-pseudo-tags">
				<xsl:with-param name="str" select="."/>
			</xsl:call-template>
		</xsl:copy>
	</xsl:template>

	<xsl:template name="filter-pseudo-tags">
		<xsl:param name="str"/>
		<xsl:choose>
			<xsl:when test="contains($str, '\b.')">
				<xsl:value-of select="substring-before($str, '\b.')"/>
				<xsl:choose>
					<xsl:when test="contains(substring-after($str, '\b.'), '\b0')">
						<b>
							<xsl:call-template name="filter-pseudo-tags">
								<xsl:with-param name="str" select="substring-before(substring-after($str, '\b.'), '\b0')"/>
							</xsl:call-template>
						</b>
						<xsl:call-template name="filter-pseudo-tags">
							<xsl:with-param name="str" select="substring-after($str, '\b0')"/>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="filter-pseudo-tags">
							<xsl:with-param name="str" select="substring-after($str, '\b.')"/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="contains($str, '\n')">
				<xsl:value-of select="substring-before($str, '\n')"/>
				<br />
				<xsl:call-template name="filter-pseudo-tags">
					<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

0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
See how I also simplified the @type to element name processing
0
 

Author Comment

by:sri1209
Comment Utility
Thank you Geert Bormans., excellent solution.
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
welcome
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now