views:

1496

answers:

3

HI, I'm new with stylesheets and looking for a solution to copy the entire XML document but remove a parent node from an xml document. 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>
     <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>

Thanks

A: 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <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>
Martin v. Löwis
does xsl:copy also copy attributes? I don't have the spec at hand right now...
Boldewyn
No, it doesn't. However, this should be no problem, since in the OP's document, there aren't any attributes.
Martin v. Löwis
Attributes are also copied if you change the template to (sorry, no formatting in comments ...): <xsl:template match="node() | @*"> <xsl:copy><xsl:apply-templates select="node() | @*"/> </xsl:copy> </xsl:template>
mkoeller
+2  A: 

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

chermosillo
Thanks for the help.
A: 

Thanks for the quick replies... I'm been playing around with both solutions. What would you do if i had the following data and i wanted to now add the parent node LoginID back

<InqRs> 
   <PAN>4506445</PAN> 
   <RqUID>93</RqUID> 
   <Dt>90703195116</Dt> 
   <CaptureDate>704</CaptureDate> 
</InqRs>

<-- Wanted results -->

<InqRs> 
    **<LoginID>** 
        <PAN> 4506445 </PAN> 
    **</LoginID>** 
    <RqUID>93</RqUID> 
    <Dt>90703195116</Dt> 
    <CaptureDate>704</CaptureDate> 
 </InqRs>

Thanks

I just edited my answer to match your expectations, so hope that helps you
chermosillo
Additional question. Please place this kind of content into an edit of the original question, or ask a completely separate new question. Reason: The rating mechanism reorders answers.
mkoeller