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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

776 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