Solved

removing the character from attribute values in xml through xslt

Posted on 2015-01-14
4
167 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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now