retrieving current node in sequence when looping over this sequence in an xpath predicate - xslt-2.0

I need to convert this input xml:
<Problems>
<problem location="engineroom/boiler/nozzle"/>
<problem location="engineroom/boiler/nozzle/leak"/>
<problem location="office/printer/paper"/>
<problem location="office/printer/paper/empty/A4"/>
<problem location="office/printer/paper/empty/A3"/>
<problem location="garage/truck/window"/>
<problem location="garage/truck/window/crack"/>
</Problems>
to this output xml:
<Problems>
<problem ignore="leak"/>
<problem ignore="leak"/>
<problem ignore="empty"/>
<problem ignore="empty"/>
<problem ignore="empty"/>
<problem location="garage/truck/window"/>
<problem location="garage/truck/window/crack"/>
</Problems>
My current code involves hardcoding the items of interest ('leak', 'empty') and its parent element ('nozzle', 'paper'):
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="problem[contains(#location, 'leak')]
| problem[ends-with(#location, 'nozzle')][contains(following-sibling::problem[1]/#location, 'nozzle/leak')]">
<xsl:apply-templates select="." mode="display">
<xsl:with-param name="location" select="'leak'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="problem[contains(#location, 'empty')]
| problem[ends-with(#location, 'paper')][contains(following-sibling::problem[1]/#location, 'paper/empty')]">
<xsl:apply-templates select="." mode="display">
<xsl:with-param name="location" select="'empty'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="problem[contains(#location, 'burnt')]
| problem[ends-with(#location, 'PCB')][contains(following-sibling::problem[1]/#location, 'PCB/burnt')]">
<xsl:apply-templates select="." mode="display">
<xsl:with-param name="location" select="'burnt'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="problem" mode="display">
<xsl:param name="location"/>
<problem ignore="{$location}"/>
</xsl:template>
What I've tried is the following. But I don't know how to pass the current node in the sequence (e.g. 'leak') to the "display" template. Also, please let me know if there's a better way than this ugly matching expression. Thanks!
<xsl:variable name="issues">
<issue name="leak" parent="nozzle"/>
<issue name="empty" parent="paper"/>
<issue name="burnt" parent="PCB"/>
</xsl:variable>
<xsl:template match="problem[true() = (for $i in $issues/issue return if(
contains(#location,$i/#name)
or ends-with(#location, $i/parent) and contains(following-sibling::problem[1]/#location,$i/#name)
) then true() else false())]">
<xsl:apply-templates select="." mode="display">
<xsl:with-param name="location" select="'???'"/>
</xsl:apply-templates>
</xsl:template>

This XSLT 2.0 transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kIgnorable" match="problem" use=
"generate-id(current())
[$pIgnorable/p[contains(current()/#location, concat('/', #part, '/', issue))]]"/>
<xsl:param name="pIgnorable">
<p part="nozzle">
<issue>leak</issue>
</p>
<p part="paper">
<issue>empty</issue>
</p>
<p part="PCB">
<issue>burnt</issue>
</p>
</xsl:param>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="problem
[
for $part in tokenize(#location, '/')[last()][. = $pIgnorable/p/#part],
$issue in $pIgnorable/p[#part = $part]/issue
return
following-sibling::problem[1]
[
contains(#location, concat('/', $part, '/', $issue))
]
]">
<xsl:variable name="vPart" select="tokenize(#location, '/')[last()]"/>
<xsl:variable name="vIssue" select=
"substring-before(concat(following-sibling::problem[1]
/substring-after(#location,
concat('/', $vPart, '/')), '/'), '/')
"/>
<problem ignore="{$vIssue}"/>
</xsl:template>
<xsl:template match="problem[key('kIgnorable', generate-id())]">
<xsl:variable name="vIgnoreRule"
select="$pIgnorable/p[contains(current()/#location,
concat('/', #part, '/', issue))]"/>
<problem ignore="{$vIgnoreRule/issue}"/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<Problems>
<problem location="engineroom/boiler/nozzle"/>
<problem location="engineroom/boiler/nozzle/leak"/>
<problem location="office/printer/paper"/>
<problem location="office/printer/paper/empty/A4"/>
<problem location="office/printer/paper/empty/A3"/>
<problem location="garage/truck/window"/>
<problem location="garage/truck/window/crack"/>
</Problems>
produces the wanted, correct result:
<Problems>
<problem ignore="leak"/>
<problem ignore="leak"/>
<problem ignore="empty"/>
<problem ignore="empty"/>
<problem ignore="empty"/>
<problem location="garage/truck/window"/>
<problem location="garage/truck/window/crack"/>
</Problems>

I am not sure I have understood the problem but the algorithm presented seems to be implementable in XSLT 3 as
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="relations" as="map(xs:string, xs:string)"
select="map { 'leak' : 'nozzle', 'empty' : 'paper' }"/>
<xsl:variable name="relation-keys" select="map:keys($relations)"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="problem[some $key in $relation-keys satisfies contains(#location, $key)]">
<xsl:apply-templates select="." mode="display">
<xsl:with-param name="location" select="$relation-keys[contains(current()/#location, .)]"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="problem[some $key in $relation-keys
satisfies (ends-with(#location, $relations($key)) and contains(following-sibling::problem[1]/#location, $key))]">
<xsl:apply-templates select="." mode="display">
<xsl:with-param name="location" select="$relation-keys[ends-with(current()/#location, $relations(.)) and contains(current()/following-sibling::problem[1]/#location, .)]"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="problem" mode="display">
<xsl:param name="location"/>
<problem ignore="{$location}"/>
</xsl:template>
</xsl:stylesheet>

Related

How can grouped the TEXT/ELEMENT by xsl:for-each-group element in XSLT 2.0

INPUT XML:
<overline-start id="tie1" specific-use="tie-bar"/>PtCl<sub>2</sub>(P((CH<sub>2</sub>)<sub><italic toggle="yes">n</italic></sub>)<sub>3</sub><overline-end rid="tie1"/>
EXPECTED XML:
<overline id="tie1" specific-use="tie-bar">PtCl<sub>2</sub>(P((CH<sub>2</sub>)<sub><italic toggle="yes">n</italic></sub>)<sub>3</sub></overline>
MY XSLT 2.0 Code:
<xsl:template match="overline-start">
<xsl:for-each-group select="self::overline-start" group-adjacent="self::overline-start[following-sibling::overline-end]">
<xsl:for-each select="current-group()">
<overline>
<xsl:apply-templates select="#*"/>
<xsl:copy-of select="current-group()"/>
</overline>
</xsl:for-each>
As you mentioned the requirement in comments, I tried it #Martin Honnen 's way:
Assuming input as:
<?xml version="1.0" encoding="UTF-8"?>
<p>
<overline-start id="tie1" specific-use="tie-bar"/>PtCl<sub>2</sub>(P((CH<sub>2</sub>)<sub><italic toggle="yes">n</italic></sub>)<sub>3</sub><overline-end rid="tie1"/>
</p>
A 2.0 solution can be:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="no" />
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<overline>
<xsl:for-each-group select="* | text()" group-starting-with="overline-start">
<xsl:for-each-group select="current-group()" group-ending-with="overline-end">
<xsl:apply-templates select="#*" />
<xsl:sequence select="(current-group() except .) [position() != last()]" />
</xsl:for-each-group>
</xsl:for-each-group>
</overline>
</xsl:template>
</xsl:stylesheet>
http://xsltfiddle.liberty-development.net/bnnZW8/1

Extracting value from External xml based on some rule in xsl

This is input xml input.xml
<root>
<bodytext>
<remotelink refptid="HKBL1.0001.lohk.CAP65">some text</remotelink>
<remotelink refptid="HKBL1.0001.lohk.CAP199999">some text</remotelink>
</bodytext>
</root>
This is Prop.xml
<?xml version="1.0" encoding="utf-8"?>
<properties>
<code dpsi="0BZG" docid="asdww">HKBL1.0001.lohk.CAP65</code>
<code dpsi="0BZH" docid="navin">HKBL1.0002.aohk.CAP383</code>
<code no="3">345</code>
</properties>
This is desired output
<root>
<bodytext>
<remotelink refptid="HKBL1.0001.lohk.CAP65" dpsi="0BZG" docid="asdww">some text</remotelink>
<remotelink refptid="HKBL1.0001.lohk.CAP199999">some text</remotelink>
</bodytext>
</root>
IF prop.xml code/text matches remotelink/#refptid than copy attribute of prop.xml to remotelink otherwise no changes in remotelink.
This is the XSLT I have written. So far I am not getting result for unmatched condition:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlsx="http://www.stylusstudio.com/XSLT/XLSX" xmlns:spml="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:saxon="http://saxon.sf.net/" version="2.0">
<xsl:template match="#*|node()" name="root">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="remotelink[#service='DOC-ID']" name="t-remote">
<xsl:variable name="refptid" select="./#refpt"/>
<xsl:variable name="path" select="doc('file:/C:/Users/DalalNS/Desktop/xslt/prop.xml')"/>
<xsl:for-each select="$path/properties/code">
<xsl:choose>
<xsl:when test="./text()=$refptid">
<xsl:element name="remotelink">
<xsl:attribute name="DOC-ID" select="./#docid"/>
<xsl:attribute name="dpsi" select="./#dpsi"/>
<xsl:attribute name="refpt" select="$refptid"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:if test="./#docrefid"/>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="remotelink[#service='DOC-ID']"> will never never be triggered, there's no service attribute on the <remotelink> element. Moreover, you don't retrieve the correct attribute (refpt instead of refptid)
This XSL transformation should do the job:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlsx="http://www.stylusstudio.com/XSLT/XLSX" xmlns:spml="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:saxon="http://saxon.sf.net/" version="2.0">
<xsl:variable name="props.doc" select="doc('file:/C:/Users/DalalNS/Desktop/xslt/prop.xml')/properties" />
<xsl:template match="#*|node()" name="root">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="remotelink" name="t-remote">
<xsl:variable name="refptid" select="./#refptid"/>
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:if test="$props.doc/code[. = $refptid]">
<xsl:apply-templates select="$props.doc/code[. = $refptid]/#*"/>
</xsl:if>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
This is the result of the transformation:
<?xml version="1.0" encoding="UTF-8"?><root>
<bodytext>
<remotelink refptid="HKBL1.0001.lohk.CAP65" dpsi="0BZG" docid="asdww">some text</remotelink>
<remotelink refptid="HKBL1.0001.lohk.CAP199999">some text</remotelink>
</bodytext>
</root>
I would simply define a global parameter or variable <xsl:variable name="path" select="doc('file:/C:/Users/DalalNS/Desktop/xslt/prop.xml')"/>, then set up a key <xsl:key name="prop" match="code" use="."/>, and then use that in a template
<xsl:template match="remotelink[key('prop', #refptid, $path)]">
<xsl:copy>
<xsl:copy-of select="key('prop', #refptid, $path)/#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
that, together with your first template, should suffice.

limiting variable with names to 127 chars, keeping whole names

I have this xml:
<Artists>
<Name>Name1</Name>
<Name>Name2</Name>
<Name>Name3</Name>
<Name>Name4</Name>
<Name>Name5</Name>
...
<Name>Namex</Name>
</Artists>
I don't know how long the list is.
And I want to end up with something like:
<Limit_Artists>Name1; Name2; Name3; Name4; Name5; Name(n)</Limit_Artists>
But where the total length of must not exeed 127 chars and the last name must not "be split" in the middle. In other words I just need as many whole names from the top of the list, that fits within 127 chars.
I can put the names in a variable all together. but how can I stop before 127 chars?
best regards..
Not efficient but
<xsl:template match="Artists">
<Limit_Artists>
<xsl:value-of select="Name[string-length(string-join((preceding-sibling::Name, .), '')) le 127]" separator="; "/>
</Limit_Artists>
</xsl:template>
should do.
A complete example is
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:param name="limit" as="xs:integer" select="127"/>
<xsl:template match="Artists">
<Limit_Artists>
<xsl:value-of select="Name[string-length(string-join((preceding-sibling::Name, .), '')) le $limit]" separator="; "/>
</Limit_Artists>
</xsl:template>
</xsl:stylesheet>
Note that the code counts only the contents of Name elements but outputs them concatenated by ; so the output could that way be longer than 127. If you want to restrict the output to 127 characters then you have to change the string-join call to use
<xsl:template match="Artists">
<Limit_Artists>
<xsl:value-of select="Name[string-length(string-join((preceding-sibling::Name, .), '; ')) le 127]" separator="; "/>
</Limit_Artists>
</xsl:template>
respectively the full example to
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:param name="limit" as="xs:integer" select="127"/>
<xsl:template match="Artists">
<Limit_Artists>
<xsl:value-of select="Name[string-length(string-join((preceding-sibling::Name, .), '; ')) le $limit]" separator="; "/>
</Limit_Artists>
</xsl:template>
</xsl:stylesheet>
Or you could use sibling recursion along the following sibling axis, collecting the length of items until you hit the length you want to limit the output to. A complete example is
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:param name="limit" as="xs:integer" select="127"/>
<xsl:template match="Artists">
<Limit_Artists>
<xsl:apply-templates select="Name[1]">
<xsl:with-param name="limit" select="$limit"/>
</xsl:apply-templates>
</Limit_Artists>
</xsl:template>
<xsl:template match="Artists/Name">
<xsl:param name="limit" as="xs:integer"/>
<xsl:param name="length" as="xs:integer" select="0"/>
<xsl:variable name="new-length" select="$length + string-length()"/>
<xsl:if test="$new-length le $limit">
<xsl:value-of select="if ($length eq 0) then . else concat('; ', .)"/>
<xsl:apply-templates select="following-sibling::Name[1]">
<xsl:with-param name="limit" select="$limit"/>
<xsl:with-param name="length" select="$new-length"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The above sample also only counts the Name characters but concatenates them so it would need to be adapted as well if you want to restrict the output to 127 characters. That could be done as follows:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:param name="limit" as="xs:integer" select="127"/>
<xsl:param name="sep" as="xs:string" select="'; '"/>
<xsl:template match="Artists">
<Limit_Artists>
<xsl:apply-templates select="Name[1]">
<xsl:with-param name="limit" select="$limit"/>
</xsl:apply-templates>
</Limit_Artists>
</xsl:template>
<xsl:template match="Artists/Name">
<xsl:param name="limit" as="xs:integer"/>
<xsl:param name="length" as="xs:integer" select="0"/>
<xsl:variable name="new-length" select="if ($length eq 0) then string-length() else $length + string-length($sep) + string-length()"/>
<xsl:if test="$new-length le $limit">
<xsl:value-of select="if ($length eq 0) then . else concat($sep, .)"/>
<xsl:apply-templates select="following-sibling::Name[1]">
<xsl:with-param name="limit" select="$limit"/>
<xsl:with-param name="length" select="$new-length"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Move elements from one place to another using XSLT 2

Move elements from one place to another using XSLT 2 (saxon8)
Hi all please help me to produce the following output using XSLT 2.0.
I have this:
<UL>
<ITEM>
aaa
<NL>iii
<ITEM1>111</ITEM1>
<ITEM2>222</ITEM2>
</NL>
</ITEM>
<ITEM>
bbb
<NL>vvv
<ITEM1>333</ITEM1>
<ITEM2>444</ITEM2>
</NL>
</ITEM>
</UL>
I need to produce this
<UL>
<ITEM>
aaa
<ITEM1>111</ITEM1>
</ITEM>
<ITEM>
bbb
<ITEM1>333</ITEM1>
</ITEM>
<NEW>
<NL>iii
<ITEM2>222</ITEM2>
</NL>
<NL>vvv
<ITEM2>444</ITEM2>
</NL>
</NEW>
</UL>
I am trying to get the output using mode option as mentioned below:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="NL"/>
<xsl:template match="ITEM2"/>
<xsl:template match="UL">
<xsl:copy>
<xsl:apply-templates/>
<NEW>
<xsl:apply-templates select="descendant::NL" mode="move"/>
</NEW>
</xsl:copy>
</xsl:template>
<xsl:template match="ITEM" mode="move">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="descendant::ITEM2" mode="move1"/>
</xsl:copy>
</xsl:template>
<xsl:template match="NL" mode="move">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="ITEM2" mode="move1">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Thanks and regards
Bala
Here is an XSLT 2.0 stylesheet that creates the structure you want, although white space is different, as it is a problem with mixed content to produce the exact output:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="UL">
<xsl:copy>
<xsl:for-each-group select="ITEM" group-by="NL/*/node-name(.)">
<xsl:choose>
<xsl:when test="position() eq 1">
<xsl:for-each select="current-group()">
<ITEM>
<xsl:apply-templates select="text() | NL/*[node-name(.) eq current-grouping-key()]"/>
</ITEM>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<NEW>
<xsl:for-each select="current-group()">
<NL>
<xsl:apply-templates select="NL/(text() | *[node-name(.) eq current-grouping-key()])"/>
</NL>
</xsl:for-each>
</NEW>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="NL/*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I get the result
<UL><ITEM>
aaa
<ITEM1>111</ITEM1>
</ITEM><ITEM>
bbb
<ITEM1>333</ITEM1>
</ITEM><NEW><NL>iii
<ITEM2>222</ITEM2>
</NL><NL>vvv
<ITEM2>444</ITEM2>
</NL></NEW></UL>
When I add instructions to strip white space from the input and indent the output, as in
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="UL">
<xsl:copy>
<xsl:for-each-group select="ITEM" group-by="NL/*/node-name(.)">
<xsl:choose>
<xsl:when test="position() eq 1">
<xsl:for-each select="current-group()">
<ITEM>
<xsl:apply-templates select="text() | NL/*[node-name(.) eq current-grouping-key()]"/>
</ITEM>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<NEW>
<xsl:for-each select="current-group()">
<NL>
<xsl:apply-templates select="NL/(text() | *[node-name(.) eq current-grouping-key()])"/>
</NL>
</xsl:for-each>
</NEW>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="NL/*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
the result is
<UL>
<ITEM>
aaa
<ITEM1>111</ITEM1>
</ITEM>
<ITEM>
bbb
<ITEM1>333</ITEM1>
</ITEM>
<NEW>
<NL>iii
<ITEM2>222</ITEM2>
</NL>
<NL>vvv
<ITEM2>444</ITEM2>
</NL>
</NEW>
</UL>
As an alternative, if we know there are just ITEM1 and ITEM2 we want to split, here is stylesheet using modes:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="#*|node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="NL">
<xsl:apply-templates select="ITEM1"/>
</xsl:template>
<xsl:template match="ITEM2"/>
<xsl:template match="UL">
<xsl:copy>
<xsl:apply-templates/>
<NEW>
<xsl:apply-templates select="descendant::NL" mode="move"/>
</NEW>
</xsl:copy>
</xsl:template>
<xsl:template match="ITEM1" mode="move"/>
</xsl:stylesheet>

XSLT Wrapping text with multiple tags

I have this span element with a class of autbib-pc-bold-italic
<span class="autbib-pc-bold-italic">autbib</span>
I want to create element tags base on the #class attribute value:
My output should be:
<autbib><pc><bold><italic>autbib</italic></bold></pc></autbib>
Here is my xsl templates:
<xsl:template match="span[contains(#class,'autbib')]">
<xsl:call-template name="pbib.loop">
<xsl:with-param name="count" select="count(tokenize(#class, '-'))"/>
<xsl:with-param name="class" select="tokenize(#class, '-')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="pbib.loop">
<xsl:param name="index" select="1" />
<xsl:param name="count" select="count(tokenize(#class, '-')) + 1"/>
<xsl:param name="class" select="tokenize(#class, '-')"/>
<xsl:element name="{$class[1]}">
<xsl:if test="not($index = $count)">
<xsl:element name="{$class[$index]}">
<xsl:apply-templates/>
</xsl:element>
</xsl:if>
</xsl:element>
<xsl:if test="not($index = $count)">
<xsl:call-template name="pbib.loop">
<xsl:with-param name="index" select="$index + 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
And have this output which is wrong:
<autbib>
<pc>autbib</pc>
<bold>autbib</bold>
<italic>autbib</italic>
</autbib>
I need to have this output:
<autbib>
<pc>
<bold>
<italic>autbib</italic>
</bold>
</pc>
</autbib>
My problem is that I'm not sure where I should place xsl:apply-template so that tags wrap with each other.
Here is my suggestion:
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output indent="yes"/>
<xsl:template match="span[contains(#class,'autbib')]">
<xsl:param name="classes" select="tokenize(#class, '-')"/>
<xsl:choose>
<xsl:when test="not($classes[1])">
<xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$classes[1]}">
<xsl:apply-templates select=".">
<xsl:with-param name="classes" select="$classes[position() gt 1]"/>
</xsl:apply-templates>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
With Saxon 9.4 that transforms the input
<span class="autbib-pc-bold-italic">autbib</span>
into the result
<autbib>
<pc>
<bold>
<italic>autbib</italic>
</bold>
</pc>
</autbib>

Resources