Solved

find and replace in xml through xslt

Posted on 2015-01-21
7
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40563194
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
ID: 40563455
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
ID: 40563779
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 40563799
<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
ID: 40563801
See how I also simplified the @type to element name processing
0
 

Author Comment

by:sri1209
ID: 40564296
Thank you Geert Bormans., excellent solution.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40564305
welcome
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

631 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