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
Solved

find and replace in xml through xslt

Posted on 2015-01-21
7
186 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
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Find out what you should include to make the best professional email signature for your organization.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

808 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