wordml2ooo.xsl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--***********************************************************
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. ***********************************************************-->
  22. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" exclude-result-prefixes="w wx aml o dt v" xmlns:fla="urn:experimental:fla">
  23. <xsl:output method="xml" indent="no" encoding="UTF-8" version="1.0"/>
  24. <xsl:include href="../../common/measure_conversion.xsl"/>
  25. <xsl:include href="../common/ms2ooo_docpr.xsl"/>
  26. <xsl:include href="wordml2ooo_text.xsl"/>
  27. <xsl:include href="wordml2ooo_settings.xsl"/>
  28. <xsl:include href="wordml2ooo_table.xsl"/>
  29. <xsl:include href="wordml2ooo_page.xsl"/>
  30. <xsl:include href="wordml2ooo_list.xsl"/>
  31. <xsl:include href="wordml2ooo_draw.xsl"/>
  32. <xsl:include href="wordml2ooo_field.xsl"/>
  33. <xsl:include href="wordml2ooo_props.xsl"/>
  34. <xsl:key name="paragraph-style" match="w:style[@w:type = 'paragraph']" use="@w:styleId"/>
  35. <xsl:key name="heading-style" match="w:style[@w:type = 'paragraph' and w:pPr/w:outlineLvl]" use="@w:styleId"/>
  36. <xsl:variable name="preserve-alien-markup">no</xsl:variable>
  37. <xsl:variable name="native-namespace-prefixes">,w,o,v,wx,aml,w10,dt,</xsl:variable>
  38. <xsl:variable name="to-dispatch-elements">,wx:sect,wx:sub-section,w:p,w:tbl,w:sectPr,w:r,w:fldSimple,w:hlink,w:t,w:pict,w:br,w:instrText,w:fldChar,w:tab,w:footnote,w:endnote,aml:annotation,w:hlink,w:footnote,w:endnote,w:tblGrid,w:tr,w:tc,wx:pBdrGroup,</xsl:variable>
  39. <xsl:template match="/">
  40. <xsl:apply-templates select="w:wordDocument"/>
  41. </xsl:template>
  42. <xsl:template match="*" mode="dispatch">
  43. <xsl:choose>
  44. <xsl:when test="not(contains($native-namespace-prefixes, concat(',', substring-before(name(), ':'), ',')))">
  45. <!-- if alien namespace dispatch -->
  46. <xsl:choose>
  47. <xsl:when test="$preserve-alien-markup = 'yes'">
  48. <xsl:copy>
  49. <xsl:copy-of select="@*"/>
  50. <xsl:apply-templates mode="dispatch"/>
  51. </xsl:copy>
  52. </xsl:when>
  53. <xsl:otherwise>
  54. <xsl:apply-templates mode="dispatch"/>
  55. </xsl:otherwise>
  56. </xsl:choose>
  57. </xsl:when>
  58. <xsl:when test="contains($to-dispatch-elements, concat(',',name(),','))">
  59. <xsl:apply-templates select="current()"/>
  60. </xsl:when>
  61. </xsl:choose>
  62. </xsl:template>
  63. <xsl:template match="w:wordDocument">
  64. <office:document office:mimetype="application/vnd.oasis.opendocument.text" office:version="1.0">
  65. <fla:fla.activate/>
  66. <xsl:apply-templates select="o:DocumentProperties"/>
  67. <xsl:apply-templates select="w:docOleData" mode="init"/>
  68. <xsl:apply-templates select="w:docPr"/>
  69. <xsl:apply-templates select="w:fonts"/>
  70. <xsl:apply-templates select="w:styles"/>
  71. <xsl:apply-templates select="w:body"/>
  72. <xsl:apply-templates select="w:docOleData" mode="exit"/>
  73. </office:document>
  74. </xsl:template>
  75. <xsl:template match="w:fonts">
  76. <xsl:element name="office:font-face-decls">
  77. <!-- MS Word's default font declaration, added for Writer automatically. glu -->
  78. <style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="roman" style:font-pitch="variable"/>
  79. <style:font-face style:name="Times New Roman" svg:font-family="'Times New Roman'" style:font-family-generic="roman" style:font-pitch="variable"/>
  80. <style:font-face style:name="Symbol" svg:font-family="Symbol" style:font-family-generic="roman" style:font-pitch="variable" style:font-charset="x-symbol"/>
  81. <style:font-face style:name="Courier New" svg:font-family="'Courier New'" style:font-family-generic="modern" style:font-pitch="fixed"/>
  82. <xsl:if test="not(w:font[@w:name='StarSymbol'])">
  83. <style:font-face style:name="StarSymbol" svg:font-family="StarSymbol" style:font-charset="x-symbol"/>
  84. </xsl:if>
  85. <xsl:for-each select="w:font">
  86. <xsl:element name="style:font-face">
  87. <xsl:attribute name="style:name">
  88. <xsl:value-of select="@w:name"/>
  89. </xsl:attribute>
  90. <xsl:attribute name="svg:font-family">
  91. <xsl:value-of select="@w:name"/>
  92. </xsl:attribute>
  93. <!-- added by glu, for process special fonts e.g. Marlett, -->
  94. <xsl:if test="w:charset/@w:val = '02'">
  95. <xsl:attribute name="style:font-charset">x-symbol</xsl:attribute>
  96. </xsl:if>
  97. <xsl:if test="w:family">
  98. <xsl:choose>
  99. <xsl:when test="w:family/@w:val = 'Swiss'">
  100. <xsl:attribute name="style:font-family-generic">swiss</xsl:attribute>
  101. </xsl:when>
  102. <xsl:when test="w:family/@w:val='Modern'">
  103. <xsl:attribute name="style:font-family-generic">modern</xsl:attribute>
  104. </xsl:when>
  105. <xsl:when test="w:family/@w:val='Roman'">
  106. <xsl:attribute name="style:font-family-generic">roman</xsl:attribute>
  107. </xsl:when>
  108. <xsl:when test="w:family/@w:val='Script'">
  109. <xsl:attribute name="style:font-family-generic">script</xsl:attribute>
  110. </xsl:when>
  111. <xsl:when test="w:family/@w:val='Decorative'">
  112. <xsl:attribute name="style:font-family-generic">decorative</xsl:attribute>
  113. </xsl:when>
  114. <xsl:when test="w:family/@w:val='System'">
  115. <xsl:attribute name="style:font-family-generic">system</xsl:attribute>
  116. </xsl:when>
  117. <xsl:otherwise>
  118. <xsl:attribute name="style:font-family-generic">system</xsl:attribute>
  119. </xsl:otherwise>
  120. </xsl:choose>
  121. </xsl:if>
  122. <xsl:if test="w:pitch and string-length(w:pitch/@w:val) &gt; 0">
  123. <xsl:attribute name="style:font-pitch">
  124. <xsl:choose>
  125. <xsl:when test="w:pitch/@w:val = 'default'">variable</xsl:when>
  126. <xsl:otherwise>
  127. <xsl:value-of select="w:pitch/@w:val"/>
  128. </xsl:otherwise>
  129. </xsl:choose>
  130. </xsl:attribute>
  131. </xsl:if>
  132. </xsl:element>
  133. </xsl:for-each>
  134. </xsl:element>
  135. </xsl:template>
  136. <xsl:template match="w:styles">
  137. <office:styles>
  138. <!--The next statement Added by wguo,collect the pict's dash and mark-style.The template is implemented in file wordml2ooo_draw.xsl-->
  139. <xsl:apply-templates select="/w:wordDocument/w:body//w:pict" mode="style4dash_mark"/>
  140. <xsl:apply-templates select="//v:fill" mode="office-style"/>
  141. <xsl:call-template name="create-default-paragraph-styles"/>
  142. <xsl:call-template name="create-default-text-styles"/>
  143. <xsl:call-template name="create-default-frame-style"/>
  144. <!-- StarWriter has no default style family 'list'. glu -->
  145. <xsl:if test="w:style[@w:type = 'paragraph' and w:pPr/w:outlineLvl and w:pPr/w:listPr]">
  146. <xsl:call-template name="create-outline-style"/>
  147. </xsl:if>
  148. <xsl:apply-templates select="w:style[@w:type='table']" mode="table"/>
  149. <xsl:apply-templates select="w:style[@w:type='list']" mode="list"/>
  150. <xsl:apply-templates select="w:style[@w:type!='list']"/>
  151. <xsl:apply-templates select="/w:wordDocument/w:docPr/w:footnotePr" mode="config"/>
  152. <xsl:apply-templates select="/w:wordDocument/w:docPr/w:endnotePr" mode="config"/>
  153. </office:styles>
  154. <office:automatic-styles>
  155. <xsl:apply-templates select="/w:wordDocument/w:body//w:p" mode="style"/>
  156. <xsl:apply-templates select="/w:wordDocument/w:body//w:rPr[not(parent::w:pPr)]" mode="style"/>
  157. <!--The next statement Added by wguo for the pict's draw-style.The template is implemented in file wordml2ooo_draw.xsl-->
  158. <xsl:apply-templates select="/w:wordDocument/w:body//w:pict" mode="style"/>
  159. <xsl:apply-templates select="/w:wordDocument/w:body//w:tblPr" mode="style"/>
  160. <xsl:apply-templates select="/w:wordDocument/w:body//w:gridCol" mode="style"/>
  161. <xsl:apply-templates select="/w:wordDocument/w:body//w:trPr" mode="style"/>
  162. <xsl:apply-templates select="/w:wordDocument/w:body//w:tcPr" mode="style"/>
  163. <xsl:apply-templates select="/w:wordDocument/w:body//w:listPr" mode="style"/>
  164. <xsl:apply-templates select="/w:wordDocument/w:body//w:sectPr" mode="page-layout"/>
  165. <xsl:call-template name="default_date_style"/>
  166. <!--add for generate the date , time style for date , time field G.Y.-->
  167. <xsl:apply-templates select="/w:wordDocument/w:body//w:instrText | /w:wordDocument/w:body//w:fldSimple " mode="style"/>
  168. </office:automatic-styles>
  169. <office:master-styles>
  170. <xsl:apply-templates select="/w:wordDocument/w:body//w:sectPr" mode="master-page"/>
  171. </office:master-styles>
  172. </xsl:template>
  173. <xsl:template match="w:style">
  174. <style:style>
  175. <xsl:attribute name="style:name">
  176. <xsl:value-of select="concat('w',translate(@w:styleId,' ~`!@#$%^*(&#x26;)+/,;?&lt;&gt;{}[]:','_'))"/>
  177. </xsl:attribute>
  178. <xsl:if test="w:basedOn">
  179. <xsl:attribute name="style:parent-style-name">
  180. <xsl:value-of select="concat('w',translate(w:basedOn/@w:val,' ~`!@#$%^*(&#x26;)+/,;?&lt;&gt;{}[]:','_'))"/>
  181. </xsl:attribute>
  182. </xsl:if>
  183. <xsl:if test="w:next">
  184. <xsl:attribute name="style:next-style-name">
  185. <xsl:value-of select="concat('w',translate(w:basedOn/@w:val,' ~`!@#$%^*(&#x26;)+/,;?&lt;&gt;{}[]:','_'))"/>
  186. </xsl:attribute>
  187. </xsl:if>
  188. <xsl:choose>
  189. <xsl:when test="@w:type = 'character'">
  190. <xsl:attribute name="style:family">text</xsl:attribute>
  191. </xsl:when>
  192. <!-- table, paragraph are the same as in Writer . glu -->
  193. <xsl:when test="@w:type">
  194. <xsl:attribute name="style:family">
  195. <xsl:value-of select="@w:type"/>
  196. </xsl:attribute>
  197. </xsl:when>
  198. <xsl:otherwise>
  199. <xsl:attribute name="style:family">text</xsl:attribute>
  200. </xsl:otherwise>
  201. </xsl:choose>
  202. <xsl:choose>
  203. <xsl:when test="@w:type = 'table'">
  204. <xsl:element name="style:table-properties">
  205. <!-- xsl:apply-templates select="w:tblPr" mode="style"/ -->
  206. </xsl:element>
  207. </xsl:when>
  208. <xsl:when test="@w:type = 'character' ">
  209. <xsl:element name="style:text-properties">
  210. <!--
  211. <xsl:apply-templates select="w:pPr/w:rPr"/>
  212. <xsl:apply-templates select="w:rPr"/>
  213. -->
  214. <xsl:for-each select="w:rPr">
  215. <xsl:call-template name="text-properties"/>
  216. </xsl:for-each>
  217. </xsl:element>
  218. </xsl:when>
  219. <xsl:otherwise>
  220. <xsl:element name="style:paragraph-properties">
  221. <xsl:apply-templates select="w:pPr"/>
  222. </xsl:element>
  223. <xsl:element name="style:text-properties">
  224. <xsl:apply-templates select="w:rPr"/>
  225. <xsl:apply-templates select="w:pPr/w:rPr"/>
  226. </xsl:element>
  227. </xsl:otherwise>
  228. </xsl:choose>
  229. </style:style>
  230. </xsl:template>
  231. <xsl:template match="w:body">
  232. <xsl:element name="office:body">
  233. <xsl:element name="office:text">
  234. <!-- to add the sequece variable declaration at the begining of the office:body G.Y.-->
  235. <text:sequence-decls>
  236. <xsl:call-template name="default_seqence_declaration"/>
  237. <xsl:apply-templates select="/w:wordDocument/w:body//w:instrText[substring(normalize-space(text()),1,3) = 'SEQ' ] | /w:wordDocument/w:body//w:fldSimple[substring(normalize-space(@w:instr),1,3) = 'SEQ' ] " mode="sequence_declare"/>
  238. </text:sequence-decls>
  239. <!-- add the user field variables declare for Docpropety fields importing G.Y.-->
  240. <text:user-field-decls>
  241. <xsl:call-template name="user_fields_declare_docproperty"/>
  242. </text:user-field-decls>
  243. <xsl:apply-templates mode="dispatch"/>
  244. </xsl:element>
  245. </xsl:element>
  246. </xsl:template>
  247. <xsl:template match="wx:sect">
  248. <xsl:apply-templates mode="dispatch"/>
  249. </xsl:template>
  250. <xsl:template match="wx:sub-section">
  251. <xsl:apply-templates mode="dispatch"/>
  252. </xsl:template>
  253. <xsl:template name="create-default-frame-style">
  254. <!--add for default frame style -->
  255. <style:style style:name="Frame" style:family="graphic">
  256. <style:graphic-properties text:anchor-type="paragraph" svg:x="0in" svg:y="0in" style:wrap="parallel" style:number-wrapped-paragraphs="no-limit" style:wrap-contour="false" style:vertical-pos="top" style:vertical-rel="paragraph-content" style:horizontal-pos="center" style:horizontal-rel="paragraph-content"/>
  257. </style:style>
  258. </xsl:template>
  259. <xsl:template name="ConvertMeasure">
  260. <xsl:param name="TargetMeasure" select="'cm'"/>
  261. <xsl:param name="TargetTruncate" select=" 'all' "/>
  262. <xsl:param name="value"/>
  263. <!-- When TargetTruncate ='all' it returns the number whichsoever the return value is negative or positive
  264. When TargetTruncate ='nonNegative' it only returns nonNegative number, all negative number to be returned as 0
  265. When TargetTruncate ='positive" it only returns positive number, all nonPositive number to be returned as 1 -->
  266. <xsl:variable name="return_value">
  267. <xsl:choose>
  268. <!-- remove the measure mark, if the value is null, the result should be 0. Must be the first case -->
  269. <xsl:when test="string-length(translate($value,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ','')) = 0">0</xsl:when>
  270. <xsl:when test="string-length(translate($value,'-.0123456789 ','')) = 0">
  271. <xsl:value-of select="$value"/>
  272. </xsl:when>
  273. <xsl:when test="$TargetMeasure = 'cm'">
  274. <xsl:call-template name="convert2cm">
  275. <xsl:with-param name="value" select="$value"/>
  276. </xsl:call-template>
  277. </xsl:when>
  278. <xsl:when test="$TargetMeasure = 'pt'">
  279. <xsl:call-template name="convert2pt">
  280. <xsl:with-param name="value" select="$value"/>
  281. </xsl:call-template>
  282. </xsl:when>
  283. <xsl:when test="$TargetMeasure = 'twip'">
  284. <xsl:call-template name="convert2twip">
  285. <xsl:with-param name="value" select="$value"/>
  286. </xsl:call-template>
  287. </xsl:when>
  288. <xsl:when test="$TargetMeasure = 'in'">
  289. <xsl:call-template name="convert2in">
  290. <xsl:with-param name="value" select="$value"/>
  291. </xsl:call-template>
  292. </xsl:when>
  293. </xsl:choose>
  294. </xsl:variable>
  295. <xsl:choose>
  296. <xsl:when test="$TargetTruncate = 'all' ">
  297. <xsl:choose>
  298. <xsl:when test="number($TargetMeasure) = 'NaN' ">
  299. <xsl:value-of select=" '0' "/>
  300. </xsl:when>
  301. <xsl:otherwise>
  302. <xsl:value-of select="$return_value"/>
  303. </xsl:otherwise>
  304. </xsl:choose>
  305. </xsl:when>
  306. <xsl:when test="$TargetTruncate = 'nonNegative' ">
  307. <xsl:choose>
  308. <xsl:when test="number($TargetMeasure) = 'NaN' ">
  309. <xsl:value-of select=" '0' "/>
  310. </xsl:when>
  311. <xsl:otherwise>
  312. <xsl:choose>
  313. <xsl:when test=" $return_value &lt; 0 ">
  314. <xsl:value-of select=" '0' "/>
  315. </xsl:when>
  316. <xsl:otherwise>
  317. <xsl:value-of select="$return_value"/>
  318. </xsl:otherwise>
  319. </xsl:choose>
  320. </xsl:otherwise>
  321. </xsl:choose>
  322. </xsl:when>
  323. <xsl:when test="$TargetTruncate = 'positive' ">
  324. <xsl:choose>
  325. <xsl:when test="number($TargetMeasure) = 'NaN' ">
  326. <xsl:value-of select=" '1' "/>
  327. </xsl:when>
  328. <xsl:otherwise>
  329. <xsl:choose>
  330. <xsl:when test=" $return_value &lt;= 0 ">
  331. <xsl:value-of select=" '1' "/>
  332. </xsl:when>
  333. <xsl:otherwise>
  334. <xsl:value-of select="$return_value"/>
  335. </xsl:otherwise>
  336. </xsl:choose>
  337. </xsl:otherwise>
  338. </xsl:choose>
  339. </xsl:when>
  340. </xsl:choose>
  341. </xsl:template>
  342. </xsl:stylesheet>