XSL repeat the parent node for each child node - xslt

For each child node, I want to duplicate my parent node so that the resulting xml, contains only one child for the parent node with the other nodes being the same.
Here is a sample input
<a>
<a1>header1</a1>
<a2>header2</a2>
<a3>
<a31>
<a311>line_1</a311>
<a311>line_2</a311>
</a31>
<a32>5o$</a32>
<a33>Add</a33>
</a3>
<a4>account_holder</a4>
</a>
What I want to do is - repeat a3 for as many times as the node a311 comes. Rest all nodes are retained
Output
<a>
<a1>header1</a1>
<a2>header2</a2>
<a3>
<a31>
<a311>line_1</a311>
</a31>
<a32>5o$</a32>
<a33>Add</a33>
</a3>
<a3>
<a31>
<a311>line_2</a311>
</a31>
<a32>5o$</a32>
<a33>Add</a33>
</a3>
<a4>account_holder</a4>
</a>

More semantic with "tunnel param" pattern, this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()" name="identity">
<xsl:param name="pCurrent"/>
<xsl:copy>
<xsl:apply-templates select="#*|node()">
<xsl:with-param name="pCurrent" select="$pCurrent"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="a3">
<xsl:variable name="vCurrent" select="."/>
<xsl:variable name="vDescendants" select=".//a311"/>
<xsl:for-each select="$vDescendants|$vCurrent[not($vDescendants)]">
<xsl:variable name="vDescendant" select="."/>
<xsl:for-each select="$vCurrent">
<xsl:call-template name="identity">
<xsl:with-param name="pCurrent" select="$vDescendant"/>
</xsl:call-template>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template match="a311">
<xsl:param name="pCurrent"/>
<xsl:if test="generate-id()=generate-id($pCurrent)">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<a>
<a1>header1</a1>
<a2>header2</a2>
<a3>
<a31>
<a311>line_1</a311>
</a31>
<a32>5o$</a32>
<a33>Add</a33>
</a3>
<a3>
<a31>
<a311>line_2</a311>
</a31>
<a32>5o$</a32>
<a33>Add</a33>
</a3>
<a4>account_holder</a4>
</a>
EDIT: Handling no descendants case.

The following (XSLT 1.0) stylesheet produces the desired result:
<xsl:stylesheet version="1.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="a3">
<xsl:apply-templates select="a31/a311" />
</xsl:template>
<xsl:template match="a311">
<a3>
<a31>
<a311>
<xsl:value-of select="." />
</a311>
</a31>
<xsl:apply-templates select="../../*[not(self::a31)]" />
</a3>
</xsl:template>
</xsl:stylesheet>

You didn't specify the XSLT version. Here's an XSLT2 stylesheet that will accomplish what you want:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="a3" mode="#default">
<xsl:apply-templates select="a31/a311"/>
</xsl:template>
<xsl:template match="a311">
<xsl:apply-templates select="../.." mode="x">
<xsl:with-param name="label" select="text()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="a3" mode="x">
<xsl:param name="label"/>
<xsl:copy>
<xsl:apply-templates select="#*"/>
<a31>
<a311><xsl:value-of select="$label"/></a311>
</a31>
<xsl:apply-templates select="* except a31"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
It uses an identity transform to pass through the "uninteresting" elements while trapping the <a3> node and applying special handling. If you need an XSLT1 solution, merely
Replace select="* except a31" with select="*[name() != 'a31']"
Remove the mode="#default" in the first "a3" template

Related

XSLT - Update namespaces of the original xml before applying other templates

I have an xml as below:
<?xml version="1.0" encoding="UTF-8"?>
<mdb:MD_Metadata xmlns:mdb="http://standards.iso.org/iso/19115/-3/mdb/1.0"
xmlns:cit="http://standards.iso.org/iso/19115/-3/cit/1.0"
xmlns:lan="http://standards.iso.org/iso/19115/-3/lan/1.0"
xmlns:mcc="http://standards.iso.org/iso/19115/-3/mcc/1.0"
xmlns:gco="http://standards.iso.org/iso/19115/-3/gco/1.0">
<mdb:metadataIdentifier>
<mcc:MD_Identifier>
<mcc:authority>
<cit:CI_Citation>
<cit:title>
<gco:CharacterString>Old UUID</gco:CharacterString>
</cit:title>
</cit:CI_Citation>
</mcc:authority>
<mcc:code>
<gco:CharacterString>3796749d-c8a5-46ae-af11-24a977cb1d9a</gco:CharacterString>
</mcc:code>
<mcc:codeSpace>
<gco:CharacterString>urn:uuid</gco:CharacterString>
</mcc:codeSpace>
</mcc:MD_Identifier>
</mdb:metadataIdentifier>
</mdb:MD_Metadata>
I want to update the namespaces (mdb and cit to2.0) before applying other templates:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cit1="http://standards.iso.org/iso/19115/-3/cit/1.0"
xmlns:cit="http://standards.iso.org/iso/19115/-3/cit/2.0"
xmlns:lan="http://standards.iso.org/iso/19115/-3/lan/1.0"
xmlns:mcc="http://standards.iso.org/iso/19115/-3/mcc/1.0"
xmlns:mco="http://standards.iso.org/iso/19115/-3/mco/1.0"
xmlns:mda="http://standards.iso.org/iso/19115/-3/mda/1.0"
xmlns:mdb1="http://standards.iso.org/iso/19115/-3/mdb/1.0"
xmlns:mdb="http://standards.iso.org/iso/19115/-3/mdb/2.0"
xmlns:gco="http://standards.iso.org/iso/19115/-3/gco/1.0"
xmlns:gcx="http://standards.iso.org/iso/19115/-3/gcx/1.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="#all">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="mdb1:*">
<xsl:element name="{name()}" namespace="http://standards.iso.org/iso/19115/-3/mdb/2.0">
<xsl:if test="count(ancestor::*) = 0">
<xsl:call-template name="add-iso19115-3.2018-namespaces"/>
</xsl:if>
</xsl:element>
</xsl:template>
<xsl:template match="cit1:*" >
<xsl:element name="{name()}" namespace="http://standards.iso.org/iso/19115/-3/cit/2.0">
<xsl:apply-templates select="#*|*"/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<!--Identity template that will copy every attribute, element, comment, and processing instruction to the output-->
<xsl:template match="#*|node()">
<xsl:copy copy-namespaces="no">
<!-- Including any attributes it has and any child nodes -->
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="mdb:MD_Metadata" mode="main">
<xsl:copy>
<xsl:apply-templates select="#* , node()"/>
<!-- add mdb:defaultLocale after mdb:metadataIdentifier -->
<mdb:defaultLocale>
<lan:PT_Locale id="EN">
<lan:language>
<lan:LanguageCode codeList="http://www.loc.gov/standards/iso639-2/" codeListValue="eng"/>
</lan:language>
<lan:characterEncoding>
<lan:MD_CharacterSetCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_CharacterSetCode" codeListValue="utf8"/>
</lan:characterEncoding>
</lan:PT_Locale>
</mdb:defaultLocale>
</xsl:copy>
</xsl:template>
<!-- replace cit:title -->
<xsl:template match="mdb:metadataIdentifier/mcc:MD_Identifier/mcc:authority/cit:CI_Citation/cit:title/gco:CharacterString/text()">
<xsl:value-of select="'UUID Test'"/>
</xsl:template>
<!-- add mcc:description -->
<xsl:template match="mdb:metadataIdentifier/mcc:MD_Identifier">
<xsl:apply-templates/>
<mcc:description>
<gco:CharacterString>Data was submitted
</gco:CharacterString>
</mcc:description>
</xsl:template>
<!-- update attribute 'codeLIst' value of mdb:metadataScope > mdb:MD_MetadataScope > mdb:resourceScope > mcc:MD_ScopeCode -->
<xsl:template match="mdb:metadataScope/mdb:MD_MetadataScope/mdb:resourceScope/mcc:MD_ScopeCode">
<xsl:if test="#codeListValue">
<xsl:attribute name="codeList">
<xsl:value-of select="'https://schemas.isotc211.org/19115/resources/Codelist/cat/codelists.xml#MD_ScopeCode'" />
</xsl:attribute>
</xsl:if>
</xsl:template>
<xsl:template name="add-iso19115-3.2018-namespaces">
<xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>
<xsl:namespace name="cit" select="'http://standards.iso.org/iso/19115/-3/cit/2.0'"/>
<xsl:namespace name="lan" select="'http://standards.iso.org/iso/19115/-3/lan/1.0'"/>
<xsl:namespace name="mcc" select="'http://standards.iso.org/iso/19115/-3/mcc/1.0'"/>
<xsl:namespace name="gco" select="'http://standards.iso.org/iso/19115/-3/gco/1.0'"/>
<xsl:namespace name="xlink" select="'http://www.w3.org/1999/xlink'"/>
</xsl:template>
</xsl:stylesheet>
The stylesheet above only work if the namespaces associated with mdb and cit in the original file are as below:
<mdb:MD_Metadata xmlns:mdb="http://standards.iso.org/iso/19115/-3/mdb/2.0"
xmlns:cit="http://standards.iso.org/iso/19115/-3/cit/2.0"
xmlns:lan="http://standards.iso.org/iso/19115/-3/lan/1.0"
xmlns:mcc="http://standards.iso.org/iso/19115/-3/mcc/1.0"
xmlns:gco="http://standards.iso.org/iso/19115/-3/gco/1.0">
....
How can I first update the namespaces and then apply the rest templates against the updated xml?
Perhaps the following is what you want, performing the namespace transformations and the other changes in one transformation step:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cit1="http://standards.iso.org/iso/19115/-3/cit/1.0"
xmlns:cit="http://standards.iso.org/iso/19115/-3/cit/2.0"
xmlns:lan="http://standards.iso.org/iso/19115/-3/lan/1.0"
xmlns:mcc="http://standards.iso.org/iso/19115/-3/mcc/1.0"
xmlns:mco="http://standards.iso.org/iso/19115/-3/mco/1.0"
xmlns:mda="http://standards.iso.org/iso/19115/-3/mda/1.0"
xmlns:mdb1="http://standards.iso.org/iso/19115/-3/mdb/1.0"
xmlns:mdb="http://standards.iso.org/iso/19115/-3/mdb/2.0"
xmlns:gco="http://standards.iso.org/iso/19115/-3/gco/1.0"
xmlns:gcx="http://standards.iso.org/iso/19115/-3/gcx/1.0"
xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="#all">
<xsl:output method="xml" indent="yes"/>
<!--Identity template that will copy every attribute, element, comment, and processing instruction to the output-->
<xsl:template match="#*|node()">
<xsl:copy copy-namespaces="no">
<!-- Including any attributes it has and any child nodes -->
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/mdb1:*">
<xsl:element name="mdb:{local-name()}">
<xsl:call-template name="add-iso19115-3.2018-namespaces"/>
<xsl:apply-templates select="#* , node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="mdb1:*">
<xsl:element name="mdb:{local-name()}">
<xsl:apply-templates select="#* , node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="cit1:*">
<xsl:element name="cit:{local-name()}">
<xsl:apply-templates select="#* , node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="mdb1:MD_Metadata">
<xsl:copy>
<xsl:apply-templates select="#* , node()"/>
<!-- add mdb:defaultLocale after mdb:metadataIdentifier -->
<mdb:defaultLocale>
<lan:PT_Locale id="EN">
<lan:language>
<lan:LanguageCode codeList="http://www.loc.gov/standards/iso639-2/" codeListValue="eng"/>
</lan:language>
<lan:characterEncoding>
<lan:MD_CharacterSetCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_CharacterSetCode" codeListValue="utf8"/>
</lan:characterEncoding>
</lan:PT_Locale>
</mdb:defaultLocale>
</xsl:copy>
</xsl:template>
<!-- replace cit:title -->
<xsl:template match="mdb1:metadataIdentifier/mcc:MD_Identifier/mcc:authority/cit1:CI_Citation/cit1:title/gco:CharacterString/text()">
<xsl:value-of select="'UUID Test'"/>
</xsl:template>
<!-- add mcc:description -->
<xsl:template match="mdb1:metadataIdentifier/mcc:MD_Identifier">
<xsl:apply-templates/>
<mcc:description>
<gco:CharacterString>Data was submitted
</gco:CharacterString>
</mcc:description>
</xsl:template>
<!-- update attribute 'codeLIst' value of mdb:metadataScope > mdb:MD_MetadataScope > mdb:resourceScope > mcc:MD_ScopeCode -->
<xsl:template match="mdb1:metadataScope/mdb1:MD_MetadataScope/mdb1:resourceScope/mcc:MD_ScopeCode">
<xsl:if test="#codeListValue">
<xsl:attribute name="codeList">
<xsl:value-of select="'https://schemas.isotc211.org/19115/resources/Codelist/cat/codelists.xml#MD_ScopeCode'" />
</xsl:attribute>
</xsl:if>
</xsl:template>
<xsl:template name="add-iso19115-3.2018-namespaces">
<xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>
<xsl:namespace name="cit" select="'http://standards.iso.org/iso/19115/-3/cit/2.0'"/>
<xsl:namespace name="lan" select="'http://standards.iso.org/iso/19115/-3/lan/1.0'"/>
<xsl:namespace name="mcc" select="'http://standards.iso.org/iso/19115/-3/mcc/1.0'"/>
<xsl:namespace name="gco" select="'http://standards.iso.org/iso/19115/-3/gco/1.0'"/>
<xsl:namespace name="xlink" select="'http://www.w3.org/1999/xlink'"/>
</xsl:template>
</xsl:stylesheet>

XSLT modify attribute value at EXACT element by passing the original value to a template

I'm struggling to get this abomination called XSLT to work. I need to get an EXACT attribute at EXACT path, pass its original value to a template and rewrite this value with the result from the template.
I'm having a file like this:
<?xml version="1.0" encoding="windows-1251"?>
<File>
<Document ReportYear="17">
...
...
</Document>
</File>
So I made an XSLT like this:
<?xml version="1.0" encoding="windows-1251"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" encoding="windows-1251" indent="yes" />
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template name="formatYear">
<xsl:param name="year" />
<xsl:value-of select="$year + 2000" />
</xsl:template>
<xsl:template match="File/Document">
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:attribute name="ReportYear">
<xsl:call-template name="formatYear">
<xsl:with-param name="year" select="#ReportYear" />
</xsl:call-template>
</xsl:attribute>
</xsl:copy>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
This works fine except it closes the <Document> tag immediately and places its content immediately after itself.
Also, can I address the ReportYear attribute value without repeating it twice? I tried current() but it didn't work.
If you're closing <xsl:copy> before applying templates to the remainder of the content of <Document>, then of course <Document> will be closed before the remainder of the content of <Document> appears in the output.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" encoding="windows-1251" indent="yes" />
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Document">
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:attribute name="ReportYear">
<xsl:value-of select="#ReportYear + 2000" />
</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
outputs
<?xml version="1.0" encoding="windows-1251"?>
<File>
<Document ReportYear="2017">
...
...
</Document>
</File>
I don't think an extra template just for adding 2000 to #ReportYear is necessary. But if you must, you can streamline the whole thing like so
<xsl:template name="formatYear">
<xsl:param name="year" select="#ReportYear" /> <!-- you can define a default value -->
<xsl:value-of select="$year + 2000" />
</xsl:template>
and
<xsl:attribute name="ReportYear">
<xsl:call-template name="formatYear" /> <!-- ...and can use it implicitly here -->
</xsl:attribute>
If you need to process the contents of the Document element with apply-templates and want to keep the result of the applied templates as the children then you need to move the apply-templates inside of the copy:
<xsl:template match="File/Document">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:attribute name="ReportYear">
<xsl:call-template name="formatYear">
<xsl:with-param name="year" select="#ReportYear"/>
</xsl:call-template>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Not sure why you haven't simply used
<xsl:template match="File/Document/#ReportYear">
<xsl:attribute name="{name()}">
<xsl:value-of select=". + 2000"/>
</xsl:attribute>
</xsl:template>
together with the identity transformation template.

Transform nested child elements to single element

I need to convert a some basic XHTML output from an WYSIWYG editor to a proprietairy typesetting XML format using XSLT.
The typesetting format may contain multiple txt elements, each which can have bold, italic and underline boolean attributes.
Now I want to be able to convert nested combinations of <I>, <B> and <U> tags and convert them to this XML format. For example:
<block><b><i><u>BoldItalicUnderlined</u>BoldItalic</i>Bold</b></block>
To:
<txt bold="true" italic="true" underlined="true">BoldItalicUnderlined</txt>
<txt bold="true" italic="true" underlined="false">BoldItalicUnderlined</txt>
<txt bold="true" italic="false" underlined="false">Bold</txt>
Can this be done with XSLT?
So far I have:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xls="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common">
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|#*" mode="mPass2">
<xsl:copy>
<xsl:apply-templates select="node()|#*" mode="mPass2"/>
</xsl:copy>
</xsl:template>
<xsl:template match="block">
<xsl:variable name="vrtfPass1Result">
<xsl:apply-templates/>
</xsl:variable>
<xsl:apply-templates mode="mPass2" select="ext:node-set($vrtfPass1Result)/*"/>
</xsl:template>
<xsl:template match="text()">
<txt underline="false" italic="false" bold="false" the-text="true">
<xsl:value-of select="."/>
</txt>
</xsl:template>
<xsl:template match="em|i">
<txt underline="false" italic="true" bold="false" the-italic="true">
<xsl:apply-templates/>
</txt>
</xsl:template>
<xsl:template match="strong|b">
<txt underline="false" italic="false" bold="true" the-bold="true">
<xsl:apply-templates/>
</txt>
</xsl:template>
<xsl:template match="u">
<txt underline="true" italic="false" bold="false" the-underline="true">
<xsl:apply-templates/>
</txt>
</xsl:template>
<xsl:template match="txt[txt]" mode="mPass2" name="txt">
<xsl:variable name="bold">
<xsl:value-of select="#bold"/>
</xsl:variable>
<xsl:for-each select="txt">
<xsl:apply-templates select="." mode="mPass2"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Which produces:
<txt underline="false" italic="false" bold="false">BoldItalicUnderlined</txt>
<txt underline="false" italic="false" bold="false">BoldItalic</txt>
<txt underline="false" italic="false" bold="false">Bold</txt>
Try this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//text()"/>
</xsl:template>
<xsl:template match="text()[normalize-space()='']"/>
<xsl:template match="text()">
<text bold="{boolean(ancestor::b)}" italic="{boolean(ancestor::i)}" underlined="{boolean(ancestor::u)}">
<xsl:value-of select="."/>
</text>
</xsl:template>
</xsl:stylesheet>

XSLT - How to split up a parent's children

My xml structure contains a program as the parent of both certificates and courses. I want to split the structure up to create an independent listing of certificates and courses without the common program parent. The original structure is:
<root>
<program>
<program-name>Math</program-name>
<certificate>...</certificate> <!-- There can 0 or more of these -->
<course>...</course> <!-- There can 0 or more of these -->
</program>
<program>
...
</program>
</root>
The output should look like so:
<root>
<program-courses>
<program>
<program-name>Math</program-name>
<course/> <!-- There can 0 or more of these -->
</program>
...
</program-courses>
<program-certificates>
<program>
<program-name>Math</program-name>
<certificate/> <!-- There can 0 or more of these -->
</program>
...
</program-certificates>
</root>
Update: Answered using Paul's suggestion for using a mode this is what the relevant portion of the xslt became:
<xsl:template match="root">
<xsl:element name="root">
<xsl:element name="program-courses">
<xsl:apply-templates select="root/program-area" mode="course"/>
</xsl:element>
<xsl:element name="program-certificates">
<xsl:apply-templates select="root/program-area" mode="certificate"/>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="program-area" mode="course">
<xsl:element name="program-area">
<!-- Get the name here -->
<xsl:element name="course">
<xsl:apply-templates select="course"/>
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="program-area" mode="certificate">
<xsl:element name="program-area">
<!-- Get the name here -->
<xsl:element name="course">
<xsl:apply-templates select="certificate"/>
</xsl:element>
</xsl:element>
</xsl:template>
Note that this solution is pared down from the actual one so it may not work as is against the original input.
Selecting program elements, You could use #mode (on apply-templates and a corresponding template) to differentiate between whether you are operating within the output of program-courses or program-certificates
From root, you could select program/course or program/certificate to generate the output program.
From root, you could use for-each select="program" and for the part that is intended to output program-courses only extract the program-name and course element, and perform the corresponding extraction in the part that outputs program-certificates.
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="program">
<xsl:apply-templates select="course[1]|certificate[1]"/>
</xsl:template>
<xsl:template match="course[1]">
<program-courses>
<program>
<xsl:apply-templates select="../*[not(self::certificate)]"
mode="copy"/>
</program>
</program-courses>
</xsl:template>
<xsl:template match="certificate[1]">
<program-certificates>
<program>
<xsl:apply-templates select="../*[not(self::course)]"
mode="copy"/>
</program>
</program-certificates>
</xsl:template>
<xsl:template match="node()" mode="copy">
<xsl:call-template name="identity"/>
</xsl:template>
</xsl:stylesheet>
Output:
<root>
<program-certificates>
<program>
<program-name>Math</program-name>
<certificate>...</certificate>
</program>
</program-certificates>
<program-courses>
<program>
<program-name>Math</program-name>
<course>...</course>
</program>
</program-courses>
</root>
EDIT: If you want something more "push style" like your posted solution:
<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="node()" mode="certificate">
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match="node()" mode="course">
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match="program">
<program-courses>
<program>
<xsl:apply-templates mode="course"/>
</program>
</program-courses>
<program-certificates>
<program>
<xsl:apply-templates mode="certificate"/>
</program>
</program-certificates>
</xsl:template>
<xsl:template match="course" mode="certificate"/>
<xsl:template match="certificate" mode="course"/>
</xsl:stylesheet>

XSLT. How to add a node in the leftmost leaf, with the given name?

I woud like to add a node in the leftmost leaf, with the given name.
For example,
<root>
<aaa name="a">
<aaa name="x"/>
</aaa>
<aaa name="b">
<aaa name="y">
<aaa name="z"/>
</aaa>
</aaa>
<aaa name="c">
<aaa name="z"/>
</aaa>
</root>
Given name= "z" and given new node is <aaa name="w">.
New tree should be the following form:
<root>
<aaa name="a">
<aaa name="x"/>
</aaa>
<aaa name="b">
<aaa name="y">
<aaa name="z">
<aaa name="w"/>
</aaa>
</aaa>
</aaa>
<aaa name="c">
<aaa name="z"/>
</aaa>
</root>
If by leftmost you mean the 'z' node with the greatest depth, you could first define a variable to work out the depth of the left-most 'z', and then add the 'w' node when you match a node at such a depth
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Work out depth of left most 'z' node -->
<xsl:variable name="LeftMost">
<xsl:for-each select="//*[#name='z']">
<xsl:sort select="count(ancestor::*)" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="count(ancestor::*)"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
<xsl:if test="#name='z' and count(ancestor::*) = $LeftMost">
<aaa name="w"/>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Using this, if you had two 'z' nodes at the same depth you would end up with both be given a 'w' node.
An alternative approach is to use generate-id() to get the ID of the fist 'z' at the greatest depth, and then add the 'w' when you match the node with the same id. This would then only add a 'w' node to the first 'z' node it finds at the greatest depth.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="LeftMost">
<xsl:for-each select="//*[#name='z']">
<xsl:sort select="count(ancestor::*)" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="generate-id()"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
<xsl:if test="#name='z' and generate-id() = $LeftMost">
<aaa name="w"/>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
A variant of #Tim C's variable based approach would involve an <xsl:key> and the preceding axis, like this:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:key
name="kCountPreceding" match="aaa[#name='z']"
use="count(preceding::aaa[#name='z'])"
/>
<xsl:variable name="vLeftMostId" select="
generate-id(key('kCountPreceding', 0))"
/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
<xsl:if test="generate-id() = $vLeftMostId">
<aaa name="w" />
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Downside of this approach is, that no variables are allowed in a match expression. This means this cannot be made dynamic, the key's match="aaa[#name='z']" must be hard-coded.
You can start with something like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
<xsl:if test="#name='z'">
<aaa name="w"/>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Why don't you use preceding axis?
<?xml version="1.0" encoding="UTF-8"?>
<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="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*[#name='z' and not(preceding::*[#name='z'])]">
<xsl:copy>
<xsl:copy-of select="#*"/>
<aaa name="w" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
BTW, the leftmost leaf is the first leaf in the document order, isn't it?
<xsl:for-each select="/descendant::*[#name='z' and not(*)][1]">
...
<aaa name="w" />
...
</xsl:for-each>
the leftmost leaf is the first leaf in
the document order, isn't it?
yes, the first leaf, with given name