Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

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>
like image 582
user139873 Avatar asked Dec 22 '22 10:12

user139873


2 Answers

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>
like image 145
vinayakchalse Avatar answered Jan 05 '23 22:01

vinayakchalse


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

like image 35
Cesar Hermosillo Avatar answered Jan 05 '23 22:01

Cesar Hermosillo