tags:

views:

30

answers:

3

Hello, 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"&gt;

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

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.

regards, ehsan

A: 

use <xsl:apply-templates select="mycase"/> instead of <xsl:apply-templates />

Basically, what you need to do is apply the templates on just a specific node set. In your case just the 'mycase' nodes.

Workshop Alex
+1  A: 

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"&gt;
  <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>
0xA3
+1 Good answer.
Alejandro
A: 

Perhaps the easiest way to do this, is to add a template that matches caseid, and have it do nothing, like this:

<xsl:template match="caseid"/>

By adding this to your existing XSL, the caseid element is ignored.

Another solution would be to only use xsl:apply-elements on elements not matching the name caseid, i.e.:

 <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 select="*[name() != 'caseid']"/>
        </xsl:element>
 </xsl:template>

Hope this helps, good luck!

vetler