Advertisement
cmhughes

xslt string splitting at ; and at :

Jan 3rd, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. Not sure where else to put this
  2.  
  3.  
  4. <!-- siunitx templates
  5. this one is called when the u attribute
  6. is not used, e.g
  7.  
  8. <quant n="1/5"/>
  9.  
  10. becomes
  11.  
  12. \num{1/5}
  13.  
  14. <xsl:template match="quant[not(@u)]">
  15. <xsl:choose>
  16. <xsl:when test="@n">
  17. <xsl:text>\num{</xsl:text>
  18. <xsl:value-of select="@n"/>
  19. <xsl:text>}</xsl:text>
  20. </xsl:when>
  21. <xsl:otherwise>
  22. <xsl:text>??quantity fix??</xsl:text>
  23. <xsl:message terminate="no">
  24. <xsl:text>no 'n' attribute present (no units either)</xsl:text>
  25. </xsl:message>
  26. </xsl:otherwise>
  27. </xsl:choose>
  28. </xsl:template>
  29. -->
  30.  
  31. <!-- siunitx commands when the u attribute is used,
  32. e.g
  33.  
  34. <quant u="kilo;gram;meter;" />
  35. <quant u="kilo:1;gram:2;meter:3;" />
  36. <quant n="63" u="kilo:1;gram:2;meter:3;" />
  37. <quant n="63" u="kilo:1;gram:2;meter:3;" per="liter:4" />
  38.  
  39. becomes
  40. \si{\kilo\gram\meter}
  41. \si{\kilo\tothe{1}\gram\tothe{2}\meter\tothe{3}}
  42. \SI{63}{\kilo\tothe{1}\gram\tothe{2}\meter\tothe{3}}
  43. \SI{63}{\kilo\tothe{1}\gram\tothe{2}\meter\tothe{3}\per\liter\tothe{4}}
  44. -->
  45. <xsl:template match="quant[@u]">
  46. <xsl:param name="unitText" select="@u"/>
  47. <xsl:choose>
  48. <!-- quantity with number -->
  49. <xsl:when test="@n">
  50. <xsl:text>\SI{</xsl:text>
  51. <xsl:value-of select="@n"/>
  52. <xsl:text>}{</xsl:text>
  53. </xsl:when>
  54. <!-- quantity with*out* number -->
  55. <xsl:otherwise>
  56. <xsl:text>\si{</xsl:text>
  57. </xsl:otherwise>
  58. </xsl:choose>
  59. <!-- either way, split the units and put them in -->
  60. <xsl:call-template name="split">
  61. <xsl:with-param name="unitText" select="$unitText"/>
  62. </xsl:call-template>
  63. <!-- if the per attribute exists, then apply it -->
  64. <xsl:if test="@per">
  65. <xsl:text>\per</xsl:text>
  66. <xsl:call-template name="split">
  67. <xsl:with-param name="unitText" select="@per"/>
  68. </xsl:call-template>
  69. </xsl:if>
  70. <xsl:text>}</xsl:text>
  71. </xsl:template>
  72.  
  73. <!-- template to split the units at the semi-colon, and then
  74. at the colon -->
  75. <xsl:template name="split">
  76. <xsl:param name="unitText"/>
  77. <xsl:if test="string-length($unitText)">
  78. <!-- split string at semi-colon
  79. http://stackoverflow.com/questions/4845660/xsl-how-to-split-strings -->
  80. <xsl:variable name="eachUnit">
  81. <xsl:value-of select="substring-before(concat($unitText,';'),';')"/>
  82. </xsl:variable>
  83. <!-- test if the string contains a :
  84. http://stackoverflow.com/questions/569908/is-there-an-xsl-contains-directive -->
  85. <xsl:choose>
  86. <!-- split each unit at the colon : (to get the exponent)
  87. but only when there is one -->
  88. <xsl:when test="contains($eachUnit,':')">
  89. <!-- insert the backslash -->
  90. <xsl:text>\</xsl:text>
  91. <xsl:value-of select="substring-before($eachUnit,':')"/>
  92. <xsl:text>\tothe{</xsl:text>
  93. <xsl:value-of select="substring-after($eachUnit,':')"/>
  94. <xsl:text>}</xsl:text>
  95. </xsl:when>
  96. <xsl:otherwise>
  97. <!-- insert the backslash -->
  98. <xsl:text>\</xsl:text>
  99. <xsl:value-of select="substring-before($unitText,';')"/>
  100. </xsl:otherwise>
  101. </xsl:choose>
  102. <!-- call this template recursively -->
  103. <xsl:call-template name="split">
  104. <xsl:with-param name="unitText" select="substring-after($unitText, ';')"/>
  105. </xsl:call-template>
  106. </xsl:if>
  107. </xsl:template>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement