Advertisement
Guest User

xsl_multi_pass.xsl

a guest
Sep 20th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 4.49 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 / ignore **
  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.  
  15.    ** Pass #2 / sortAttributes **
  16.    9.) All attributes will be sorted by name
  17.  
  18.    ** Pass #3 / sortElements **
  19.    10.)  All elements will be sorted by name recursively
  20.  
  21.    ** Pass #4 / deDup **
  22.    11.)  Duplicate sibling elements will be removed
  23. -->
  24. <xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  25.  
  26.     <!-- Set output options -->
  27.     <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
  28.     <xsl:strip-space elements="*"/>
  29.  
  30. <!--****************************************************************************
  31.  
  32. Mode templates
  33.  
  34. *****************************************************************************-->
  35.  
  36.     <xsl:template match="/">
  37.         <!-- First pass with ignore mode templates -->
  38.         <xsl:variable name="ignoreRslt">
  39.             <xsl:apply-templates mode="ignore"/>
  40.         </xsl:variable>
  41.  
  42.         <!-- Second pass with sortAttributes mode templates -->
  43.         <xsl:variable name="sortAttributesRslt">
  44.             <xsl:apply-templates mode="sortAttributes" select="$ignoreRslt"/>
  45.         </xsl:variable>
  46.  
  47.         <!-- Third pass with sortElements mode templates -->
  48.         <xsl:variable name="sortElementsRslt">
  49.             <xsl:apply-templates mode="sortElements" select="$sortAttributesRslt"/>
  50.         </xsl:variable>
  51.  
  52.         <!-- Fourth pass with deDup mode templates -->
  53.         <xsl:apply-templates mode="deDup" select="$sortElementsRslt"/>
  54.     </xsl:template>
  55.  
  56. <!--****************************************************************************
  57.  
  58. Pass #1 / ignore mode templates
  59.  
  60. *****************************************************************************-->
  61.  
  62.     <!-- Elements/attributes to ignore -->
  63.     <xsl:template match="@*[normalize-space()='']|info|@file_line_nr|@file_name|*[not(@*|node())]" mode="ignore"/>
  64.  
  65.     <!-- Match any attribute or element -->
  66.     <xsl:template match="@*|*" mode="ignore">
  67.         <xsl:copy>
  68.             <xsl:apply-templates mode="ignore" select="@*"/>
  69.             <xsl:apply-templates mode="ignore"/>
  70.         </xsl:copy>
  71.     </xsl:template>
  72.  
  73. <!--****************************************************************************
  74.  
  75. Pass #2 / sortAttributes mode templates
  76.  
  77. *****************************************************************************-->
  78.  
  79.     <!-- Match any attribute or element -->
  80.     <xsl:template match="@*|*" mode="sortAttributes">
  81.         <xsl:copy>
  82.             <xsl:apply-templates mode="sortAttributes" select="@*">
  83.                 <xsl:sort select="name()"/>
  84.             </xsl:apply-templates>
  85.             <xsl:apply-templates mode="sortAttributes"/>
  86.         </xsl:copy>
  87.     </xsl:template>
  88.  
  89. <!--****************************************************************************
  90.  
  91. Pass #3 / sortElements mode templates
  92.  
  93. *****************************************************************************-->
  94.  
  95.     <!-- Match any attribute or element -->
  96.     <xsl:template match="@*|*" mode="sortElements">
  97.         <xsl:copy>
  98.             <xsl:apply-templates mode="sortElements" select="@*"/>
  99.             <xsl:apply-templates mode="sortElements">
  100.                 <xsl:sort select="name()"/>
  101.                 <xsl:sort select="."/>
  102.                 <!-- <xsl:sort select="string-length(.)"/> -->
  103.             </xsl:apply-templates>
  104.         </xsl:copy>
  105.     </xsl:template>
  106.  
  107. <!--****************************************************************************
  108.  
  109. Pass #4 / deDup mode templates
  110.  
  111. *****************************************************************************-->
  112.  
  113.     <!-- Match any element -->
  114.     <xsl:template match="@*|node()" mode="deDup">
  115.         <xsl:copy>
  116.             <xsl:apply-templates mode="deDup" select="@*|node()"/>
  117.         </xsl:copy>
  118.     </xsl:template>
  119.  
  120.     <!-- Ignore elements which are deep-equal to a preceding sibling element -->
  121.     <xsl:template match="*[some $ps in preceding-sibling::* satisfies deep-equal(., $ps)]" mode="deDup"/>
  122.  
  123. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement