Skip nodes using XSLT

Is it possible to skip nodes when processing an xml file. for example i have the following xml code

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

and I want it to look like

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

I'm currently 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 creates what I want, but because

<xsl:apply-template/>

handles all the node. while I want it to skip caseid and serid processing all together. This also applies to other nodes that will not be available in the new XML structure. So, how can I skip nodes that I don’t want to process with xslt.

Regards, Ehsan

+5
source share
3 answers

You can use empty templates to suppress the output of certain nodes in your source 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>
+8
source

, - , caseid, , :

<xsl:template match="caseid"/>

XSL, caseid .

xsl:apply-elements , caseid, :

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

, , !

+4

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

, , node. "mycase".

+2

All Articles