[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

removing the character from attribute values in xml through xslt

Posted on 2015-01-14
4
Medium Priority
?
178 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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 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