<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output encoding="utf-8" indent="yes" method="html"/>

<!--Here is my xslt code:-->
<xsl:template match="p">
<xsl:choose>
 <xsl:when test="contains(text()[last()], 'mod-')">
  <xsl:if test="count(*[preceding-sibling::text()[last()]]) > 0">
  <!--I know this is wrong one-->
   <xsl:apply-templates select="text()|node()[. &gt;&gt; preceding-sibling::node()[last()]]"/> 
  </xsl:if>
  <xsl:for-each select="text()">
   <xsl:value-of select="substring-before(., '{')"/>
   <xsl:call-template name="Replace">
   <xsl:with-param name="outputString" select="substring-after(., '{')"/>
   <xsl:with-param name="target" select="'\['"/>
   <xsl:with-param name="replacement" select="substring-after(., '[module ')"/>
   </xsl:call-template>
  </xsl:for-each>
 </xsl:when>
 <!--This piece even didn't work for me-->
 <xsl:when test="matches(text()[last()], 'module ') and not(matches(text()[last()], '\{'))">
	<xsl:element name="p">
	<xsl:if test="count(*[preceding-sibling::text()[last()]]) > 0">
		<xsl:apply-templates select="text()|node()[. &gt;&gt; preceding-sibling::text()[last()] and . &lt;&lt; text()[last()]]"/>
	</xsl:if>
	<xsl:for-each select="text()">
		<xsl:choose>
			<xsl:when test="matches(., '\{')">
				<xsl:apply-templates select="text()|node()[. &lt;&lt; ../text()[position()]]"/>
				<xsl:value-of select="substring-before(., '{')"/>
				<xsl:call-template name="Replace">
  				<xsl:with-param name="outputString" select="substring-after(., '{')"/>
  				<xsl:with-param name="target" select="'\['"/>
 				<xsl:with-param name="replacement" select="substring-after(../text()[last()], '[module ')"/>
  				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise></xsl:otherwise>
		</xsl:choose>
	</xsl:for-each>
	</xsl:element>
	</xsl:when>
<xsl:otherwise>
 <xsl:element name="p"><xsl:apply-templates/></xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!--call-template "Replace"-->
<xsl:template name="Replace">
  <xsl:param name="outputString"/>
  <xsl:param name="target"/>
  <xsl:param name="replacement"/>
  <xsl:variable name="temp" select="substring-before($outputString,'module ')"/>
  <xsl:choose>
    <xsl:when test="$temp or contains($outputString,'\[')">
		<xsl:element name="a"><xsl:attribute name="href">rhll-module://<xsl:value-of select="replace($replacement, '\]\}', '')"/></xsl:attribute><xsl:value-of select="replace($temp, '\s\[', '')"/></xsl:element>
      <xsl:call-template name="Replace">
        <xsl:with-param name="outputString" select="substring-after($outputString,'\[')"/>
        <xsl:with-param name="target" select="'\['"/>
        <xsl:with-param name="replacement" select="$replacement"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$outputString"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="node()|@*">
    <xsl:copy>
	  <xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
  </xsl:template>

</xsl:stylesheet><!-- Stylus Studio meta-information - (c) 2004-2007. Progress Software Corporation. All rights reserved.

<metaInformation>
	<scenarios>
		<scenario default="yes" name="Scenario1" userelativepaths="yes" externalpreview="no" url="source.xml" htmlbaseurl="" outputurl="" processortype="saxon8" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml=""
		          commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator="">
			<advancedProp name="sInitialMode" value=""/>
			<advancedProp name="bXsltOneIsOkay" value="true"/>
			<advancedProp name="bSchemaAware" value="true"/>
			<advancedProp name="bXml11" value="false"/>
			<advancedProp name="iValidation" value="0"/>
			<advancedProp name="bExtensions" value="true"/>
			<advancedProp name="iWhitespace" value="0"/>
			<advancedProp name="sInitialTemplate" value=""/>
			<advancedProp name="bTinyTree" value="true"/>
			<advancedProp name="bWarnings" value="true"/>
			<advancedProp name="bUseDTD" value="false"/>
			<advancedProp name="iErrorHandling" value="fatal"/>
		</scenario>
	</scenarios>
	<MapperMetaTag>
		<MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/>
		<MapperBlockPosition></MapperBlockPosition>
		<TemplateContext></TemplateContext>
		<MapperFilter side="source"></MapperFilter>
	</MapperMetaTag>
</metaInformation>
-->