Link to home
Start Free TrialLog in
Avatar of sri1209
sri1209

asked on

removing the character from attribute values in xml through xslt

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
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sri1209
sri1209

ASKER

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 .
Avatar of sri1209

ASKER

figured it out now , thank you for the help.