user139873
user139873

Reputation: 51

xsl: Copy the entire xml except a parent node but keep its child node

I want to copy the entire XML document but remove a parent node. However this parent node also has a child that I'd like to keep.

The node to remove is <LoginID> and the child node to keep is <PAN>.

<InqRs>
    <LoginID>                <!-- remove -->
        <PAN>4506445</PAN>   <!--  keep  -->
    </LoginID>
    <RqUID>93</RqUID>
    <Dt>90703195116</Dt>
    <CaptureDate>704</CaptureDate>
    <ApprovalCode>934999</ApprovalCode>
    <StatusCode>000</StatusCode>
    <List>
        <Count>9</Count>
        <AddDataFlag>N</AddDataFlag>
        <Use>C</Use>
        <DetRec>
            <ID>007237048637</ID>
            <Type1>62</Type1>
            <Qual />
            <ID>0010</ID>
            <Status>1</Status>
            <InqFlag>Y</InqFlag>
        </DetRec>
    </List>
</InqRs>

Upvotes: 4

Views: 16766

Answers (3)

vinayakchalse
vinayakchalse

Reputation: 81

This XSL should do the needful.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
<xsl:template match="*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>   
<xsl:template match="InqRs/LoginID">
      <xsl:copy-of select="@*|node()" />    
  </xsl:template>
</xsl:stylesheet>

Upvotes: 8

Cesar Hermosillo
Cesar Hermosillo

Reputation: 942

from that code if you want to remove the node InqRs just apply the following xsl :

<xsl:output method="xml"/>
<xsl:template match="node()">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="PAN">
    <LoginID>
           <xsl:copy-of select="."/>
    </LoginID>
</xsl:template>

you will get something like this

<InqRs>
    <LoginID> 
        <PAN> 4506445 </PAN>           
    </LoginID>
    <RqUID>93</RqUID>
    <Dt>90703195116</Dt>
    <CaptureDate>704</CaptureDate>
    <ApprovalCode>934999</ApprovalCode>
    <StatusCode>000</StatusCode>
    <List> 
         <Count>9</Count> 
         <AddDataFlag>N</AddDataFlag> 
         <Use>C</Use> 
         <DetRec> 
             <ID>007237048637</ID> 
             <Type1>62</Type1>
             <Qual/> 
             <ID>0010</ID> 
             <Status>1</Status> 
             <InqFlag>Y</InqFlag> 
         </DetRec> 
    </List>
<InqRs>

I hope this help you

Upvotes: 2

Martin v. L&#246;wis
Martin v. L&#246;wis

Reputation: 127547

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="LoginID">
    <xsl:apply-templates select="PAN"/>
  </xsl:template>
  <xsl:template match="*">
   <xsl:copy><xsl:apply-templates/></xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions