remove text from a node in input XML

1k views Asked by At

i am trying to copy entire input xml in a string for further processing and also i have a requirement to remove text from a particular node (plancode) before copying in the variable. May I know how can i achieve this using xslt

INPUT XML:

<CallMember>
    <PlanD>
      <abcpr>you</abcpr>
      <Desd>Protection</Desd>
      <plancode>76789</plancode>
      <plaDesc>goody</plaDesc>
     </PlanD>
   <fType>ONLINE</fType>
</CallMember>

XSLT i am trying :

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt" xmlns:func="http://exslt.org/functions" xmlns:dp="http://www.datapower.com/extensions" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:tglfn="http://test.com/tgl" xmlns:date="http://exslt.org/dates-and-times" exclude-result-prefixes="#all" extension-element-prefixes="dp regexp">

    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
    <xsl:template match="/">

            <xsl:variable name="InputRequest">
                <xsl:copy-of select="."/>                   
            </xsl:variable>

            <xsl:variable name="modifiedRequest">
                <xsl:copy-of select="."/>   
                <plancode></plancode>
            </xsl:variable>
</xsl:template>

OUTput i am expecting in the modifiedRequest variable:

<CallMember>
    <PlanD>
      <abcpr>you</abcpr>
      <Desd>Protection</Desd>
      <plancode></plancode>  <!-- this value needs to get emptied -->
      <plaDesc>goody</plaDesc>
     </PlanD>
   <fType>ONLINE</fType>
</CallMember>
1

There are 1 answers

0
zx485 On BEST ANSWER

Use an identity template in combination with an (nearly) empty template for filtering and apply-templates to it:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xalan="http://xml.apache.org/xslt" 
    xmlns:func="http://exslt.org/functions" 
    xmlns:dp="http://www.datapower.com/extensions" 
    xmlns:regexp="http://exslt.org/regular-expressions" 
    xmlns:tglfn="http://test.com/tgl" 
    xmlns:date="http://exslt.org/dates-and-times" 
    exclude-result-prefixes="#all" extension-element-prefixes="dp regexp">    
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>

    <!-- identity template -->
    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
      </xsl:copy>
    </xsl:template>

    <xsl:template match="/">

        <xsl:variable name="InputRequest">
            <xsl:copy-of select="."/>
        </xsl:variable>

        <!-- copies subtree except matching empty template -->
        <xsl:variable name="modifiedRequest">
          <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
          </xsl:copy>
        </xsl:variable>

    </xsl:template>

    <!-- (nearly) empty template copies only element without content -->
    <xsl:template match="plancode">
      <xsl:copy />
    </xsl:template>
</xsl:stylesheet>