Xsl: Copy all xml except the parent node, but keep its child element node

I want to copy the whole XML document, but delete the parent node. However, this parent node also has a child that I would like to keep.

The node to delete is <LoginID>, and the child of node 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>
+3
source share
3 answers

This XSL should do the necessary.

<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>
+7
source

from this code, if you want to remove node InqRs, 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 helps you

+2
source
<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>
+1
source

Source: https://habr.com/ru/post/1712830/


All Articles