Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

removing the character from attribute values in xml through xslt

Posted on 2015-01-14
4
Medium Priority
?
177 Views
Last Modified: 2015-01-15
i have the below xml , need to change it to the result xml mentioned below, i have taken help from the forum members for the same earlier and have the following xslt(modified a bit to include additional tags), it is working fine to remove the the characters "\b." and "\b0". from  Section[@type='Income']/Summary/Amount/@Name but not from
"Section[@type='Income']/Summary/Persons/Person/@Name , could you please let me know where am i going wrong on this ..? i need to change the characters in this particular "Section[@type='Income']" as i have similar tags with diff attribute names hat i need to keep it as is.


========xslt=========

<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="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="Section[@type='Income']/Summary/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 name="strip-name">
        <xsl:param name="str"/>
        <xsl:value-of select="substring-before(substring-after($str, '\b.'), '\b0.')"/>
    </xsl:template>
 
<xsl:template match="Section[@type='Income']/Summary/Persons/Person/@Name">
        <xsl:attribute name="Name">
            <xsl:call-template name="strip-name-inc">
                <xsl:with-param name="str" select="."></xsl:with-param>
            </xsl:call-template>
        </xsl:attribute>
    </xsl:template>
   
    <xsl:template name="strip-name-inc">
        <xsl:param name="str1"/>
        <xsl:value-of select="substring-before(substring-after($str1, '\b.'), '\b0.')"/>
    </xsl:template>
   
</xsl:stylesheet>
  =========================================================================

========================
current xml
==========================


<Section type="Expense">
                                    <SubDetails>
                                                <Sub>
                                                            <HeaderLogo>d:\Apps\MCI\PRD\Adobe\Corr\Logo\icon_elig_bills.gif</HeaderLogo>
                                                            <Header>Expenses</Header>
                                                            <AssetSubHeader>Who has expenses?</AssetSubHeader>
                                                            <Caption>Who has expenses?</Caption>
                                                            <TotalText>Total expenses</TotalText>
                                                            <Month1>12/2014</Month1>
                                                            <Month2>01/2015</Month2>
                                                </Sub>
                                    </SubDetails>
                                    <Persons>
                                                <Person>
                                                            <Name>\b.GILBERTO\b0.</Name>
                                                            <Source>Goods, supplies and material cost</Source>
                                                </Person>
                                                <Person>
                                                            <Name>\b.GILBERTO\b0.</Name>
                                                            <Source>Other</Source>
                                                </Person>
                                    </Persons>
                                    <Summary>
                                                <Amount Name="\b.GILBERTO\b0." Source="Goods, supplies and material cost" Month="12/2014">29.17</Amount>
                                                <Amount Name="\b.GILBERTO\b0." Source="Other" Month="12/2014">8.33</Amount>
                                                <Amount Name="\b.GILBERTO\b0." Source="Goods, supplies and material cost" Month="01/2015">29.17</Amount>
                                                <Amount Name="\b.GILBERTO\b0." Source="Other" Month="01/2015">8.33</Amount>
                                    </Summary>
                        </Section>

need to remove the "\b." and "\b0." from the <Section type="Expense".

the result xml needs to be

<Section type="Expense">
                                    <SubDetails>
                                                <Sub>
                                                            <HeaderLogo>d:\Apps\MCI\PRD\Adobe\Corr\Logo\icon_elig_bills.gif</HeaderLogo>
                                                            <Header>Expenses</Header>
                                                            <AssetSubHeader>Who has expenses?</AssetSubHeader>
                                                            <Caption>Who has expenses?</Caption>
                                                            <TotalText>Total expenses</TotalText>
                                                            <Month1>12/2014</Month1>
                                                            <Month2>01/2015</Month2>
                                                </Sub>
                                    </SubDetails>
                                    <Persons>
                                                <Person>
                                                            <Name>GILBERTO</Name>
                                                            <Source>Goods</Source>
                                                </Person>
                                                <Person>
                                                            <Name>GILBERTO</Name>
                                                            <Source>Other</Source>
                                                </Person>
                                    </Persons>
                                    <Summary>
                                                <Amount Name="GILBERTO" Source="Goods" Month="12/2014">29.17</Amount>
                                                <Amount Name="GILBERTO" Source="Other" Month="12/2014">8.33</Amount>
                                                <Amount Name="GILBERTO" Source="Goods" Month="01/2015">29.17</Amount>
                                                <Amount Name="GILBERTO" Source="Other" Month="01/2015">8.33</Amount>
                                    </Summary>
                        </Section>



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
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 40549946
the obvious is that you are passing a parameter $str but declare $str1 meaning that you won't use the passed in parameter at all
That is your bug, drop the '1'
Having said that... the point of having a named template with the functionality is that you can reuse it (as a function)
So, you should drop this entirely
<xsl:template name="strip-name-inc">
         <xsl:param name="str1"/>
         <xsl:value-of select="substring-before(substring-after($str1, '\b.'), '\b0.')"/>
     </xsl:template>

and turn this
<xsl:call-template name="strip-name-inc">
into
<xsl:call-template name="strip-name">
0
 

Author Comment

by:sri1209
ID: 40551376
Thank you for trying to help me out here, but being new to this and not able to understand how to make it work in a slightly different requirement , i would like to know and learn how can i remove the "\b." and \b0." from the
"Section[@type='Income']/Summary/Persons/Person/Name

the following xslt works fine to remove the "\b." and "\b0." from Section[@type='Income']/Summary/Amount/@Name
current xslt:


========xslt=========

<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="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="Section[@type='Income']/Summary/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>
-----this part of the xslt does not work it removes the whole <Name >tag from xml not from the value of the <Name>---
<xsl:template match="Section[@type='Income']/Summary/Persons/Person/Name">
        <xsl:attribute name="Name">
            <xsl:call-template name="strip-name-inc">
                <xsl:with-param name="str" select="."></xsl:with-param>
            </xsl:call-template>
        </xsl:attribute>
    </xsl:template>

   
    <xsl:template name="strip-name">
        <xsl:param name="str"/>
        <xsl:value-of select="substring-before(substring-after($str, '\b.'), '\b0.')"/>
    </xsl:template>
 
</xsl:stylesheet>

Thank you .
0
 

Author Comment

by:sri1209
ID: 40551438
figured it out now , thank you for the help.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40551952
welcome
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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)
Suggested Courses

610 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