Solved

find and replace in xml through xslt

Posted on 2015-01-21
7
200 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

738 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