Advertisement
Guest User

xsl_2.0_full_document_sorter.xsl

a guest
Sep 23rd, 2013
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 5.54 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 & #4 / sortElements **
  19.    10.)  All elements will be sorted by name and their contents recursively
  20.  
  21.    ** Pass #5 / deDup **
  22.    11.)  Duplicate sibling elements will be removed
  23. -->
  24. <xsl:stylesheet version="2.0" xmlns:custom="custom:custom" 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="sortElementsRslt1">
  49.             <xsl:apply-templates mode="sortElements" select="$sortAttributesRslt"/>
  50.         </xsl:variable>
  51.  
  52.         <!-- Fourth pass with sortElements mode templates -->
  53.         <xsl:variable name="sortElementsRslt2">
  54.             <xsl:apply-templates mode="sortElements" select="$sortElementsRslt1"/>
  55.         </xsl:variable>
  56.  
  57.         <!-- Fifth pass with deDup mode templates -->
  58.         <xsl:apply-templates mode="deDup" select="$sortElementsRslt2"/>
  59.     </xsl:template>
  60.  
  61. <!--****************************************************************************
  62.  
  63. Pass #1 / ignore mode templates
  64.  
  65. *****************************************************************************-->
  66.  
  67.     <!-- Elements/attributes to ignore -->
  68.     <xsl:template match="@*[normalize-space()='']|info|@file_line_nr|@file_name|*[not(@*|node())]" mode="ignore"/>
  69.  
  70.     <!-- Match any attribute or element -->
  71.     <xsl:template match="@*|*" mode="ignore">
  72.         <xsl:copy>
  73.             <xsl:apply-templates mode="ignore" select="@*"/>
  74.             <xsl:apply-templates mode="ignore"/>
  75.         </xsl:copy>
  76.     </xsl:template>
  77.  
  78. <!--****************************************************************************
  79.  
  80. Pass #2 / sortAttributes mode templates
  81.  
  82. *****************************************************************************-->
  83.  
  84.     <!-- Match any attribute or element -->
  85.     <xsl:template match="@*|*" mode="sortAttributes">
  86.         <xsl:copy>
  87.             <xsl:apply-templates mode="sortAttributes" select="@*">
  88.                 <xsl:sort select="name()"/>
  89.             </xsl:apply-templates>
  90.             <xsl:apply-templates mode="sortAttributes"/>
  91.         </xsl:copy>
  92.     </xsl:template>
  93.  
  94. <!--****************************************************************************
  95.  
  96. Pass #3 & #4 / sortElements mode templates
  97.  
  98. *****************************************************************************-->
  99.  
  100.     <!-- Match any attribute or element -->
  101.     <xsl:template match="@*|*" mode="sortElements">
  102.         <xsl:copy>
  103.             <xsl:apply-templates mode="sortElements" select="@*"/>
  104.             <xsl:apply-templates mode="sortElements">
  105.                 <xsl:sort select="name()"/>
  106.                 <xsl:sort select="custom:sortElementsByAttrNameAndVal(.)"/>
  107.                 <xsl:sort select="."/>
  108.             </xsl:apply-templates>
  109.         </xsl:copy>
  110.     </xsl:template>
  111.  
  112. <!--****************************************************************************
  113.  
  114. Pass #5 / deDup mode templates
  115.  
  116. *****************************************************************************-->
  117.  
  118.     <!-- Match any element -->
  119.     <xsl:template match="@*|node()" mode="deDup">
  120.         <xsl:copy>
  121.             <xsl:apply-templates mode="deDup" select="@*|node()"/>
  122.         </xsl:copy>
  123.     </xsl:template>
  124.  
  125.     <!-- Ignore elements which are deep-equal to a preceding sibling element -->
  126.     <xsl:template match="*[some $ps in preceding-sibling::* satisfies deep-equal(.,$ps)]" mode="deDup"/>
  127.  
  128. <!--****************************************************************************
  129.  
  130. sortElementsByAttrNameAndVal
  131.  
  132. *****************************************************************************-->
  133.  
  134.     <!-- Function to sort elements by attribute name and value -->
  135.     <xsl:function name="custom:sortElementsByAttrNameAndVal" as="xs:string">
  136.         <xsl:param name="aNode" as="node()"/>
  137.         <xsl:variable name="sequenceFragments">
  138.             <xsl:for-each select="$aNode/@*">
  139.                 <xsl:sort select="name()"/>
  140.                 <xsl:value-of select="concat(name(),'+',.)"/>
  141.             </xsl:for-each>
  142.             <xsl:text>|</xsl:text>
  143.         </xsl:variable>
  144.         <xsl:sequence select="string-join($sequenceFragments,'')"/>
  145.     </xsl:function>
  146.  
  147. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement