Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

skip over nodes with XSLT

Tags:

xml

Is it possible to skip over nodes while processing a xml file? For example: say I have the following xml code:

<mycase desc="">
  <caseid> id_1234 </caseid>
  <serid ref=""/>    
  ......
  ......
  ......  
</mycase>

and I want to make it look like this:

<mycase desc="" caseid="id_1234">
 .....
 .....
</mycase>

Currently I'm doing this:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs xdt err fn"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:fn="http://www.w3.org/2005/xpath-functions"
            xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
            xmlns:err="http://www.w3.org/2005/xqt-errors">

          <xsl:output method="xml" indent="yes"/>
          <xsl:template match="/">
            <xsl:apply-templates/> 
          </xsl:template>

         <xsl:template match="mycase">          
            <xsl:element name="mycase">
               <xsl:attribute name="desc"/>
               <xsl:attribute name="caseid">
                 <xsl:value-of select="caseid"/>
               </xsl:attribute>
              <xsl:apply-templates/>
            </xsl:element>
         </xsl:template>
         ......
         ......

This does create what I want it to but because of <xsl:apply-templates/> it processes all the node. while I want it to skip processing caseid and serid all together. This also applys for other nodes, which will not be available in the new XML structure. So how can I skip the nodes that I don't want to process using xslt.

like image 669
Goozo Avatar asked Oct 12 '10 11:10

Goozo


1 Answers

You can use empty templates to suppress output of certain nodes in your input document:

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

  <xsl:template match="mycase">
    <mycase caseid="{caseid}">
      <xsl:apply-templates select="@*|node()"/>
    </mycase>
  </xsl:template>

  <xsl:template match="caseid|serid"/>

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

</xsl:stylesheet>
like image 100
Dirk Vollmar Avatar answered Nov 14 '22 22:11

Dirk Vollmar