Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello All

I have one issue related to removing tag from xml file while creating it by applying xsl.

e.g:
XML
<!--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

1 solution

Try this:

XML
<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>
 
Share this answer
 

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

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900