Advertisement
Guest User

Untitled

a guest
Dec 21st, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. <persons>
  2. <person>
  3. <name>Tom</name>
  4. <age>20</age>
  5. <mails>
  6. <mail>x@test.com</mail>
  7. <mail>y@test.com</mail>
  8. </mails>
  9. </person>
  10. <person>
  11. <name>Tom</name>
  12. <age>20</age>
  13. <mails>
  14. <mail>y@test.com</mail>
  15. <mail>z@test.com</mail>
  16. </mails>
  17. </person>
  18. </persons>
  19.  
  20. <persons>
  21. <person>
  22. <name>Tom</name>
  23. <age>20</age>
  24. <mails>
  25. <mail>x@test.com</mail>
  26. <mail>y@test.com</mail>
  27. <mail>z@test.com</mail>
  28. </mails>
  29. </person>
  30. </persons>
  31.  
  32. <xsl:stylesheet version="1.0"
  33. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  34. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  35. <xsl:strip-space elements="*"/>
  36.  
  37. <xsl:key name="kPersByNameAndAge" match="person"
  38. use="concat(name, '+', age)"/>
  39.  
  40. <xsl:key name="kmailByNameAndAge" match="mail"
  41. use="concat(../../name, '+', ../../age)"/>
  42.  
  43. <xsl:key name="kmailByNameAndAgeAndVal" match="mail"
  44. use="concat(../../name, '+', ../../age, '+', .)"/>
  45.  
  46. <xsl:template match="node()|@*">
  47. <xsl:copy>
  48. <xsl:apply-templates select="node()|@*"/>
  49. </xsl:copy>
  50. </xsl:template>
  51.  
  52. <xsl:template match="/*">
  53. <persons>
  54. <xsl:apply-templates select=
  55. "person[generate-id()
  56. =
  57. generate-id(key('kPersByNameAndAge',
  58. concat(name, '+', age)
  59. )
  60. [1]
  61. )
  62. ]
  63. "/>
  64. </persons>
  65. </xsl:template>
  66.  
  67. <xsl:template match="mails">
  68. <mails>
  69. <xsl:apply-templates select=
  70. "key('kmailByNameAndAge', concat(../name, '+', ../age))
  71. [generate-id()
  72. =
  73. generate-id(key('kmailByNameAndAgeAndVal',
  74. concat(../../name, '+', ../../age, '+', .)
  75. )
  76. [1]
  77. )
  78. ]
  79. "/>
  80. </mails>
  81. </xsl:template>
  82. </xsl:stylesheet>
  83.  
  84. <persons>
  85. <person>
  86. <name>Tom</name>
  87. <age>20</age>
  88. <mails>
  89. <mail>x@test.com</mail>
  90. <mail>y@test.com</mail>
  91. </mails>
  92. </person>
  93. <person>
  94. <name>Tom</name>
  95. <age>20</age>
  96. <mails>
  97. <mail>y@test.com</mail>
  98. <mail>z@test.com</mail>
  99. </mails>
  100. </person>
  101. </persons>
  102.  
  103. <persons>
  104. <person>
  105. <name>Tom</name>
  106. <age>20</age>
  107. <mails>
  108. <mail>x@test.com</mail>
  109. <mail>y@test.com</mail>
  110. <mail>z@test.com</mail>
  111. </mails>
  112. </person>
  113. </persons>
  114.  
  115. <xsl:stylesheet version="2.0"
  116. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  117. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  118. <xsl:strip-space elements="*"/>
  119.  
  120. <xsl:key name="kmailByNameAndAge" match="mail"
  121. use="concat(../../name, '+', ../../age)"/>
  122.  
  123. <xsl:template match="node()|@*">
  124. <xsl:copy>
  125. <xsl:apply-templates select="node()|@*"/>
  126. </xsl:copy>
  127. </xsl:template>
  128.  
  129. <xsl:template match="/*">
  130. <persons>
  131. <xsl:for-each-group select="person" group-by="concat(name, '+', age)">
  132. <xsl:apply-templates select="."/>
  133. </xsl:for-each-group>
  134. </persons>
  135. </xsl:template>
  136.  
  137. <xsl:template match="mails">
  138. <mails>
  139. <xsl:for-each-group select=
  140. "key('kmailByNameAndAge', concat(../name, '+', ../age))"
  141. group-by="concat(../../name, '+', ../../age, '+', .)"
  142. >
  143. <xsl:apply-templates select="."/>
  144. </xsl:for-each-group>
  145. </mails>
  146. </xsl:template>
  147. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement