Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML XSLT
Hello All
 
I have one issue related to removing tag from xml file while creating it by applying xsl.
 
e.g:
<!--START: operation-->
<xsl:element name="operation">
<xsl:if test="string-length(here value of a1 from another xml) > 0">
    <xsl:element name="a1">
       <xsl:value-of select="normalize-space(here value of a1 from another xml); />
    </xsl:element>
</xsl:if>
<xsl:if test="string-length(here value of a1 from another xml) > 0">
       <xsl:element name="a1">
            <xsl:value-of select="normalize-space(here value of a1 from another xml); />
    </xsl:element>
</xsl:if>
<xsl:if test="string-length(here value of a1 from another xml) > 0">
    <xsl:element name="a1">
       <xsl:value-of select="normalize-space(here value of a1 from another xml); />
    </xsl:element>
</xsl:if>
    <!--END: a1-->
</xsl:element>
<!--END: operation-->
 
There are number of child tags such as a1,b1,c1,d1 with multiple counts[as shown in example a1] whose value is coming from another Parent XML{whose tags and names are different}. The child elements are created only when the value in another XML i.e. Parent XML is not null.
My issue is i want to remove the operation tag if its child node does not exists.
Note: In this i don't want to apply another XSL for removing the operation tag. As i created the sample application by applying another XSL on the output.xml which is working fine.
the code is use to remove the operation tag if child node does not exists is shown below used in another xsl
<xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="operation[not(a1or b1 or c1 or d1)]"/>
 
Is there any way by which i can remove the tag by applying only one XSL.
 
Thanks for helping in advance and waiting for your suggestion.
Posted 30-Jul-12 19:08pm
sagar55910

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this:
 
<xsl:template match="text()">
    <xsl:variable name="Nod" >
       <xsl:value-of select="local-name(..)" />
    </xsl:variable>
    <xsl:variable name="NodValue" >
       <xsl:value-of select="." />
    </xsl:variable>
 <xsl:if test="(contains($Nod,'a1') or contains($Nod,'b1') or contains($Nod,'c1') or contains($Nod,'d1')) and NodValue != ''">
  <xsl:value-of select="." />
</xsl:if>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 405
1 Sergey Alexandrovich Kryukov 272
2 OriginalGriff 165
3 George Jonsson 159
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,860
2 CPallini 5,135
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 20 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100