Split Attribute using ; as delimiter in XSLT - xslt

How to split an elements using ; as delimiter.my requirement is like below.
input:
<Element1>C:KEK39519US; U:085896395195; A:K39519US; B:S2345843</Element1>
output:
<CustItem>KEK39519US</CustItem>
<UNumber>085896395195</UNumber>
<ANumber>K39519US</ANumber>
<BNumber>S2345843</BNumber>
the input is every time not same.some times it comes like C:KEK39519US; U:085896395195; B:S2345843
some time like this C:KEK39519US; A:K39519US; B:S2345843
sometime like this U:085896395195; A:K39519US;
sometime like this C:KEK39519US; U:085896395195; A:K39519US;

To solve this in XSLT 1.0 you may need a named template which recursively calls itself. The template will process of the string before the first semi-colon, and output the element accordingly. It will then recursively call itself with the remaining part of the string after this semi-colon (if there is one)
Here is the full XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Element1">
<xsl:call-template name="outputElements">
<xsl:with-param name="list" select="." />
</xsl:call-template>
</xsl:template>
<xsl:template name="outputElements">
<xsl:param name="list"/>
<xsl:variable name="first" select="normalize-space(substring-before(concat($list, ';'), ';'))"/>
<xsl:variable name="remaining" select="normalize-space(substring-after($list, ';'))"/>
<xsl:call-template name="createElement">
<xsl:with-param name="element" select="$first" />
</xsl:call-template>
<!-- If there are still elements left in the list, call the template recursively -->
<xsl:if test="$remaining">
<xsl:call-template name="outputElements">
<xsl:with-param name="list" select="$remaining"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="createElement">
<xsl:param name="element"/>
<xsl:variable name="elementName">
<xsl:choose>
<xsl:when test="substring-before($element, ':') = 'C'">CustItem</xsl:when>
<xsl:otherwise><xsl:value-of select="concat(substring-before($element, ':'), 'Number')" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$elementName}">
<xsl:value-of select="substring-after($element, ':')" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When applied to you XML, the following is output
<CustItem>KEK39519US</CustItem>
<UNumber>085896395195</UNumber>
<ANumber>K39519US</ANumber>
<BNumber>S2345843</BNumber>
Note the use of Attribute Value Templates in specifying the name of each new element.

Related

XSLT: Concatenate and select the element

XSLT:
Written my XSLT like this but I'm not getting required output as below that means I want to loop through each record and get frontimagefilename and rearimagefilename for each check.
<xsl:template name="ChequeDetailsTemplate">
<xsl:param name ="vItemcurrIndx" />
<xsl:variable name ="x" select ="$allRecs[$vItemcurrIndx]/Content/*" />
<xsl:call-template name="loop">
<xsl:with-param name="i" select="1"/>
<xsl:with-param name="limit" select="$x/NumberOfCheques"/>
<xsl:with-param name="vItemcurrIndx" select="position()"></xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="i"/>
<xsl:param name="limit"/>
<xsl:param name ="vItemcurrIndx" />
<xsl:variable name ="y" select ="$allRecs[$vItemcurrIndx]/Content/*" />
<xsl:if test="$i <= $limit">
<Check>
<CheckAmount><xsl:value-of select="concat(./CourtesyAmount,$i)"/> </CheckAmount>
<FrontImage><xsl:value-of select="concat(./FrontImageFilename,$i)"/></FrontImage>
<RearImage><xsl:value-of select="concat(./RearImageFilename,$i)"/></RearImage>
</Check>
<xsl:call-template name="loop">
<xsl:with-param name="i" select="$i+1"/>
<xsl:with-param name="limit" select="$limit"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Output looking for:
-<Check><CheckAmount>300</CheckAmount><Codeline><006474< :########## 12345678<</Codeline><FrontImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135318f001.tif</FrontImage><RearImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135318r001.tif</RearImage><CourtesyAmount>300</CourtesyAmount></Check>-<Check><CheckAmount>300</CheckAmount><Codeline><006474< :########## 12345678<</Codeline><FrontImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135320f002.tif</FrontImage><RearImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135320r002.tif</RearImage><CourtesyAmount>300</CourtesyAmount></Check>
But the output which im getting now is:
-<Check><CheckAmount>1</CheckAmount><FrontImage>1</FrontImage><RearImage>1</RearImage></Check>-<Check><CheckAmount>2</CheckAmount><FrontImage>2</FrontImage><RearImage>2</RearImage></Check>
If I am guessing (!) correctly, your input looks something like this:
<ChqDepAppSrvChequeScanComplete>
<CourtesyAmount1>100</CourtesyAmount1>
<FrontImageFilename1 t="String">R:\bin_simulated\Images\TempChequeImages\100f001.bmp</FrontImageFilename1>
<RearImageFilename1 t="String">R:\bin_simulated\Images\TempChequeImages\100r001.bmp</RearImageFilename1>
<RefuseReason1 t="String">None</RefuseReason1>
<CourtesyAmount2>200</CourtesyAmount2>
<FrontImageFilename2 t="String">R:\bin_simulated\Images\TempChequeImages\200f002.bmp</FrontImageFilename2>
<RearImageFilename2 t="String">R:\bin_simulated\Images\TempChequeImages\200r002.bmp</RearImageFilename2>
<RefuseReason2 t="String">None</RefuseReason2>
<NumberOfCheques t="Int32">2</NumberOfCheques>
</ChqDepAppSrvChequeScanComplete>
Your mistake is in the way you have built this expression:
<xsl:value-of select="concat(./CourtesyAmount,$i)"/>
There is no <CourtesyAmount> node in your input, so you are concatenating nothing and $i, resulting in the current value of $i.
What you really want to do here is get the value from a node whose name is the concatenation of the string "CourtesyAmount" and the current value of $i. Something like:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/ChqDepAppSrvChequeScanComplete">
<output>
<xsl:call-template name="loop">
<xsl:with-param name="limit" select="NumberOfCheques"/>
</xsl:call-template>
</output>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="i" select="1"/>
<xsl:param name="limit"/>
<xsl:if test="$i <= $limit">
<Check>
<CheckAmount>
<xsl:value-of select="*[local-name()=concat('CourtesyAmount', $i)]"/>
</CheckAmount>
<FrontImage>
<xsl:value-of select="*[local-name()=concat('FrontImageFilename', $i)]"/>
</FrontImage>
<RearImage>
<xsl:value-of select="*[local-name()=concat('RearImageFilename', $i)]"/>
</RearImage>
</Check>
<xsl:call-template name="loop">
<xsl:with-param name="i" select="$i+1"/>
<xsl:with-param name="limit" select="$limit"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
which when applied to the above input will result in:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<Check>
<CheckAmount>100</CheckAmount>
<FrontImage>R:\bin_simulated\Images\TempChequeImages\100f001.bmp</FrontImage>
<RearImage>R:\bin_simulated\Images\TempChequeImages\100r001.bmp</RearImage>
</Check>
<Check>
<CheckAmount>200</CheckAmount>
<FrontImage>R:\bin_simulated\Images\TempChequeImages\200f002.bmp</FrontImage>
<RearImage>R:\bin_simulated\Images\TempChequeImages\200r002.bmp</RearImage>
</Check>
</output>
Note:
The real problem here is the input. It should not be formatted this way. Things would be much easier if the person ahead of you did a proper job and supplied you with a well-structured XML, for example:
<ChqDepAppSrvChequeScanComplete>
<NumberOfCheques t="Int32">2</NumberOfCheques>
<Cheque>
<CourtesyAmount>100</CourtesyAmount>
<FrontImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\100f001.bmp</FrontImageFilename>
<RearImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\100r001.bmp</RearImageFilename>
<RefuseReason t="String">None</RefuseReason>
</Cheque>
<Cheque>
<CourtesyAmount>200</CourtesyAmount>
<FrontImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\200f002.bmp</FrontImageFilename>
<RearImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\200r002.bmp</RearImageFilename>
<RefuseReason t="String">None</RefuseReason>
</Cheque>
</ChqDepAppSrvChequeScanComplete>
with none of this numbered nodes nonsense.

Varying amount of iterations in XSL recursive loop within a for loop

I'm generating a CSV file from an XML using XSL. The XML contains Main elements with child elements Tags, which in turn contain varying amounts of child elements Tag. A part of the XML looks for example like this:
<Main>
<Tags>
<Tag>tag1</Tag>
<Tag>tag2</Tag>
<Tag>tag3</Tag>
</Tags>
</Main>
<Main>
<Tags>
<Tag>tag1</Tag>
<Tag>tag2</Tag>
<Tag>tag3</Tag>
<Tag>tag4</Tag>
<Tag>tag5</Tag>
<Tag>tag6</Tag>
</Tags>
</Main>
In the XSL I have a for each loop that goes through all my Main elements of my XML file. I want to print the values for all the Tag elements. I do this in another for-each loop which is inside the major loop. However, I always want to iterate 10 times, regardless of the amount of Tag elements. I want to print some text in each of the remaining iterations when I have exceeded the amount of printable Tag.
This is the output I'm after:
tag1,tag2,tag3,1,1,1,1,1,1,1,
tag1,tag2,tag3,tag4,tag5,tag6,1,1,1,1,
After the Tag for each loop, I'm calling a template, providing a variable with the amount of Tag in Tags. I then want the template to call itself recursively until it has done the varying amount of remaining iterations for the Tag elements of the current Main element. The amount of Tag elements changes with each Main iteration, which I suspect is a problem in my current solution (which causes my transformation software, Notepad++ with XML Tools, to crash):
<xsl:template match="/">
<xsl:for-each select="Main">
<xsl:for-each select="Tags/Tag">
<xsl:value-of select="Tag"/>
<xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:call-template name="repeatable">
<xsl:with-param name="tagamount" select="count(Tags/*)"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template name="repeatable">
<xsl:param name="tagamount"/>
<xsl:param name="index" select="0" />
<xsl:text>1,</xsl:text>
<xsl:if test="not($index = 10-$tagamount)">
<xsl:call-template name="repeatable">
<xsl:with-param name="index" select="$index + 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
Does anyone have any idea if it's possible to do this type of varying iteration, or am I out of luck?
Edit:
I managed to solve it. The problem was I had forgotten to pass on the variable tagamount with each recursive call. See my solution further below.
I couldn't wrap my head around your code. How about something simpler?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="sep" select="','"/>
<xsl:variable name="LF" select="'
'"/>
<xsl:variable name="filler" select="'1,2,3,4,5,6,7,8,9,10'"/>
<xsl:template match="/">
<xsl:for-each select="rt/Main/Tags">
<xsl:for-each select="Tag">
<xsl:value-of select="concat(., $sep)"/>
</xsl:for-each>
<xsl:value-of select="substring($filler, 2*count(Tag)+1)"/>
<xsl:value-of select="$LF"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note:
1. Your XML is missing a root element: I am using "rt" as a placeholder.
2. For testing purposes, I have changed "1,1,1,..." into "1,2.3...".
Here is one way to do it.
This XSLT stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<!-- Sets the number of iterations per Tags element. -->
<xsl:variable name="maximum" select="10"/>
<!-- Matches all the Tags elements and calls a recursive template, intializing the count to 1. -->
<xsl:template match="//Tags">
<xsl:call-template name="output-tags">
<xsl:with-param name="count" select="1"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
</xsl:template>
<!-- A recursive template that will repeat itself until its count reaches the maximum value.
If the count is equal to or less then the number of Tag elements inside the current Tags
element, then find the Tag element in the count position and print its value. Otherwise,
print 1. -->
<xsl:template name="output-tags">
<xsl:param name="count"/>
<xsl:if test="$count <= $maximum">
<xsl:choose>
<xsl:when test="$count <= count(Tag)">
<xsl:value-of select="Tag[count(preceding-sibling::Tag) = $count - 1]"/>
<xsl:text>,</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>1,</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="output-tags">
<xsl:with-param name="count" select="$count + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
produces the following output when applied to your example input XML:
tag1,tag2,tag3,1,1,1,1,1,1,1,
tag1,tag2,tag3,tag4,tag5,tag6,1,1,1,1,
Thanks for the answers!
I managed to solve it right after I posted. The problem was I had forgotten to send the tagamount variable with the recursive call. After adding it, it works. The repeatable template then looks like this:
<xsl:param name="tagamount"/>
<xsl:param name="index" select="0" />
<xsl:text>1,</xsl:text>
<xsl:if test="not($index = 10-$tagamount)">
<xsl:call-template name="repeatable">
<xsl:with-param name="tagamount" select="$tagamount"/> <-----------
<xsl:with-param name="index" select="$index + 1" />
</xsl:call-template>
</xsl:if>

Transformation in XSLT with Recursive Nested Templates

I have this Entry Input XML
<BusinessInteractionTypes>
<BusinessInteractionType>
<TypeId>123</TypeId>
<Name>Foo</Name>
<Description>XSLT</Description>
</BusinessInteractionType>
...a lot BusinessInteractionType Elements
</BusinessInteractionTypes>
But a have a lot of transformation (to SOA case), I need Transform this input using XSLT to:
<businessInteractionTypes>
<businessInteractionType>
<BusinessInteractionType> <!-- Just first input node -->
<TypeId/>
</BusinessInteractionType>
</businessInteractionType>
<businessInteractionTypeHas> <!-- Recursive interaction began -->
<businessInteractionType>
<BusinessInteractionType>
<TypeId />
</BusinessInteractionType>
<businessInteractionTypeHas>
... recursive
</businessInteractionType>
</businessInteractionTypeHas>
</businessInteractionTypes>
Srs, I fix the issue... follow
Thanks all
<?xml version="1.0" encoding="UTF-8"?>
<xsl:template match="cus:BusinessInteractionType">
<xsl:call-template name="Master">
<xsl:with-param name="nodeD" select="bus1:BusinessInteractionType"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="Master">
<xsl:param name="nodeD" select="bus1:BusinessInteractionType"/>
<cas:businessInteractionTypes>
<cas:businessInteractionType>
<xsl:if test="bus1:BusinessInteractionType[position() = 1]">
<bus3:BusinessInteractionType>
<bus1:TypeId>
<xsl:value-of select="//bus1:TypeId"/>
</bus1:TypeId>
</bus3:BusinessInteractionType>
</xsl:if>
<xsl:call-template name="MasterNested">
<xsl:with-param name="current_node" select="bus1:BusinessInteractionType[position()>1]"/>
</xsl:call-template>
</cas:businessInteractionType>
</cas:businessInteractionTypes>
</xsl:template>
<xsl:template name="MasterNested">
<xsl:param name="current_node" select="bus1:BusinessInteractionType"/>
<xsl:if test="$current_node">
<cas:businessInteractionTypeHas>
<cas:businessInteractionType>
<bus3:TypeId>
<xsl:value-of select="$current_node//bus1:TypeId"/>
</bus3:TypeId>
<bus3:Name>
<xsl:value-of select="$current_node//bus1:Name"/>
</bus3:Name>
<bus3:Description>
<xsl:value-of select="$current_node//bus1:Description"/>
</bus3:Description>
</cas:businessInteractionType>
<xsl:call-template name="MasterNested">
<xsl:with-param name="current_node" select="$current_node/following-sibling::node()"/>
</xsl:call-template>
</cas:businessInteractionTypeHas>
</xsl:if>
</xsl:template>

How to show a character n times in XSLT?

I have a template with a parameter. How can I insert a tab character n times?
n is the value of the parameter.
In XSLT 2.0:
<xsl:for-each select="1 to $count"> </xsl:for-each>
(Sadly though, I suspect that if you were using XSLT 2.0 you wouldn't need to ask the question).
Another technique often used with XSLT 1.0 is the hack:
<xsl:for-each select="//*[position() <= $count]"> </xsl:for-each>
which works provided the number of elements in your source document is greater than the number of tab characters you want to output.
Just call it recursively; output a tab, then call the same template again with n-1 passed in, if n > 1.
<xsl:template name="repeat">
<xsl:param name="output" />
<xsl:param name="count" />
<xsl:if test="$count > 0">
<xsl:value-of select="$output" />
<xsl:call-template name="repeat">
<xsl:with-param name="output" select="$output" />
<xsl:with-param name="count" select="$count - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
As has been pointed out, this example will actually output a minimum of one. In my experience where the output is whitespace, it's usually needed. You can adapt the principle of a recursive template like this any way you see fit.
This seems the simplest and most flexible to me.
For XSLT 1.0 (or perhaps 1.1).
<xsl:variable name="count">10</xsl:variable>
<xsl:variable name="repeat"><xsl:text> </xsl:text></xsl:variable>
<xsl:sequence select="string-join((for $i in 1 to $count return $repeat),'')"/>
Of course the count variable is where you assign your n parameter.
I used the variable repeat to hold the tab character, but you could just replace the $repeat with the tab character in single quotes in the sequence element. Note: This variable can be of a length greater than 1, which creates a whole bunch of possibilities.
It does not use recursion, so it won't run into a recursion limit.
I don't know the maximum value you can use for count, but I tested it up to 10,000.
Globally define a long enough array of tabs:
<xsl:variable name="TABS" select="' '" />
Then use like this:
<xsl:value-of select="fn:substring($TABS, 1, fn:number($COUNT))" />
(XSLT 1.0)
<xsl:template name="tabs">
<xsl:param name="n"/>
<xsl:if test="$n > 0"> <!-- When n = 0, output nothing. -->
<xsl:call-template name="tabs"> <!-- Recursive call: call same template... -->
<xsl:with-param name="n" select="$n - 1"/> <!-- ... for writing n - 1 tabs. -->
</xsl:call-template>
<xsl:text> </xsl:text> <!-- Add one tab character. -->
</xsl:if>
</xsl:template>
Example usage:
<xsl:call-template name="tabs">
<xsl:with-param name="n" select="3"/>
</xsl:call-template>
I've discovered an LGPL-licensed library for doing this called functx, as I was sure someone had to have already done this... This is a "standard library" type XSLT library, which contains a function called repeat-string. From the docs:
The functx:repeat-string function returns a string consisting of a given number of copies of $stringToRepeat concatenated together.
Where I use it like this in my code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:functx="http://www.functx.com">
<xsl:import href="../buildlib/functx-1.0.xsl"/>
<xsl:output omit-xml-declaration="yes" />
<xsl:variable name="INDENT" select="' '" />
....
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="data-pusher-properties">
<xsl:for-each select="property">
<xsl:choose>
...
<xsl:when test="boolean(#value = '${pusher.notifications.server}')">
<xsl:value-of select="functx:repeat-string($INDENT, #indent)" />
<xsl:text>"</xsl:text>
<xsl:value-of select="#name" />
<xsl:text>": </xsl:text>
<xsl:text>"</xsl:text>
<xsl:value-of select="$pusher.notifications.email.server" />
<xsl:text>"\
</xsl:text>
</xsl:when>
...
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
So for printing a tab character n times, call it like this:
<xsl:value-of select="functx:repeat-string(' ', n)" />
I know this question is old, but I hope this can still help someone.
Documentation for the repeat-string function

xslt: create new node from mixed content within parent element

I'm trying to write a template that will grab a mixture of text nodes and elements within a parent element and create a new node. I've done a lot of searching and couldn't find what I was looking for...so hopefully I'm not asking to basic a question.
Here is a sample of xml I want to transform:
<?xml version="1.0"?>
<root>
<para>Here is some text that will ask users to enter a <rule-line/> [<emph type="it">date</emph>], and maybe their <rule-line/> [<emph type="it">name</emph>]. The text could come in different [<emph type="it">order</emph>] <rule-line/>, and their could be any number of instances.</para>
</root>
I want to group the bracketed text and the rule into a new element like so:
<entry>[<emph type"it">date</emph>]</entry>
I have a template that can identify the text I want to change, and I can change it, but I don't know how to add the text I want to the result tree and omit the old text.
Here are the relevant templates:
<xsl:template match="para">
<xsl:for-each select="* | text()">
<xsl:choose>
<xsl:when test="self::rule-line and following-sibling::node()[1][starts-with(., ' [')] and string(node-name(following-sibling::node()[2])) = 'emph' and following-sibling::node()[3][starts-with(., ']')]">
<xsl:comment>made match</xsl:comment>
<xsl:call-template name="codeEntry">
<xsl:with-param name="rule" select="."/>
<xsl:with-param name="openBracket" select="following-sibling::node()[1]"/>
<xsl:with-param name="emphTag" select="following-sibling::node()[2]"/>
<xsl:with-param name="closeBracketString" select="following-sibling::node()[3]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template name="codeEntry">
<xsl:param name="rule"/>
<xsl:param name="openBracket"/>
<xsl:param name="emphTag"/>
<xsl:param name="closeBracketString"/>
<entry>
<xsl:copy-of select="$openBracket"/>
<xsl:copy-of select="$emphTag"/>
<xsl:text>] </xsl:text>
</entry>
<xsl:value-of select="substring-after($closeBracketString, ']')"/>
</xsl:template>
Obviously, the when statement grabs a group of nodes, but when each node goes through the otherwise block it gets copied to the result tree. I'm not really sure how to handle this since the para could have any number of these node groupings in any order, or none. (Once I figure this out I'll add another when block that deals with the bracketed text before the rule)
I think creating a variable that tells the template to ignore the node is the way to go...but I'm a little foggy on the immutable variables and their scope...
I was also trying to think of a way I could try to do this recursively...but that would require adding a start tag at one point, an end tag in another, or no tag if the node being processed is in the middle of the sequence...and I know that can get weird in xslt.
Anyone run into this type of situation before?
thanks,
jason
any ideas
Just for fun (What a mess of a schema!), this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|#*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="rule-line"/>
<xsl:template match="emph">
<entry>
<xsl:text>[</xsl:text>
<xsl:call-template name="identity"/>
<xsl:text>]</xsl:text>
</entry>
</xsl:template>
<xsl:template match="text()[normalize-space()='[']
[following-sibling::*[1][self::emph]] |
text()[normalize-space()=']']
[preceding-sibling::*[1][self::emph]]"
priority="1"/>
<xsl:template match="text()[starts-with(normalize-space(),']')]
[preceding-sibling::*[1][self::emph]]">
<xsl:value-of select="substring-after(.,']')"/>
</xsl:template>
<xsl:template match="text()[substring(normalize-space(),
string-length(normalize-space()),
1) = '[']
[following-sibling::*[1][self::emph]]">
<xsl:call-template name="crop-both">
<xsl:with-param name="pString" select="concat(']',.)"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="text()[starts-with(normalize-space(),']')]
[substring(normalize-space(),
string-length(normalize-space()),
1) = '[']
[preceding-sibling::*[1][self::emph]]
[following-sibling::*[1][self::emph]]"
priority="1" name="crop-both">
<xsl:param name="pString" select="."/>
<xsl:variable name="vReverse">
<xsl:call-template name="reverse">
<xsl:with-param name="pString"
select="substring-after(.,']')"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="reverse">
<xsl:with-param name="pString"
select="substring-after($vReverse,'[')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="pString"/>
<xsl:if test="$pString!=''">
<xsl:call-template name="reverse">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
<xsl:value-of select="substring($pString,1,1)"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<root>
<para>Here is some text that will ask users to enter a <entry>[<emph type="it">date</emph>]</entry>, and maybe their <entry>[<emph type="it">name</emph>]</entry>. The text could come in different <entry>[<emph type="it">order</emph>]</entry>, and their could be any number of instances.</para>
</root>