Hi i'm having the below xml.
<primaryie>
<content-style font-style="bold">Administration</content-style>
</primaryie>
and when i'm applying the below xslt it is working fine.(the content-style part)
<xsl:template match="primaryie">
<div class="primaryie">
<xsl:apply-templates select="content-style"/>
<xsl:if test="contains(current()/text(), '.')">
<xsl:variable name="numberString" select="substring(current()/text(), string-length(substring-before(current()/text(),'.'))-1)"></xsl:variable>
<xsl:call-template name="numbersToLink">
<xsl:with-param name="numbersString" select="$numberString"></xsl:with-param>
</xsl:call-template>
</xsl:if>
</div>
</xsl:template>
<xsl:template name="numbersToLink">
<xsl:param name="numbersString"></xsl:param>
<xsl:choose>
<xsl:when test="contains($numbersString, ',')">
<xsl:call-template name="splitByComma">
<xsl:with-param name="numString" select="$numbersString"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($numbersString, '-')">
<xsl:call-template name="splitByHyphen">
<xsl:with-param name="numString" select="$numbersString"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="5 >= string-length(normalize-space($numbersString))">
<xsl:variable name="x">
<xsl:value-of select="substring-after($numbersString,'.')"></xsl:value-of>
</xsl:variable>
<xsl:variable name="y">
<xsl:value-of select="normalize-space(substring-before($numbersString,'.'))"></xsl:value-of>
</xsl:variable>
<xsl:variable name="conca">
<xsl:value-of select="concat('er:#BVI_CH_0',$y,'/P',$y,'-',$x)"/>
</xsl:variable>
<a href="{$conca}">
<xsl:value-of select="$numbersString"/>
</a>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="splitByComma">
<xsl:param name="numString"></xsl:param>
<xsl:choose>
<xsl:when test="contains(substring-before($numString,','), '-')">
<xsl:call-template name="splitByHyphen">
<xsl:with-param name="numString" select="$numString"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($numString,',')">
<xsl:variable name="abc">
<xsl:value-of select="normalize-space(substring-before($numString,'.'))"/>
</xsl:variable>
<xsl:variable name="def">
<xsl:value-of select="substring-before(substring-after($numString,'.'),',') "/>
</xsl:variable>
<xsl:variable name="conct">
<xsl:value-of select="concat('er:#BVI_CH_0',$abc,'/P',$abc,'-',$def)"/>
<!--"concat(concat('er:#BVI_CH_0',,'/P',$y,'-',$x)"/-->
</xsl:variable>
<a href="{$conct}">
<xsl:value-of select="substring-before($numString,',')"/>
</a>
<xsl:text>, </xsl:text>
<xsl:if test="contains(substring-after($numString,','), '.')">
<xsl:call-template name="numbersToLink">
<xsl:with-param name="numbersString" select="normalize-space(substring-after($numString,','))"/>
</xsl:call-template>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="splitByHyphen">
<xsl:param name="numString"></xsl:param>
<xsl:choose>
<xsl:when test="contains($numString,'-')">
<xsl:variable name="abc">
<xsl:value-of select="normalize-space(substring-before($numString,'.'))"/>
</xsl:variable>
<xsl:variable name="def">
<xsl:value-of select="substring-before(substring-after($numString,'.'),'-') "/>
</xsl:variable>
<xsl:variable name="conct">
<xsl:value-of select="concat('er:#BVI_CH_0',$abc,'/P',$abc,'-',$def)"/>
<!--"concat(concat('er:#BVI_CH_0',,'/P',$y,'-',$x)"/-->
</xsl:variable>
<a href="{$conct}">
<!--<xsl:value-of select="substring-before($numString,'-')"/>-->
<xsl:value-of select="substring-before($numString,'-')"/>
</a>
<xsl:text>–</xsl:text>
<xsl:if test="contains(substring-after($numString,'-'), '.')">
<xsl:call-template name="numbersToLink">
<xsl:with-param name="numbersString" select="normalize-space(substring-after($numString,'-'))"/>
</xsl:call-template>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:template>
but i have a special case in the same document as below xml states.
<primaryie>
<content-style font-style="bold">VIRRGIN system</content-style> 7.204, 7.205
</primaryie>
here when i'm applying the above template it is working with content-style, but i want it to work also with numberstolink template.
the outputs are as below.
current:
<div class="primaryie"><span class="font-style-bold">Virgin Islands Special Trust Act (VISTA) 9.077</span></div>
expexted:
<div class="primaryie"><span class="font-style-bold">Virgin Islands Special Trust Act (VISTA) 9.077</span></div>
Please change this below portion in your code
<xsl:template match="primaryie">
<div class="primaryie">
<xsl:apply-templates select="content-style"/>
<xsl:if test="contains(current(), '.')">
<xsl:variable name="numberString" select="substring(current(), string-length(substring-before(current(),'.'))-1)"></xsl:variable>
<xsl:call-template name="numbersToLink">
<xsl:with-param name="numbersString" select="$numberString"></xsl:with-param>
</xsl:call-template>
</xsl:if>
</div>
</xsl:template>
Use only Current() instead of Current()/Text()
Output for give XML is
<div class="primaryie">VIRRGIN system 7.204, 7.205</div>
Related
My thesis uses an APA citation. However, with research documents that is written in Thai, an inline citation must be of the form (firstname lastname, year) rather than the APA (lastname, year). This should be possible with a custom Microsoft Word 2013 bibliography style which would require an adjustment to the APA.XSL file in C:\Program Files (x86)\Microsoft Office\Office14\Bibliography\Style to include an additional logic similar to the follow:
<xsl:choose>
<xsl:when test="b:LCID='1054'">
(Firstname Lastname, year)
</xsl:when>
<xsl:otherwise>
(Lastname, year)
</xsl:otherwise>
</xsl:choose>
I believe that the additional logic should be added in this section, and that it must involve an adjustment to xsl:template formatNameCore.
<xsl:variable name="author0">
</xsl:variable>
However, my mind is drawing a blank beyond that. I wonder if anyone could point me to a correct direction. Help is much appreciated.
Here's the current xsl file.
After some more searching (and reading), I have stumbled upon this post in which all the temp1_prop_APA__ are specified as included below:
templ_prop_APA_MainAuthors_FML = %L, %f %m
templ_prop_APA_MainAuthors_FM = %f %m
templ_prop_APA_MainAuthors_ML = %L, %m
templ_prop_APA_MainAuthors_FL = %L, %f
Having nothing to lose; hence, I tried. While I do not know how exactly these %L, %F, or %M and their lowercase counterparts have came to be (explanations would have been nice), the expressions do work. In turn, included below is the section of adjusted code. So far, it seems to work for the purpose.
<xsl:template name="templ_prop_APA_CitationLong_FML" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$_LCID='1054'">
<xsl:value-of select="'%F %M %L'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'%L, %F %M'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="templ_prop_APA_CitationLong_FM" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$_LCID='1054'">
<xsl:value-of select="'%F %M'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'%F %M'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="templ_prop_APA_CitationLong_ML" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$_LCID='1054'">
<xsl:value-of select="'%M %L'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'%L, %M'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="templ_prop_APA_CitationLong_FL" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$_LCID='1054'">
<xsl:value-of select="'%F %L'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'%L, %F'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="templ_prop_APA_CitationShort_FML" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$_LCID='1054'">
<xsl:value-of select="'%F %M %L'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'%L'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="templ_prop_APA_CitationShort_FM" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$_LCID='1054'">
<xsl:value-of select="'%F %M'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'%F'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="templ_prop_APA_CitationShort_ML" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$_LCID='1054'">
<xsl:value-of select="'%M %L'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'%L'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="templ_prop_APA_CitationShort_FL" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$_LCID='1054'">
<xsl:value-of select="'%F %L'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'%L'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Note: A full file is included here.
It has something to do with these templates:
<xsl:template name="templ_prop_APA_CitationShort_FML" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="/*/b:Locals/b:Local[#LCID=$_LCID]/b:APA/b:CitationShort/b:FML"/>
</xsl:template>
<!-- ... -->
<xsl:template name="templ_prop_APA_CitationShort_FL" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="/*/b:Locals/b:Local[#LCID=$_LCID]/b:APA/b:CitationShort/b:FL"/>
</xsl:template>
Try:
<xsl:template name="templ_prop_APA_CitationShort_FML" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="/*/b:Locals/b:Local[#LCID=$_LCID]/b:APA/b:CitationShort/b:FM"/>
<xsl:text> </xsl:text>
<xsl:value-of select="/*/b:Locals/b:Local[#LCID=$_LCID]/b:APA/b:CitationShort/b:FML"/>
</xsl:template>
<!-- ... -->
<xsl:template name="templ_prop_APA_CitationShort_FL" >
<xsl:param name="LCID" />
<xsl:variable name="_LCID">
<xsl:call-template name="localLCID">
<xsl:with-param name="LCID" select="$LCID"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="/*/b:Locals/b:Local[#LCID=$_LCID]/b:APA/b:CitationShort/b:FM"/>
<xsl:text> </xsl:text>
<xsl:value-of select="/*/b:Locals/b:Local[#LCID=$_LCID]/b:APA/b:CitationShort/b:FL"/>
</xsl:template>
I can't figure out where these values are getting populated, so this is the best I can do for now.
It seem safe to make this change here, instead of in the loop in <xsl:variable name="author0">...</xsl:variable>, as it's only ever called from there. This change won't affect your bibliography.
You may also have to modify the other temp1_prop_APA_*_* templates similarly.
I hope someone can help me with this...
I am using SharePoint 2013 and trying to render a CQWP to show the latest blog posts. The problem I have is that when displaying the 'content' from the post I get ' ' added and quotes are shown as '"'. I have managed to strip the HTML mark up but can't seem to get rid of these.
My code is as follows - any help would be much appreciated, thanks.
Generate Summary and Remove HTML
<!-- Generate Summary -->
<xsl:template name="GenerateSummary">
<xsl:param name="Content"/>
<xsl:param name="Length"/>
<xsl:param name="Suffix"/>
<xsl:variable name="cleanContent">
<xsl:call-template name="RemoveHtml">
<xsl:with-param name="String" select="$Content"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="SubstringBeforeLast">
<xsl:with-param name="String"
select="substring($cleanContent, 1, $Length)"/>
<xsl:with-param name="Char" select="' '"/>
</xsl:call-template>
<xsl:if test="string-length($cleanContent) > $Length">
<xsl:value-of select="$Suffix"
disable-output-escaping="yes"/>
</xsl:if>
</xsl:template>
<!-- RemoveHTML -->
<xsl:template name="RemoveHtml">
<xsl:param name="String"/>
<xsl:choose>
<xsl:when test="contains($String, '<')">
<xsl:value-of select="substring-before($String, '<')"/>
<xsl:call-template name="RemoveHtml">
<xsl:with-param name="String"
select="substring-after($String, '>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$String"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="SubstringBeforeLast">
<xsl:param name="String" />
<xsl:param name="Char" />
<xsl:param name="subsequent"/>
<xsl:choose>
<xsl:when test="contains($String, $Char)">
<xsl:if test="$subsequent = 1">
<xsl:value-of select="$Char"/>
</xsl:if>
<xsl:value-of select="substring-before($String, $Char)"/>
<xsl:call-template name="SubstringBeforeLast">
<xsl:with-param name="String"
select="substring-after($String, $Char)" />
<xsl:with-param name="Char" select="$Char" />
<xsl:with-param name="subsequent" select="1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:if test="$subsequent != 1">
<xsl:value-of select="$String"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Calling it here
<div class="fcItemContent">
<xsl:call-template name="GenerateSummary">
<xsl:with-param name="Content" select="#content" />
<xsl:with-param name="Length" select="200" />
<xsl:with-param name="Suffix" select="'...'"/>
</xsl:call-template>
</div>
Use function of XSLT
normalize-space()
**<xsl:value-of select="normalize-space()"/>**
White space is normalized by stripping leading and trailing white space and replacing sequences of white space characters with a single space. If the argument is omitted, the string-value of the context node is normalized and returned.
referred Link :
Remove Space using XSLT
Given the following XML:
<application>
<documentation>Before text.
[This|http://google.com] is a link.
Click [here|http://google.com] for another link.
After text.
</documentation>
</application>
I want to replace new lines with <br/> tags and create links. The outcome should produce the following HTML:
Before text.<br/>
This is a link.<br/>
Click here for another link.<br/>
After text.
I have the XSLT below with the appropriate templates. My problem is that the <br/> elements added by the first function are stripped out by the second function. I've tried using an identity transform, but I can't wrap my head around it.
Any help would be appreciated.
My current XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0"/>
<xsl:template match="application">
<p>
<xsl:variable name="with_new_lines">
<xsl:call-template name="replace-newlines">
<xsl:with-param name="text" select="documentation"/>
<xsl:with-param name="replace" select="'
'"/>
<xsl:with-param name="by" select="'new_line'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="with_links">
<xsl:call-template name="create-links">
<xsl:with-param name="text">
<xsl:copy-of select="$with_new_lines"/>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$with_links"/>
</p>
</xsl:template>
<xsl:template name="replace-newlines">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '
')">
<xsl:copy-of select="substring-before($text,'
')"/>
<br/>
<xsl:call-template name="replace-newlines">
<xsl:with-param name="text" select="substring-after($text,'
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Changing [here|http://a.com] to hereand
[http://a.com] to http://a.com-->
<xsl:template match="*" name="create-links">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, 'http')">
<xsl:copy-of select="substring-before($text,'[')"/>
<xsl:variable name="the_link_and_after">
<xsl:copy-of select="substring-after($text,'[')"/>
</xsl:variable>
<xsl:variable name="the_link">
<xsl:copy-of select="substring-before($the_link_and_after,']')"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($the_link,'|')">
<xsl:variable name="the_url">
<xsl:copy-of select="substring-after($the_link,'|')"/>
</xsl:variable>
<a href="{$the_url}">
<xsl:copy-of select="substring-before($the_link,'|')"/>
</a>
</xsl:when>
<xsl:otherwise>
<a href="{$the_link}">
<xsl:copy-of select="$the_link"/>
</a>
</xsl:otherwise>
</xsl:choose>
<!--<xsl:copy-of select="substring-after($text,']')" />-->
<xsl:call-template name="create-links">
<xsl:with-param name="text" select="substring-after($text,']')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The problem with your approach is that each of your named templates creates a result-tree-fragment, containing a mixture of text nodes and elements (either <br> or <a>). When you send the result to be processed as a string by the other template, only the text nodes survive the treatment.
I would suggest you start by tokenizing the input into lines as elements. Then send each line in turn to be processed by the other template:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="application">
<xsl:variable name="lines">
<xsl:call-template name="tokenize-lines">
<xsl:with-param name="text" select="documentation"/>
</xsl:call-template>
</xsl:variable>
<p>
<xsl:for-each select="exsl:node-set($lines)/line">
<xsl:call-template name="create-links">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<br/>
</xsl:for-each>
</p>
</xsl:template>
<xsl:template name="tokenize-lines">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '
')">
<line><xsl:copy-of select="substring-before($text,'
')"/></line>
<xsl:call-template name="tokenize-lines">
<xsl:with-param name="text" select="substring-after($text,'
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<line><xsl:copy-of select="$text"/></line>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="create-links">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, 'http')">
<xsl:copy-of select="substring-before($text,'[')"/>
<xsl:variable name="the_link_and_after">
<xsl:copy-of select="substring-after($text,'[')"/>
</xsl:variable>
<xsl:variable name="the_link">
<xsl:copy-of select="substring-before($the_link_and_after,']')"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($the_link,'|')">
<xsl:variable name="the_url">
<xsl:copy-of select="substring-after($the_link,'|')"/>
</xsl:variable>
<a href="{$the_url}">
<xsl:copy-of select="substring-before($the_link,'|')"/>
</a>
</xsl:when>
<xsl:otherwise>
<a href="{$the_link}">
<xsl:copy-of select="$the_link"/>
</a>
</xsl:otherwise>
</xsl:choose>
<!--<xsl:copy-of select="substring-after($text,']')" />-->
<xsl:call-template name="create-links">
<xsl:with-param name="text" select="substring-after($text,']')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Alternatively, you could call the 2nd template from within the 1st one, for each line separately before returning it:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="application">
<p>
<xsl:call-template name="create-lines">
<xsl:with-param name="text" select="documentation"/>
</xsl:call-template>
</p>
</xsl:template>
<xsl:template name="create-lines">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '
')">
<xsl:call-template name="create-links">
<xsl:with-param name="text" select="substring-before($text,'
')"/>
</xsl:call-template>
<br/>
<xsl:call-template name="create-lines">
<xsl:with-param name="text" select="substring-after($text,'
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="create-links">
<xsl:with-param name="text" select="$text"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="create-links">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, 'http')">
<xsl:copy-of select="substring-before($text,'[')"/>
<xsl:variable name="the_link_and_after">
<xsl:copy-of select="substring-after($text,'[')"/>
</xsl:variable>
<xsl:variable name="the_link">
<xsl:copy-of select="substring-before($the_link_and_after,']')"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($the_link,'|')">
<xsl:variable name="the_url">
<xsl:copy-of select="substring-after($the_link,'|')"/>
</xsl:variable>
<a href="{$the_url}">
<xsl:copy-of select="substring-before($the_link,'|')"/>
</a>
</xsl:when>
<xsl:otherwise>
<a href="{$the_link}">
<xsl:copy-of select="$the_link"/>
</a>
</xsl:otherwise>
</xsl:choose>
<!--<xsl:copy-of select="substring-after($text,']')" />-->
<xsl:call-template name="create-links">
<xsl:with-param name="text" select="substring-after($text,']')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
i am converting an html form to xml sequence, i am using a recursive function to achieve this
so the input to param "list" will be of the form
name=value&name=value&name=value
The template below does this fine and returns an xml sequence as follows
<name>value</name><name>value</name><name>value</name>
Ok so the problem
some of the name value pairs are special and i would like to add a attribute to them so the output would be
<name>value</name><name attr="special">value</name><name>value</name>
so to do this i have an external xml file with a list of special names as follows
<settings><google><option from="color"/><option from="size"/></google></settings>
So if we assume i have a xsl variable $SETTINGS connected to this external document above
<xsl:for-each select="$SETTINGS/google/option"></xsl:for-each>
Should be node with 2 children 1 called color and the other size
wot i want to do is if 1 of these children names = $name add the attribute
something like <xsl:if test="$name = $SETTINGS/google/option/ckild name">
<xsl:template name="tokenize">
<xsl:param name="list"/>
<xsl:variable name="seperator" select="'&'"/>
<xsl:variable name="first" select="substring-before(concat($list, $seperator), $seperator)"/>
<xsl:variable name="butfirst" select="substring-after($list, $seperator)"/>
<xsl:variable name="name" select="normalize-space(substring-before($first, '='))"/>
<xsl:variable name="value" select="normalize-space(substring-after($first, '='))"/>
<xsl:if test="string-length($name)>0 and string-length($value)>0">
<xsl:element name="{$name}">
<xsl:for-each select="$SETTINGS/google/option">
-----> <xsl:if test="$name = $SETTINGS/google/option/ckild name">
<xsl:attribute name="option"/>
</xsl:if>
</xsl:for-each>
<xsl:value-of select="$value"/>
</xsl:element>
</xsl:if>
<xsl:if test="$butfirst">
<xsl:call-template name="tokenize">
<xsl:with-param name="list" select="$butfirst"/>
<xsl:with-param name="seperator" select="$seperator"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Many Thanks
Tim Dodgson
This stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="SETTINGS" select="/settings"/>
<xsl:template match="/">
<xsl:call-template name="tokenize">
<xsl:with-param name="pString"
select="'color=blue&name=value&size=big'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="pString"/>
<xsl:param name="pSeperator" select="'&'"/>
<xsl:choose>
<xsl:when test="not($pString)"/>
<xsl:when test="contains($pString,$pSeperator)">
<xsl:call-template name="tokenize">
<xsl:with-param name="pString"
select="substring-before($pString, $pSeperator)"/>
<xsl:with-param name="pSeperator" select="$pSeperator"/>
</xsl:call-template>
<xsl:call-template name="tokenize">
<xsl:with-param name="pString"
select="substring-after($pString, $pSeperator)"/>
<xsl:with-param name="pSeperator" select="$pSeperator"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vName"
select="normalize-space(substring-before($pString,'='))"/>
<xsl:variable name="vValue"
select="normalize-space(substring-after($pString,'='))"/>
<xsl:if test="$vName and $vValue">
<xsl:element name="{$vName}">
<xsl:if test="$vName = $SETTINGS/google/option/#from">
<xsl:attribute name="attr">special</xsl:attribute>
</xsl:if>
<xsl:value-of select="$vValue"/>
</xsl:element>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
With this input:
<settings>
<google>
<option from="color"/>
<option from="size"/>
</google>
</settings>
Output:
<color attr="special">blue</color>
<name>value</name>
<size attr="special">big</size>
Note: Node set comparison
I'm transforming my XSLT-stylesheets into documentation, and I want a rich experience within the comment nodes for each code-chunk, therefore I want to convert the following comment and output as xhtml:
String:
# This is a title with __bold__ text and *italic* #
This is just a normal line
- list point with some __bold__
- list point with a "link"[http://www.stackoverflow.com]
Wanted output:
<h1> This is a title with <strong>bold</strong> and <span>italic</span> </h1>
<p>This is a normal line</p>
<ul>
<li>list point with some <strong>bold</strong></li>
<li>list point with a link</li>
</ul>
I tried with a recursive function that uses xsl:analyze-string recursively from a ruleset, but can't find a solution that works really well.
Anyone have done this lately, or is there some frameworks out there that has functions to do this ?
thanx in advance! :)
Edit: Added one dirty example:
<!-- Output comments -->
<xsl:template match="comment()" mode="COMMENT">
<xsl:copy-of select="ips:groupReplace(normalize-space(.),
'
(.*)(\n|\r)(.*),
(.*)\*(.*)\*(.*),
(.*)\*\*(.*)\*\*(.*),
(.*)__(.*)__(.*),
(.*)#(.*)#(.*),
(.*)-(.*)
',
'
br,
span.italic,
span.bold,
strong,
h1,
li
')" />
</xsl:template>
<!-- Initializing the iterateRegex function -->
<xsl:function name="ips:groupReplace">
<xsl:param name="string" as="xs:string" />
<xsl:param name="search" />
<xsl:param name="replace" />
<xsl:variable name="regex" select="tokenize($search, ',')" />
<xsl:variable name="replacements" select="tokenize($replace, ',')" />
<xsl:copy-of select="ips:iterateRegex(count($replacements), $string, $regex, $replacements)" />
</xsl:function>
<!-- Iterate each regex -->
<xsl:function name="ips:iterateRegex">
<xsl:param name="counter" />
<xsl:param name="string" />
<xsl:param name="list_regex" />
<xsl:param name="list_replace" />
<xsl:variable name="newStr">
<xsl:analyze-string select="$string" regex="{normalize-space($list_regex[$counter])}" flags="xm">
<xsl:matching-substring>
<xsl:variable name="cc" select="contains($list_replace[$counter], '.')" />
<xsl:variable name="tag" select="normalize-space(if ($cc) then (substring-before($list_replace[$counter], '.')) else ($list_replace[$counter]))" />
<xsl:copy-of select="regex-group(1)" />
<xsl:choose>
<xsl:when test="normalize-space(regex-group(2)) = ''">
<xsl:element name="{$tag}" />
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$tag}" >
<xsl:if test="$cc">
<xsl:attribute name="class" select="substring-after($list_replace[$counter],'.')" />
</xsl:if>
<xsl:copy-of select="regex-group(2)" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
<xsl:copy-of select="regex-group(3)" />
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:copy-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:variable name="count" select="number($counter) - 1" />
<xsl:choose>
<xsl:when test="$count > 0">
<xsl:copy-of select="ips:iterateRegex($count, $newStr, $list_regex, $list_replace)" />
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$newStr" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
I think you would need a parser. So this stylesheet implements a verbose one:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text" name="block">
<xsl:param name="pString" select="."/>
<xsl:if test="$pString != ''">
<xsl:choose>
<xsl:when test="starts-with($pString,'#')">
<xsl:call-template name="header">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="starts-with($pString,'
')">
<xsl:call-template name="list">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="paragraph">
<xsl:with-param name="pString"
select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template name="header">
<xsl:param name="pString"/>
<xsl:variable name="vInside"
select="substring-before($pString,'#
')"/>
<xsl:choose>
<xsl:when test="$vInside != ''">
<h1>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$vInside"/>
</xsl:call-template>
</h1>
<xsl:call-template name="block">
<xsl:with-param name="pString"
select="substring-after($pString,'#
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="paragraph">
<xsl:with-param name="pString"
select="concat('#',$pString)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="list">
<xsl:param name="pString"/>
<xsl:variable name="vCheckList" select="starts-with($pString,'- ')"/>
<xsl:choose>
<xsl:when test="$vCheckList">
<ul>
<xsl:call-template name="listItem">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</ul>
<xsl:call-template name="block">
<xsl:with-param name="pString">
<xsl:call-template name="afterlist">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="block">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="paragraph">
<xsl:param name="pString"/>
<xsl:choose>
<xsl:when test="contains($pString,'
')">
<p>
<xsl:value-of select="substring-before($pString,'
')"/>
</p>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:value-of select="$pString"/>
</p>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="block">
<xsl:with-param name="pString"
select="substring-after($pString,'
')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="afterlist">
<xsl:param name="pString"/>
<xsl:choose>
<xsl:when test="starts-with($pString,'- ')">
<xsl:call-template name="afterlist">
<xsl:with-param name="pString"
select="substring-after($pString,'
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="listItem">
<xsl:param name="pString"/>
<xsl:if test="starts-with($pString,'- ')">
<li>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring-before(substring($pString,3),'
')"/>
</xsl:call-template>
</li>
<xsl:call-template name="listItem">
<xsl:with-param name="pString"
select="substring-after($pString,'
')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="inline">
<xsl:param name="pString" select="."/>
<xsl:if test="$pString != ''">
<xsl:choose>
<xsl:when test="starts-with($pString,'__')">
<xsl:call-template name="strong">
<xsl:with-param name="pString"
select="substring($pString,3)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="starts-with($pString,'*')">
<xsl:call-template name="span">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="starts-with($pString,'"')">
<xsl:call-template name="link">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($pString,1,1)"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template name="strong">
<xsl:param name="pString"/>
<xsl:variable name="vInside" select="substring-before($pString,'__')"/>
<xsl:choose>
<xsl:when test="$vInside != ''">
<strong>
<xsl:value-of select="$vInside"/>
</strong>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring-after($pString,'__')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'__'"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="span">
<xsl:param name="pString"/>
<xsl:variable name="vInside" select="substring-before($pString,'*')"/>
<xsl:choose>
<xsl:when test="$vInside != ''">
<span>
<xsl:value-of select="$vInside"/>
</span>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring-after($pString,'*')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'*'"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="link">
<xsl:param name="pString"/>
<xsl:variable name="vInside"
select="substring-before($pString,'"')"/>
<xsl:choose>
<xsl:when test="$vInside != ''">
<xsl:call-template name="href">
<xsl:with-param name="pString"
select="substring-after($pString,'"')"/>
<xsl:with-param name="pInside" select="$vInside"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'"'"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="href">
<xsl:param name="pString"/>
<xsl:param name="pInside"/>
<xsl:variable name="vHref"
select="substring-before(substring($pString,2),']')"/>
<xsl:choose>
<xsl:when test="starts-with($pString,'[') and $vHref != ''">
<a href="{$vHref}">
<xsl:value-of select="$pInside"/>
</a>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="substring-after($pString,']')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('"',$pInside,'"')"/>
<xsl:call-template name="inline">
<xsl:with-param name="pString" select="$pString"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
With this input:
<text>
# This is a title with __bold__ text and *italic* #
This is just a normal line
- list point with some __bold__
- list point with a "link"[http://www.stackoverflow.com]
</text>
Output:
<h1> This is a title with
<strong>bold</strong> text and
<span>italic</span>
</h1>
<p>This is just a normal line</p>
<ul>
<li>list point with some
<strong>bold</strong>
</li>
<li>list point with a
link
</li>
</ul>
Note: Look how many templates are similar (they follow a pattern), so these could be parametrized. I didn't do that in this case because there seems to be more questions which need some sort of parser, so by the end of the week I will repost an answer implementing functional parser and parser combinators pattern that make very easy to write parsers (just writing its grammar rules).
Edit: XSLT 2.0 solution. This stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text">
<xsl:param name="pString" select="."/>
<xsl:analyze-string select="$pString"
regex="(#(.*)#
)|((- (.*)
)+)">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(1)">
<h1>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="regex-group(2)"/>
</xsl:call-template>
</h1>
</xsl:when>
<xsl:when test="regex-group(3)">
<ul>
<xsl:call-template name="list">
<xsl:with-param name="pString"
select="regex-group(3)"/>
</xsl:call-template>
</ul>
</xsl:when>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:if test=".!='
'">
<p>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="normalize-space(.)"/>
</xsl:call-template>
</p>
</xsl:if>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template name="list">
<xsl:param name="pString"/>
<xsl:analyze-string select="$pString" regex="- (.*)
">
<xsl:matching-substring>
<li>
<xsl:call-template name="inline">
<xsl:with-param name="pString"
select="regex-group(1)"/>
</xsl:call-template>
</li>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template name="inline">
<xsl:param name="pString" select="."/>
<xsl:analyze-string select="$pString"
regex="(__(.*)__)|(\*(.*)\*)|("(.*)"\[(.*)\])">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(1)">
<strong>
<xsl:value-of select="regex-group(2)"/>
</strong>
</xsl:when>
<xsl:when test="regex-group(3)">
<span>
<xsl:value-of select="regex-group(4)"/>
</span>
</xsl:when>
<xsl:when test="regex-group(5)">
<a href="{regex-group(7)}">
<xsl:value-of select="regex-group(6)"/>
</a>
</xsl:when>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
Output:
<h1> This is a title with
<strong>bold</strong> text and
<span>italic</span>
</h1>
<p>This is just a normal line</p>
<ul>
<li>list point with some
<strong>bold</strong>
</li>
<li>list point with a
link
</li>
</ul>
This transformation (111 lines):
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="my:my"
exclude-result-prefixes="xml xsl xs my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vLines" select="tokenize(., '\n')"/>
<xsl:sequence select="my:parse-lines($vLines)"/>
</xsl:template>
<xsl:function name="my:parse-lines" as="element()*">
<xsl:param name="pLines" as="xs:string*"/>
<xsl:sequence select=
"my:parse-line($pLines, 1, count($pLines))"/>
</xsl:function>
<xsl:function name="my:parse-line" as="element()*">
<xsl:param name="pLines" as="xs:string*"/>
<xsl:param name="pLineNum" as="xs:integer"/>
<xsl:param name="pTotalLines" as="xs:integer"/>
<xsl:if test="not($pLineNum gt $pTotalLines)">
<xsl:variable name="vLine" select="$pLines[$pLineNum]"/>
<xsl:variable name="vLineLength"
select="string-length($vLine)"/>
<xsl:choose>
<xsl:when test=
"starts-with($vLine, '#')
and
ends-with($vLine, '#')
">
<xsl:variable name="vInnerString"
select="substring($vLine, 2, $vLineLength -2)"/>
<h1>
<xsl:sequence select="my:parse-string($vInnerString)"/>
</h1>
<xsl:sequence select=
"my:parse-line($pLines, $pLineNum+1, $pTotalLines)"/>
</xsl:when>
<xsl:when test=
"starts-with($vLine, '- ')
and
not(starts-with($pLines[$pLineNum -1], '- '))
">
<ul>
<li>
<xsl:sequence select="my:parse-string(substring($vLine, 2))"/>
</li>
<xsl:sequence select=
"my:parse-line($pLines, $pLineNum+1, $pTotalLines)"/>
</ul>
</xsl:when>
<xsl:when test="starts-with($vLine, '- ')">
<li>
<xsl:sequence select="my:parse-string(substring($vLine, 2))"/>
</li>
<xsl:sequence select=
"my:parse-line($pLines, $pLineNum+1, $pTotalLines)"/>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:sequence select="my:parse-string($vLine)"/>
</p>
<xsl:sequence select=
"my:parse-line($pLines, $pLineNum+1, $pTotalLines)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:function>
<xsl:function name="my:parse-string" as="node()*">
<xsl:param name="pS" as="xs:string"/>
<xsl:analyze-string select="$pS" flags="x" regex=
'(__(.*?)__)
|
(\*(.*?)\*)
|
("(.*?)"\[(.*?)\])
'>
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(1)">
<strong>
<xsl:sequence select="my:parse-string(regex-group(2))"/>
</strong>
</xsl:when>
<xsl:when test="regex-group(3)">
<span>
<xsl:sequence select="my:parse-string(regex-group(4))"/>
</span>
</xsl:when>
<xsl:when test="regex-group(5)">
<a href="{regex-group(7)}">
<xsl:sequence select="regex-group(6)"/>
</a>
</xsl:when>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>
</xsl:stylesheet>
when applied on this XML document (the provided text complicated with nested constructs and wrapped in an element):
<t># This is a title with __bold__ text and *italic* #
This is just a normal line
- list point with some __bold__
- list point with a __*"link"[http://www.stackoverflow.com]*__</t>
produces the wanted, correct output:
<h1> This is a title with <strong>bold</strong> text and <span>italic</span>
</h1>
<p>This is just a normal line</p>
<p/>
<ul>
<li> list point with some <strong>bold</strong>
</li>
<li> list point with a <strong>
<span>
link
</span>
</strong>
</li>
</ul>
Do note: The RegEx mechanism of XPath 2.0 and XSLT 2.0 is adequate for solving this problem.