XSLT转换xml文件的案例

前端之家收集整理的这篇文章主要介绍了XSLT转换xml文件的案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

XML文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <doc>
  3. <html>
  4.  
  5. <table a="b"></table>
  6. <table style="a:b;" a="b"></table>
  7. <table style="WIDTH:100px;" a="b"></table>
  8. <table style="WIDTH:100PX;">
  9. <th TEXT-ALIGN='10' a="a">
  10. <td BACKGROUND-COLOR="red" TEXT-ALIGN='20' a='b'>1</td>
  11. </th>
  12. </table>
  13. <span style="TEXT-DECORATION:underline"><a>ABC</a></span>
  14. <span style="text-decoration:underline"><a>ABC</a></span>
  15. <span style="TEXT-DECORATION:underline" a="b"><a>ABC</a></span>
  16. <span style="color:#ff3300">dfafadssfas</span>
  17. <span style="COLOR:#0033ff">dfafadssfas </span>
  18. <span style="color:red"><a>EFG</a></span>
  19. <p style="COLOR:#ff3300">dfafadssfas </p>
  20. <p style="COLOR:#0033ff">dfafadssfas </p>
  21. <p style="align:center">dfafadssfas </p>
  22.  
  23. <ul>
  24. <li>
  25. <p>1</p>
  26. <ul>
  27. <li>
  28. <p><span style="TEXT-DECORATION:underline"><a>ABC</a></span></p>
  29. <ul>
  30. <li>
  31. <p><span style="color:#ff3300"><span style="TEXT-DECORATION:underline"><a>ABC</a></span></span></p>
  32. </li>
  33. </ul>
  34. </li>
  35. </ul>
  36. </li>
  37. <li>1231</li>
  38. </ul>
  39.  
  40. <ul><ul></ul></ul>
  41.  
  42.  
  43. <strong a="1" b="2">a</strong>
  44.  
  45. <h1>a</h1>
  46. <h2>q</h2>
  47. <h3>w</h3>
  48.  
  49. <em>em</em>
  50. </html>
  51. </doc>

结果:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <jpxml>
  3.  
  4.  
  5. <table a="b" />
  6. <table a="b" />
  7. <table width="100px&#10;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;"
  8. a="b" />
  9. <table width="100px&#10;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;">
  10. <th align="10" a="a">
  11. <td bgcolor="red" align="20" a="b">1</td>
  12. </th>
  13. </table>
  14. <u>
  15. <a>ABC</a>
  16. </u>
  17. <u>
  18. <a>ABC</a>
  19. </u>
  20. <u a="b">
  21. <a>ABC</a>
  22. </u>
  23. <red>dfafadssfas</red>
  24. <blue>dfafadssfas </blue>
  25. <span>
  26. <a>EFG</a>
  27. </span>
  28. <red>dfafadssfas </red>
  29. <blue>dfafadssfas </blue>
  30. <p>dfafadssfas </p>
  31.  
  32. <ul1>
  33. <li1>
  34. <p>1</p>
  35. <ul2>
  36. <li2>
  37. <p>
  38. <u>
  39. <a>ABC</a>
  40. </u>
  41. </p>
  42. <ul3>
  43. <li3>
  44. <p>
  45. <red>
  46. <u>
  47. <a>ABC</a>
  48. </u>
  49. </red>
  50. </p>
  51. </li3>
  52. </ul3>
  53. </li2>
  54. </ul2>
  55. </li1>
  56. <li1>1231</li1>
  57. </ul1>
  58.  
  59. <ul1>
  60. <ul2 />
  61. </ul1>
  62.  
  63.  
  64. <b a="1" b="2">a</b>
  65.  
  66. <h1>a</h1>
  67. <h2>q</h2>
  68. <h3>w</h3>
  69.  
  70. <i>em</i>
  71.  
  72. </jpxml>
  73.  


XSL文件内容
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet version="1.0"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4. <xsl:output method="xml" version="1.0" encoding="utf-8"
  5. indent="yes" cdata-section-elements="DataContent " />
  6. <xsl:template match="/">
  7. <jpxml>
  8. <xsl:apply-templates />
  9. </jpxml>
  10. </xsl:template>
  11. <!-- ul,ol的转换 -->
  12. <xsl:template match="ul|ol">
  13. <xsl:choose>
  14. <xsl:when test="number(count(ancestor::ul)) > 0">
  15. <xsl:element name="{concat('ul',number(count(ancestor-or-self::ul)))}">
  16. <xsl:for-each select="@*">
  17. <xsl:copy/>
  18. </xsl:for-each>
  19. <xsl:apply-templates/>
  20. </xsl:element>
  21. </xsl:when>
  22. <xsl:otherwise>
  23. <xsl:element name="ul1">
  24. <xsl:for-each select="@*">
  25. <xsl:copy/>
  26. </xsl:for-each>
  27. <xsl:apply-templates/>
  28. </xsl:element>
  29. </xsl:otherwise>
  30. </xsl:choose>
  31. </xsl:template>
  32.  
  33. <!-- li的转换 -->
  34. <xsl:template match="li">
  35. <xsl:choose>
  36. <xsl:when test="number(count(ancestor::li)) > 0">
  37. <xsl:element name="{concat('li',number(count(ancestor-or-self::li)))}">
  38. <xsl:for-each select="@*">
  39. <xsl:copy/>
  40. </xsl:for-each>
  41. <xsl:apply-templates/>
  42. </xsl:element>
  43. </xsl:when>
  44. <xsl:otherwise>
  45. <xsl:element name="li1">
  46. <xsl:for-each select="@*">
  47. <xsl:copy/>
  48. </xsl:for-each>
  49. <xsl:apply-templates/>
  50. </xsl:element>
  51. </xsl:otherwise>
  52. </xsl:choose>
  53. </xsl:template>
  54. <!-- 表格处理:style含有WIDTH的变成width属性删除style属性 -->
  55. <xsl:template match="table">
  56. <xsl:element name="{name(.)}">
  57. <xsl:choose>
  58. <!-- 判断当前table是否含有style属性值WIDTH -->
  59. <xsl:when test="contains(@style,'WIDTH')">
  60. <xsl:for-each select="@*">
  61. <!-- 判断当前属性是否是style -->
  62. <xsl:choose>
  63. <!-- 当前属性是style -->
  64. <xsl:when test="name(.) = 'style'">
  65. <xsl:attribute name="width">
  66. <!--判断width的属性值是否有px -->
  67. <xsl:choose>
  68. <xsl:when test="contains(substring-before(substring-after(string(.),'WIDTH:'),';'),'px')">
  69. <xsl:value-of select="number(substring-before(substring-after(string(.),'px;'))"/>px
  70. </xsl:when>
  71. <xsl:when test="contains(substring-before(substring-after(string(.),'PX')">
  72. <xsl:value-of select="number(substring-before(substring-after(string(.),'PX;'))"/>px
  73. </xsl:when>
  74. <!-- WITDTH值没有px -->
  75. <xsl:otherwise>
  76. <xsl:value-of select="number(substring-before(substring-after(string(.),';'))"/>
  77. </xsl:otherwise>
  78. </xsl:choose>
  79. </xsl:attribute>
  80. </xsl:when>
  81. <xsl:otherwise>
  82. <!-- 当前属性不是style -->
  83. <xsl:attribute name="{name(.)}">
  84. <xsl:value-of select="."/>
  85. </xsl:attribute>
  86. </xsl:otherwise>
  87. </xsl:choose>
  88. </xsl:for-each>
  89. </xsl:when>
  90. <xsl:otherwise>
  91. <xsl:for-each select="@*">
  92. <xsl:if test="name(.) != 'style'">
  93. <xsl:attribute name="{name(.)}">
  94. <xsl:value-of select="."/>
  95. </xsl:attribute>
  96. </xsl:if>
  97. </xsl:for-each>
  98. </xsl:otherwise>
  99. </xsl:choose>
  100. <xsl:apply-templates/>
  101. </xsl:element>
  102. </xsl:template>
  103. <!-- td处理:将TEXT-ALIGN属性改为align,BACKGROUND-COLOR改为bgcolor,属性值不变 -->
  104. <xsl:template match="td">
  105. <xsl:element name="{name(.)}">
  106. <xsl:for-each select="@*">
  107. <xsl:choose>
  108. <xsl:when test="name(.) = 'TEXT-ALIGN'">
  109. <xsl:attribute name="align">
  110. <xsl:value-of select="."/>
  111. </xsl:attribute>
  112. </xsl:when>
  113. <xsl:when test="name(.) = 'BACKGROUND-COLOR'">
  114. <xsl:attribute name="bgcolor">
  115. <xsl:value-of select="."/>
  116. </xsl:attribute>
  117. </xsl:when>
  118. <xsl:otherwise>
  119. <xsl:copy/>
  120. </xsl:otherwise>
  121. </xsl:choose>
  122. </xsl:for-each>
  123. <xsl:apply-templates/>
  124. </xsl:element>
  125. </xsl:template>
  126. <!-- th处理:将TEXT-ALIGN属性改为align,属性值不变 -->
  127. <xsl:template match="th">
  128. <xsl:element name="{name(.)}">
  129. <xsl:for-each select="@*">
  130. <xsl:choose>
  131. <xsl:when test="name(.) = 'TEXT-ALIGN'">
  132. <xsl:attribute name="align">
  133. <xsl:value-of select="."/>
  134. </xsl:attribute>
  135. </xsl:when>
  136. <xsl:otherwise>
  137. <xsl:copy/>
  138. </xsl:otherwise>
  139. </xsl:choose>
  140. </xsl:for-each>
  141. <xsl:apply-templates/>
  142. </xsl:element>
  143. </xsl:template>
  144. <!-- <span style="TEXT-DECORATION:underline">ABC</span> -->
  145. <!-- <span style="COLOR:#ff3300">dfafadssfas </span> -->
  146. <!-- span标签下划线和两种颜色的转换和去除span标签中的属性和值 -->
  147.  
  148. <xsl:template match="span">
  149. <xsl:choose>
  150. <xsl:when test="@style = 'TEXT-DECORATION:underline' or @style = 'text-decoration:underline'">
  151. <xsl:element name="u">
  152. <xsl:for-each select="@*">
  153. <xsl:if test="name(.) != 'style'">
  154. <xsl:attribute name="{name(.)}">
  155. <xsl:value-of select="."/>
  156. </xsl:attribute>
  157. </xsl:if>
  158. </xsl:for-each>
  159. <xsl:apply-templates/>
  160. </xsl:element>
  161. </xsl:when>
  162. <xsl:when test="@style = 'COLOR:#ff3300' or @style = 'color:#ff3300'">
  163. <xsl:element name="red">
  164. <xsl:apply-templates/>
  165. </xsl:element>
  166. </xsl:when>
  167. <xsl:when test="@style = 'COLOR:#0033ff' or @style = 'color:#0033ff'">
  168. <xsl:element name="blue">
  169. <xsl:apply-templates/>
  170. </xsl:element>
  171. </xsl:when>
  172. <xsl:otherwise>
  173. <xsl:element name="{name(.)}">
  174. <xsl:apply-templates/>
  175. </xsl:element>
  176. </xsl:otherwise>
  177. </xsl:choose>
  178. </xsl:template>
  179. <!-- <p style="COLOR:#ff3300">dfafadssfas </p> -->
  180. <!-- p标签两种颜色和删除属性和值的转换 -->
  181. <xsl:template match="p">
  182. <xsl:choose>
  183. <xsl:when test="@style = 'COLOR:#ff3300' or @style = 'color:#ff3300'">
  184. <xsl:element name="red">
  185. <xsl:apply-templates/>
  186. </xsl:element>
  187. </xsl:when>
  188. <xsl:when test="@style = 'COLOR:#0033ff' or @style = 'color:#0033ff'">
  189. <xsl:element name="blue">
  190. <xsl:apply-templates/>
  191. </xsl:element>
  192. </xsl:when>
  193. <xsl:otherwise>
  194. <xsl:element name="{name(.)}">
  195. <xsl:apply-templates/>
  196. </xsl:element>
  197. </xsl:otherwise>
  198. </xsl:choose>
  199. </xsl:template>
  200.  
  201.  
  202. <xsl:template match="a">
  203. <xsl:element name="{name(.)}">
  204. <xsl:for-each select="@*">
  205. <xsl:copy />
  206. </xsl:for-each>
  207. <xsl:apply-templates />
  208. </xsl:element>
  209.  
  210. </xsl:template>
  211.  
  212. <xsl:template name="strong" match="strong">
  213. <xsl:element name="b">
  214. <xsl:for-each select="@*">
  215. <xsl:copy />
  216. </xsl:for-each>
  217. <xsl:apply-templates/>
  218. </xsl:element>
  219. </xsl:template>
  220. <xsl:template match="em">
  221. <xsl:element name="i">
  222. <xsl:for-each select="@*">
  223. <xsl:copy />
  224. </xsl:for-each>
  225. <xsl:apply-templates/>
  226. </xsl:element>
  227. </xsl:template>
  228.  
  229. <xsl:template match="h1|h2|h3">
  230. <xsl:element name="{name(.)}">
  231. <xsl:for-each select="@*">
  232. <xsl:copy/>
  233. </xsl:for-each>
  234. <xsl:apply-templates/>
  235. </xsl:element>
  236. </xsl:template>
  237.  
  238. </xsl:stylesheet>

Java代码
  1. public static void main(String argv[]){
  2. String src = "E:\\workspace2\\test1\\WebContent\\test2\\a.xml";
  3. String dest = "E:\\workspace2\\test1\\WebContent\\test2\\a1.xml";
  4. String xslt = "E:\\workspace2\\test1\\WebContent\\test2\\a.xsl";
  5. // String dest = "E:\\workspace2\\.Metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\test1\\WEB-INF\\results\\a2.xml";
  6. // String xslt = "E:\\workspace2\\.Metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\test1\\WEB-INF\\templates\\a.xsl";
  7.  
  8. File src2 = new File(src);
  9. File dest2 = new File(dest);
  10. File xslt2 = new File (xslt);
  11.  
  12. Source srcSource = new StreamSource(src2);
  13. Result destResult = new StreamResult(dest2);
  14. Source xsltSource = new StreamSource(xslt2);
  15.  
  16. try{
  17. TransformerFactory transFact = TransformerFactory.newInstance();
  18. Transformer trans = transFact.newTransformer(xsltSource);
  19. trans.transform(srcSource,destResult);
  20. }catch(TransformerConfigurationException e){
  21. e.printStackTrace();
  22. }catch(TransformerFactoryConfigurationError e){
  23. e.printStackTrace();
  24. }catch(TransformerException e){
  25. e.printStackTrace();
  26. }
  27. }

猜你在找的XML相关文章