generalize.xsl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- This file is part of the DITA Open Toolkit project hosted on
  3. Sourceforge.net. See the accompanying license.txt file for
  4. applicable licenses.-->
  5. <!-- (c) Copyright IBM Corp. 2004, 2005 All Rights Reserved. -->
  6. <!-- generalize.xsl
  7. | Convert specialied DITA topics into revertable, "generalized" form
  8. *-->
  9. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  10. xmlns:java="org.dita.dost.util.GenUtils" exclude-result-prefixes="java">
  11. <xsl:param name="outdir.subsidiary"/>
  12. <xsl:variable name="file-prefix">
  13. <xsl:choose>
  14. <xsl:when test="string-length($outdir.subsidiary) &gt; 0 and not(substring($outdir.subsidiary,string-length($outdir.subsidiary))='/')and not(substring($outdir.subsidiary,string-length($outdir.subsidiary))='\')">
  15. <xsl:value-of select="translate($outdir.subsidiary,
  16. '\/=+|?[]{}()!#$%^&amp;*__~`;:.,-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  17. '//=+|?[]{}()!#$%^&amp;*__~`;:.,-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"/><xsl:text>/</xsl:text>
  18. </xsl:when>
  19. <xsl:otherwise>
  20. <xsl:value-of select="translate($outdir.subsidiary,
  21. '\/=+|?[]{}()!#$%^&amp;*__~`;:.,-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  22. '//=+|?[]{}()!#$%^&amp;*__~`;:.,-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
  23. </xsl:otherwise>
  24. </xsl:choose>
  25. </xsl:variable>
  26. <xsl:output method="xml" indent="no"/>
  27. <xsl:template match="*[@class]">
  28. <xsl:variable name="generalize" select="substring-before(substring-after(@class,'/'),' ')" />
  29. <xsl:element name="{$generalize}">
  30. <xsl:copy-of select="@*"/>
  31. <xsl:apply-templates/>
  32. </xsl:element>
  33. </xsl:template>
  34. <xsl:template match="*|@*|comment()|processing-instruction()|text()">
  35. <xsl:copy>
  36. <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
  37. </xsl:copy>
  38. </xsl:template>
  39. <xsl:template match="*[contains(@class,' topic/unknown ') or contains(@class,' topic/foreign ')]" priority="10">
  40. <xsl:variable name="spec-type">
  41. <xsl:call-template name="get-spec-type"/>
  42. </xsl:variable>
  43. <xsl:variable name="generalize" select="substring-before(substring-after(@class,'/'),' ')"/>
  44. <xsl:element name="{$generalize}">
  45. <xsl:apply-templates select="*|@*|text()|comment()|processing-instruction()" mode="generalize-foreign-unknown">
  46. <xsl:with-param name="spec-type" select="$spec-type"/>
  47. </xsl:apply-templates>
  48. </xsl:element>
  49. </xsl:template>
  50. <xsl:template match="*[contains(@class,' topic/object ')]|@*|text()|comment()|processing-instruction()" mode="generalize-foreign-unknown">
  51. <xsl:copy>
  52. <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
  53. </xsl:copy>
  54. </xsl:template>
  55. <xsl:template match="*[not(contains(@class,' topic/object '))]" mode="generalize-foreign-unknown">
  56. <xsl:param name="spec-type"/>
  57. <xsl:value-of select="preceding-sibling::*[not(contains(@class,' topic/topic '))]"/>
  58. <!-- find out the subsidiary result file name that need to be generated. -->
  59. <xsl:variable name="filename">
  60. <xsl:text>dita-generalized-</xsl:text>
  61. <xsl:value-of select="ancestor::*[contains(@class,' topic/topic ')][1]/@id"/>
  62. <xsl:text>-</xsl:text>
  63. <xsl:value-of select="concat($spec-type,generate-id(.))"/>
  64. <xsl:text>.xml</xsl:text>
  65. </xsl:variable>
  66. <xsl:element name="object">
  67. <xsl:attribute name="data">
  68. <xsl:value-of select="$filename"/>
  69. </xsl:attribute>
  70. <xsl:attribute name="type">
  71. <xsl:text>DITA-foreign</xsl:text>
  72. </xsl:attribute>
  73. </xsl:element>
  74. <xsl:variable name="generalize" select="substring-before(substring-after(../@class,'/'),' ')"></xsl:variable>
  75. <xsl:value-of select="java:clear()"/>
  76. <xsl:value-of select="java:setOutput(concat($file-prefix,$filename))"/>
  77. <xsl:value-of select="java:startElement($generalize)"/>
  78. <xsl:value-of select="java:addAttr('class',string(../@class))"/>
  79. <xsl:apply-templates select="." mode="generalize-subsidiary"/>
  80. <xsl:value-of select="java:endElement($generalize)"/>
  81. <xsl:value-of select="java:flush()"/>
  82. </xsl:template>
  83. <xsl:template match="*" mode="generalize-subsidiary">
  84. <xsl:variable name="name" select="name()"/>
  85. <xsl:value-of select="java:startElement($name)"/>
  86. <xsl:apply-templates select="*|@*|text()|comment()" mode="generalize-subsidiary"/>
  87. <xsl:value-of select="java:endElement($name)"/>
  88. </xsl:template>
  89. <xsl:template match="@*" mode="generalize-subsidiary">
  90. <xsl:variable name="name" select="name()"/>
  91. <xsl:variable name="value"><xsl:value-of select="."/></xsl:variable>
  92. <xsl:value-of select="java:addAttr($name,$value)"/>
  93. </xsl:template>
  94. <xsl:template match="text()|comment()" mode="generalize-subsidiary">
  95. <xsl:variable name="text"><xsl:value-of select="."/></xsl:variable>
  96. <xsl:value-of select="java:addText($text)"/>
  97. </xsl:template>
  98. <xsl:template name="get-spec-type">
  99. <xsl:param name="class" select="substring-after(@class,' ')"/>
  100. <xsl:choose>
  101. <xsl:when test="contains(substring-after($class,'/'),'/')">
  102. <xsl:call-template name="get-spec-type">
  103. <xsl:with-param name="class" select="substring-after($class,' ')"/>
  104. </xsl:call-template>
  105. </xsl:when>
  106. <xsl:otherwise>
  107. <xsl:value-of select="substring-before($class,'/')"/>
  108. </xsl:otherwise>
  109. </xsl:choose>
  110. </xsl:template>
  111. </xsl:stylesheet>