tags:

views:

282

answers:

3

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.

A: 

Here it is

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="xml" indent="yes"/>

  <xsl:template match="SomeName">
    <xsl:copy>
      <xsl:for-each select="identifier|MainNode1|MainNode2">
        <xsl:apply-templates select="." />
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Gart
Does not include identifier, MainNode1, MainNode2 as required by the OP.
Obalix
@Obalix: thanks for the comment, I corrected the error in my own way.Your solution is fine too.
Gart
A: 

This should work:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="SomeName">
    <xsl:copy>
      <xsl:for-each select="identifier|MainNode1|MainNode2">
        <xsl:copy>
          <xsl:apply-templates />
        </xsl:copy>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
Obalix
A: 

Probably the shortest solution is the following:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template
 match="MainNodeIDONTCARE | MainNodeuseless | MainNodewhatever"/>
</xsl:stylesheet>

When this transformation is applied on the provided XML document, the wanted output is produced:

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

Do note the use of the most fundamental XSLT design pattern: using and overriding the identity rule.

Dimitre Novatchev
This solution is good only when you know all the elements you want to discard
Gart
@Gart: Yes, the OP hasn't specifically indicated if he wants all elements discarded except some known elements, or if he wants all elements copied except some known elements.
Dimitre Novatchev