
    matches normalize-space(node()), which in this case is the text in the domain element.
2) key(...)[1]: This only grabs the first element from the node list that the key function returns.
3) count(.|key...)=1: This is an obscure method that XSLT provides to return true only if 2 nodes are the same.
    If the current node (.) is the same as what step 2 gave us (key...), this expression will return true.

The result is that we will get a node list of domain elements that are all unique. We've discarded all but the
first instance of each duplicate domain.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" encoding="utf-8"/>

    <!-- Setup the keys that we'll need. -->
    <!-- The *Path keys basically use a compound (domain+path) key. This is because we don't need to ensure uniqueness of paths, but rather paths and their domains. -->
    <xsl:key name="emieDomain" match="/rules/emie/domain" use="normalize-space(node())"/>
    <xsl:key name="emiePath" match="/rules/emie/domain/path" use="concat(normalize-space(node()), ' ', normalize-space(..))"/>
    <xsl:key name="docModeDomain" match="/rules/docMode/domain" use="normalize-space(node())"/>
    <xsl:key name="docModePath" match="/rules/docMode/domain/path" use="concat(normalize-space(node()), ' ', normalize-space(..))"/>
    <xsl:key name="allDomain" match="/rules/emie/domain | /rules/docMode/domain" use="normalize-space(node())"/>

    <xsl:template match="/">
        <site-list>
            <!-- Only add the version attribute if we actually find one in the input. -->
            <xsl:if test="/rules/@version">
                <xsl:attribute name="version">
                    <xsl:value-of select="/rules/@version"/>
                </xsl:attribute>
            </xsl:if>

            <!-- This will loop through each unique domain in the emie section. If there are duplicates, only the first will be processed. -->
            <!-- This XPath expression will loop through all of /rules/emie/domain, but the predicate will filter out the duplicates. -->
            <!-- See the comment at the top for details on how this works. -->
            <xsl:for-each select="/rules/emie/domain[count(.|key('emieDomain', normalize-space(node()))[1])=1]">
                <xsl:variable name="domain" select="normalize-space(node())"/>

                <site>
                    <!-- Write out the actual url. -->
                    <xsl:attribute name="url">
                        <xsl:value-of select="$domain"/>
                    </xsl:attribute>

                    <!-- Preserve the comments on this node as well as the corresponding node in the docMode section. -->
                    <xsl:attribute name="comment">
                        <xsl:value-of select="@comment"/>

                        <!-- To find the domain in the docMode section, we do the same thing as above with the sameDomainDocMode variable. -->
                        <xsl:for-each select="/rules/docMode/domain[$domain=normalize-space(node()) and count(.|key('docModeDomain', normalize-space(node()))[1])=1]">
                            <xsl:if test="@comment">
                                <!-- We'll separate the emie section comment from the docMode section comment with a space. -->
                                <xsl:text> </xsl:text>
                                <xsl:value-of select="@comment"/>
                            </xsl:if>
                        </xsl:for-each>
                    </xsl:attribute>

                    <compat-mode>
                        <xsl:choose>
                            <xsl:when test="@exclude = 'true'">
                                <xsl:choose>
                                    <!-- exclude="true" and forceCompatView="true" -->
                                    <xsl:when test="@forceCompatView = 'true'">IE7</xsl:when>
                                    <!-- exclude="true" and forceCompatView="false" -->
                                    <!-- Let's look for this same domain in the docMode section. That's why we do $domain=... -->
                                    <!-- Of course we only want the first one, so we do the count(.|key(...))=1 thing too. -->
                                    <xsl:otherwise>
                                        <xsl:variable name="sameDomainDocMode" select="/rules/docMode/domain[$domain=normalize-space(node()) and count(.|key('docModeDomain', normalize-space(node()))[1])=1]"/>
                                        <xsl:choose>
                                            <!-- Use the value from the docMode section -->
                                            <xsl:when test="$sameDomainDocMode">
                                                <xsl:for-each select="$sameDomainDocMode">
                                                    <xsl:call-template name="docModeAttribute"/>
                                                </xsl:for-each>
                                            </xsl:when>
                                            <!-- No domain in the docMode section, so just use the default -->
                                            <xsl:otherwise>Default</xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:choose>
                                    <!-- exclude="false" and forceCompatView="true" -->
                                    <xsl:when test="@forceCompatView = 'true'">IE7Enterprise</xsl:when>
                                    <!-- exclude="false" and forceCompatView="false" -->
                                    <xsl:otherwise>IE8Enterprise</xsl:otherwise>
                                </xsl:choose>
                            </xsl:otherwise>
                        </xsl:choose>
                    </compat-mode>

                    <!-- Process the doNotTransition attribute. This is easy because we don't have to look at the docMode section at all. -->
                    <xsl:call-template name="dntAttribute"/>
                </site>

                <!-- Process unique paths on this element. -->
                <!-- This uses a familiar method for only getting unique paths within this domain. -->
                <!-- The important differences are that we are only looking at paths that are children of this domain, and we are using our compound domain+path key. -->
                <xsl:for-each select="path[count(.|key('emiePath', concat(normalize-space(node()), ' ', normalize-space(..)))[1])=1]">
                    <site>
                        <xsl:variable name="path" select="normalize-space(node())"/>

                        <!-- Write out the actual url. -->
                        <xsl:attribute name="url">
                            <xsl:value-of select="$domain"/>
                            <xsl:if test="not(starts-with($path, '/'))">/</xsl:if>
                            <xsl:value-of select="$path"/>
                        </xsl:attribute>

                        <!-- Preserve the comments on this node as well as the corresponding node in the docMode section. -->
                        <xsl:attribute name="comment">
                            <xsl:value-of select="@comment"/>

                            <!-- To find the domain in the docMode section, we do the same thing as above with the samePathDocMode variable. -->
                            <xsl:for-each select="/rules/docMode/domain[$domain=normalize-space(node()) and count(.|key('docModeDomain', normalize-space(node()))[1])=1]/path[$path=normalize-space(node()) and count(.|key('docModePath', concat(normalize-space(node()), ' ', normalize-space(..)))[1])=1]">
                                <xsl:if test="@comment">
                                    <!-- We'll separate the emie section comment from the docMode section comment with a space. -->
                                    <xsl:text> </xsl:text>
                                    <xsl:value-of select="@comment"/>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:attribute>

                        <compat-mode>
                            <xsl:choose>
                                <xsl:when test="@exclude = 'true'">
                                    <xsl:choose>
                                        <!-- exclude="true" and forceCompatView="true" -->
                                        <xsl:when test="@forceCompatView = 'true'">IE7</xsl:when>
                                        <!-- exclude="true" and forceCompatView="false" -->
                                        <!-- This is similar to the sameDomainDocMode variable above, except there is a path clause added to the select expression. -->
                                        <!-- The extra path predicate is basically the same as the domain predicate, but it obviously needs to use the compound domain+path key. -->
                                        <xsl:otherwise>
                                            <xsl:variable name="samePathDocMode" select="/rules/docMode/domain[$domain=normalize-space(node()) and count(.|key('docModeDomain', normalize-space(node()))[1])=1]/path[$path=normalize-space(node()) and count(.|key('docModePath', concat(normalize-space(node()), ' ', normalize-space(..)))[1])=1]"/>
                                            <xsl:choose>
                                                <!-- Use the value from the docMode section -->
                                                <xsl:when test="$samePathDocMode">
                                                    <xsl:for-each select="$samePathDocMode">
                                                        <xsl:call-template name="docModeAttribute"/>
                                                    </xsl:for-each>
                                                </xsl:when>
                                                <!-- No path in the docMode section, so just use the default -->
                                                <xsl:otherwise>Default</xsl:otherwise>
                                            </xsl:choose>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:choose>
                                        <!-- exclude="false" and forceCompatView="true" -->
                                        <xsl:when test="@forceCompatView = 'true'">IE7Enterprise</xsl:when>
                                        <!-- exclude="false" and forceCompatView="false" -->
                                        <xsl:otherwise>IE8Enterprise</xsl:otherwise>
                                    </xsl:choose>
                                </xsl:otherwise>
                            </xsl:choose>
                        </compat-mode>

                        <!-- Process the doNotTransition attribute. This is easy because we don't have to look at the docMode section at all. -->
                        <xsl:call-template name="dntAttribute"/>
                    </site>
                </xsl:for-each>

                <!-- Now we want to grab all the paths that are on this domain but in the docMode section. -->
                <!-- If the path also exists in the emie section, we either don't care, or we handled it above with the samePathDocMode variable. -->
                <!-- First we are only interested in the first instance of this domain in the docMode section (in case there are dupes), so that is what the first predicate achieves. -->
                <!-- The next predicate uses the docModePath key with the complex domain+path key to make sure we don't get duplicate paths within this domain element. -->
                <xsl:for-each select="/rules/docMode/domain[$domain=normalize-space(node()) and count(.|key('docModeDomain', normalize-space(node()))[1])=1]/path[count(.|key('docModePath', concat(normalize-space(node()), ' ', normalize-space(..)))[1])=1]">
                    <xsl:call-template name="docModeDomain">
                        <xsl:with-param name="domain" select="$domain"/>
                        <xsl:with-param name="path" select="normalize-space(node())"/>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:for-each>

            <!-- Now we will look at the docMode section on its own. -->
            <!-- Since we already handled everything in the emie section, we only care about stuff in this section that we've never seen before. -->
            <!-- For that reason, we use the allDomain key. We only want domains that are appearing in the docMode section for the first time. -->
            <xsl:for-each select="/rules/docMode/domain[count(.|key('allDomain', normalize-space(node()))[1])=1]">
                <xsl:call-template name="docModeDomain">
                    <xsl:with-param name="domain" select="normalize-space(node())"/>
                </xsl:call-template>
            </xsl:for-each>
        </site-list>
    </xsl:template>

    <!-- Process a domain or path element in the docMode section. This is easy because we don't have to worry about the emie section. -->
    <xsl:template name="docModeDomain">
        <xsl:param name="domain"/>
        <xsl:param name="path"/>

        <site>
            <xsl:attribute name="url">
                <xsl:value-of select="$domain"/>
                <xsl:if test="$path">
                    <xsl:if test="not(starts-with(normalize-space(node()), '/'))">/</xsl:if>
                    <xsl:value-of select="normalize-space(node())"/>
                </xsl:if>
            </xsl:attribute>
            <xsl:attribute name="comment">
                <xsl:value-of select="@comment"/>
            </xsl:attribute>
            <compat-mode>
                <xsl:call-template name="docModeAttribute"/>
            </compat-mode>
            <xsl:call-template name="dntAttribute"/>
        </site>

        <!-- Process unique paths on this element. -->
        <!-- This uses a familiar method for only getting unique paths within this domain. Look at how this gets done for paths in the emie section. -->
        <xsl:for-each select="path[count(.|key('docModePath', concat(normalize-space(node()), ' ', normalize-space(..)))[1])=1]">
            <xsl:call-template name="docModeDomain">
                <xsl:with-param name="domain" select="$domain"/>
                <xsl:with-param name="path" select="normalize-space(node())"/>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <!-- This maps the docMode attribute value to the new compat-mode element. -->
    <xsl:template name="docModeAttribute">
        <xsl:choose>
            <xsl:when test="@docMode = 5">IE5</xsl:when>
            <xsl:when test="@docMode = 7">IE7</xsl:when>
            <xsl:when test="@docMode = 8">IE8</xsl:when>
            <xsl:when test="@docMode = 9">IE9</xsl:when>
            <xsl:when test="@docMode = 10">IE10</xsl:when>
            <xsl:when test="@docMode = 11 or @docMode = 12 or @docMode = 'edge'">IE11</xsl:when>
            <xsl:otherwise>Default</xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- This maps the doNotTransition attribute to the new open-in element. -->
    <xsl:template name="dntAttribute">
        <open-in>
            <xsl:choose>
                <xsl:when test="@doNotTransition = 'true'">None</xsl:when>
                <xsl:otherwise>IE11</xsl:otherwise>
            </xsl:choose>
        </open-in>
    </xsl:template>
</xsl:stylesheet>
