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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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 …
Simple Linear Regression

617 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