Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
359 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 2000 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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Six Sigma Control Plans
Introduction to Processes

670 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