normalize-xml.xsl 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?xml version='1.0' encoding='UTF-8' ?>
  2. <!--
  3. Normalize an XML document to make it easier to compare whether 2 documents will
  4. be seen as "equal" to an XML processor.
  5. The normalization is similiar, in spirit, to {@link https://www.w3.org/TR/xml-c14n11/ Canonical XML},
  6. but without some aspects of C14N that make the kinds of assertions we need difficult.
  7. For example, the following XML documents will be interpreted the same by an XML processor,
  8. even though a string comparison of them would show differences:
  9. <root xmlns='urn:example'>
  10. <ns0:child xmlns:ns0='urn:another-example'>this is a test</ns0:child>
  11. </root>
  12. <ns0:root xmlns:ns0='urn:example'>
  13. <child xmlns='urn:another-example'>this is a test</child>
  14. </ns0:root>
  15. -->
  16. <xsl:transform
  17. xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  18. version='1.0'
  19. >
  20. <!--
  21. Output UTF-8 XML, no indendation and all CDATA sections replaced with their character content.
  22. -->
  23. <xsl:output
  24. method='xml'
  25. indent='no'
  26. cdata-section-elements=''
  27. encoding='UTF-8' />
  28. <!--
  29. Strip insignificant white space.
  30. -->
  31. <xsl:strip-space elements='*' />
  32. <!--
  33. Noramlize elements by not relying on the prefix used in the input document
  34. and ordering attributes first by namespace-uri and then by local-name.
  35. -->
  36. <xsl:template match='*' priority='10'>
  37. <xsl:element name='{local-name()}' namespace='{namespace-uri()}'>
  38. <xsl:apply-templates select='@*'>
  39. <xsl:sort select='namespace-uri()' />
  40. <xsl:sort select='local-name()' />
  41. </xsl:apply-templates>
  42. <xsl:apply-templates select='node()' />
  43. </xsl:element>
  44. </xsl:template>
  45. <!--
  46. Noramlize attributes by not relying on the prefix used in the input document.
  47. -->
  48. <xsl:template match='@*'>
  49. <xsl:attribute name='{local-name()}' namespace='{namespace-uri()}'>
  50. <xsl:value-of select='.' />
  51. </xsl:attribute>
  52. </xsl:template>
  53. <!--
  54. Strip comments.
  55. -->
  56. <xsl:template match='comment()' priority='10' />
  57. <!--
  58. Pass all other nodes through unchanged.
  59. -->
  60. <xsl:template match='node()'>
  61. <xsl:copy>
  62. <xsl:apply-templates select='node()' />
  63. </xsl:copy>
  64. </xsl:template>
  65. </xsl:transform>