Solved

how to check if a value exist in a xml tag and delete it if exists with xslt

Posted on 2015-01-15
3
308 Views
Last Modified: 2015-01-16
i have the following 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.GILBER\b0.</Name>
                                                            <Source>Goods, supplies and material cost</Source>
                                                </Person>
                                                <Person>
                                                            <Name>\b.GILBER\b0.</Name>
                                                            <Source>Other</Source>
                                                </Person>
                                    </Persons>
                                    <Summary>
                                                <Amount Name="\b.GILBER\b0." Source="Goods, supplies and material cost" Month="12/2014">29.17</Amount>
                                                <Amount Name="\b.GILBER\b0." Source="Other" Month="12/2014">8.33</Amount>
                                                <Amount Name="\b.GILBER\b0." Source="Goods, supplies and material cost" Month="01/2015">29.17</Amount>
                                                <Amount Name="\b.GILBER\b0." Source="Other" Month="01/2015">8.33</Amount>
                                    </Summary>
                        </Section>

Open in new window


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>GILBER</Name>
                                                            <Source>Goods</Source>
                                                </Person>
                                                <Person>
                                                            <Name>GILBER</Name>
                                                            <Source>Other</Source>
                                                </Person>
                                    </Persons>
                                    <Summary>
                                                <Amount Name="GILBER" Source="Goods" Month="12/2014">29.17</Amount>
                                                <Amount Name="GILBER" Source="Other" Month="12/2014">8.33</Amount>
                                                <Amount Name="GILBER" Source="Goods" Month="01/2015">29.17</Amount>
                                                <Amount Name="GILBER" Source="Other" Month="01/2015">8.33</Amount>
                                    </Summary>
                        </Section>

Open in new window


the current xslt to strip the "/b." and "/b0."

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

Open in new window


this works fine if there are  "/b." and "/b0." , but these re not always there in the xml, the current xslt strps the whole Name if the "/b." and "/b0." are not present , how d i check if these "/b." and "/b0. are present in the specific tags only then strip them  else do nothing.
0
Comment
Question by:sri1209
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 40551656
make a test in the template strip-name

    <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>

Open in new window

0
 

Author Comment

by:sri1209
ID: 40553738
Thank you again Geert Bormans.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40553744
welcome
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
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 …

743 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

13 Experts available now in Live!

Get 1:1 Help Now