Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 22nd, 2012  |  syntax: None  |  size: 2.48 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Need to remove xmi node if there are just 1 child
  2. <xmi:XMI attribute="2" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A">
  3. <namespace:node attribute2="att">
  4. ...
  5. </namespace:node>
  6. </xmi:XMI>
  7.        
  8. <namespace:node attribute2="att" attribute="2" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A">
  9. ...
  10. </namespace:node>
  11.        
  12. <xmi:XMI attribute="2" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A">
  13. <namespace:node attribute2="att">
  14. ...
  15. </namespace:node>
  16. <otherNode/>
  17. </xmi:XMI>
  18.        
  19. <xsl:stylesheet version="1.0"
  20.  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  21.  xmlns:xmi="http://www.omg.org/spec/XMI/20110701">
  22.  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  23.  <xsl:strip-space elements="*"/>
  24.  
  25.  <xsl:template match="node()|@*">
  26.      <xsl:copy>
  27.        <xsl:apply-templates select="node()|@*"/>
  28.      </xsl:copy>
  29.  </xsl:template>
  30.  
  31.  <xsl:template match="xmi:XMI[not(*[2])]">
  32.   <xsl:apply-templates/>
  33.  </xsl:template>
  34.  
  35.  <xsl:template match="xmi:XMI[not(*[2])]/*">
  36.      <xsl:copy>
  37.        <xsl:apply-templates select="node()|@* | ../@*"/>
  38.      </xsl:copy>
  39.  </xsl:template>
  40. </xsl:stylesheet>
  41.        
  42. <xmi:XMI attribute="2"
  43.            xmlns:xmi="http://www.omg.org/spec/XMI/20110701"
  44.            xmlns:a="A" >
  45.         <namespace:node attribute2="att" xmlns:namespace="some:namespace">
  46.           ...
  47.         </namespace:node>
  48.         <otherNode/>
  49. </xmi:XMI>
  50.        
  51. <xmi:XMI xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A" attribute="2">
  52.    <namespace:node xmlns:namespace="some:namespace" attribute2="att">
  53.           ...
  54.         </namespace:node>
  55.    <otherNode/>
  56. </xmi:XMI>
  57.        
  58. <xmi:XMI attribute="2"
  59.            xmlns:xmi="http://www.omg.org/spec/XMI/20110701"
  60.            xmlns:a="A" >
  61.         <namespace:node attribute2="att" xmlns:namespace="some:namespace">
  62.           ...
  63.         </namespace:node>
  64. </xmi:XMI>
  65.        
  66. <namespace:node xmlns:namespace="some:namespace"    
  67.  xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A"
  68.  attribute="2" attribute2="att">
  69.           ...
  70. </namespace:node>
  71.        
  72. <xsl:stylesheet
  73.   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  74.   version="2.0"
  75.   xmlns:xmi="http://www.omg.org/spec/XMI/20110701">
  76.  
  77.  
  78. <xsl:template match="@* | node()">
  79.   <xsl:copy>
  80.     <xsl:apply-templates select="@* , node()"/>
  81.   </xsl:copy>
  82. </xsl:template>
  83.  
  84. <xsl:template match="xmi:XMI[not(*[2])">
  85.   <xsl:apply-templates/>
  86. </xsl:template>
  87.  
  88. <xsl:template match="xmi:XMI[not(*[2])]/*">
  89.   <xsl:copy>
  90.     <xsl:apply-templates select="../@*, @*, node()"/>
  91.   </xsl:copy>
  92. </xsl:template>
  93.  
  94. </xsl:stylesheet>