Remove all nodes from xml excluding specific nodes using XSLT

Posted by Grinner on Stack Overflow See other posts from Stack Overflow or by Grinner
Published on 2010-05-26T16:20:38Z Indexed on 2010/05/26 16:21 UTC
Read the original article Hit count: 315

Filed under:
|
|
|

Hi, I have a bunch of xml files with a varying amount of data nodes in them and I want to change the files using XSLT to include only specific nodes. Example:

<?xml version="1.0" encoding="UTF-8"?> 
 <SomeName> 
 <identifier> 
    <UID> 1234 </UID> 
 </identifier> 
 <MainNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a0</Subnode1a> 
     </SubNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a1</Subnode1a> 
     </SubNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a2</Subnode1a> 
     </SubNode1> 
  </MainNode1> 

  <MainNode2> 
     <SubNode2> 
        <Subnode2a>DATA2a0</Subnode2a> 
     </SubNode2> 
  </MainNode2> 

  <MainNodeIDONTCARE> 
       <SubnodeWhatever> 
       </SubnodeWhatever> 
  </MainNodeIDONTCARE> 

  <MainNodeuseless> 
       <SubnodeWhatever> 
       </SubnodeWhatever> 
  </MainNodeuseless>

  <MainNodewhatever> 
       <SubnodeWhatever> 
       </SubnodeWhatever> 
  </MainNodewhatever>
</SomeName> 

Now my final XML file should look like:

<?xml version="1.0" encoding="UTF-8"?> 
 <SomeName> 
 <identifier> 
    <UID> 1234 </UID> 
 </identifier> 
 <MainNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a0</Subnode1a> 
     </SubNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a1</Subnode1a> 
     </SubNode1> 
     <SubNode1> 
        <Subnode1a>DATA1a2</Subnode1a> 
     </SubNode1> 
  </MainNode1> 

  <MainNode2> 
     <SubNode2> 
        <Subnode2a>DATA2a0</Subnode2a> 
     </SubNode2> 
  </MainNode2>
</SomeName> 

I've been trying to get it done with XSLT, but I can't seem to get it done.

Thanks for any help.

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xslt