Advertisement
Guest User

xsl_concise.xsl

a guest
Sep 20th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 3.45 KB | None | 0 0
  1. <!--
  2.    When a file is transformed using this stylesheet the output will be
  3.    formatted as follows:
  4.  
  5.    ** Pass #1 **
  6.    1.)  Elements named "info" will be removed
  7.    2.)  Attributes named "file_line_nr" or "file_name" will be removed
  8.    3.)  Comments will be removed
  9.    4.)  Processing instructions will be removed
  10.    5.)  XML declaration will be removed
  11.    6.)  Extra whitespace will be removed
  12.    7.)  Empty attributes will be removed
  13.    8.)  Elements which have no attributes, child elements, or text will be removed
  14.    9.)  All elements will be sorted by name recursively
  15.    10.) All attributes will be sorted by name
  16.  
  17.    **Pass #2 **
  18.    11.)  Duplicate sibling elements will be removed
  19. -->
  20. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  21.  
  22.     <!-- Set output options -->
  23.     <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
  24.     <xsl:strip-space elements="*"/>
  25.  
  26. <!--****************************************************************************
  27.  
  28. Mode templates
  29.  
  30. *****************************************************************************-->
  31.  
  32.     <xsl:template match="/">
  33.         <!-- First pass with default mode templates -->
  34.         <xsl:variable name="pass1">
  35.             <xsl:apply-templates/>
  36.         </xsl:variable>
  37.  
  38.         <!-- Second pass with custom de-dup templates -->
  39.         <xsl:apply-templates mode="de-dup" select="$pass1"/>
  40.     </xsl:template>
  41.  
  42. <!--****************************************************************************
  43.  
  44. Pass #1 / default mode templates
  45.  
  46. *****************************************************************************-->
  47.  
  48.     <!-- Match any attribute -->
  49.     <xsl:template match="@*">
  50.         <xsl:copy/>
  51.     </xsl:template>
  52.  
  53.     <!-- Match any element -->
  54.     <xsl:template match="*">
  55.         <xsl:copy>
  56.             <xsl:apply-templates select="@*">
  57.                 <xsl:sort select="local-name()"/>
  58.             </xsl:apply-templates>
  59.             <xsl:for-each-group group-adjacent="boolean(self::*)" select="node() except (processing-instruction(), comment())">
  60.                 <xsl:choose>
  61.                     <xsl:when test="current-grouping-key()">
  62.                         <xsl:apply-templates select="current-group()">
  63.                             <xsl:sort select="local-name()"/>
  64.                             <xsl:sort select="."/>
  65.                         </xsl:apply-templates>
  66.                     </xsl:when>
  67.                     <xsl:otherwise>
  68.                         <xsl:apply-templates select="current-group()"/>
  69.                     </xsl:otherwise>
  70.                 </xsl:choose>
  71.             </xsl:for-each-group>
  72.         </xsl:copy>
  73.     </xsl:template>
  74.  
  75.     <!-- Elements/attributes to ignore -->
  76.     <xsl:template match="@*[normalize-space()='']|info|@file_line_nr|@file_name|*[not(@*|node())]"/>
  77.  
  78. <!--****************************************************************************
  79.  
  80. Pass #2 / de-dup mode templates
  81.  
  82. *****************************************************************************-->
  83.  
  84.     <!-- Match any element -->
  85.     <xsl:template match="@*|node()" mode="de-dup">
  86.         <xsl:copy>
  87.             <xsl:apply-templates mode="de-dup" select="@*|node()"/>
  88.         </xsl:copy>
  89.     </xsl:template>
  90.  
  91.     <!-- Ignore elements which are deep-equal to a preceding sibling element -->
  92.     <xsl:template match="*[some $ps in preceding-sibling::* satisfies deep-equal(., $ps)]" mode="de-dup"/>
  93.  
  94. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement