Search code examples
xmlxsltmergeelementmultivalue

XSL Create New Element from MultipleXML Fields including some with the Same Name


I'm trying to create a new XML element by joining multiple elements (some with the same name) into a new element. I'm able to create a new element by joining the Publisher elements, but I'm not sure how to proceed. The end result would be a new | delimited element containing Key, Author, Title and Publisher. The multi-valued fields would be separated by commas. I can use XSL 1.0 or 2.0. Thanks.

Input File

<Records>
    <Record>
        <Key>11111</Key>
        <Author>AA</Author>
        <Author>AB</Author>
        <Author>AC</Author>
        <Author>AD</Author>
        <Author>AE</Author>
        <Entrydate>10/12/1956</Entrydate>
        <Title>Paper Title 1</Title>
        <Title>Paper Title 2</Title>
        <Title>Paper Title 3</Title>
        <Title>Paper Title 4</Title>
        <Title>Paper Title 5</Title>
        <Publisher>Publisher 1</Publisher>
        <Publisher>Publisher 2</Publisher>
        <Publisher>Publisher 3</Publisher>
        <Publisher>Publisher 4</Publisher>
        <Publisher>Publisher 5</Publisher>
    </Record>
    <Record>
        <Key>33333</Key>
        <Author>BA</Author>
        <Author>BB</Author>
        <Entrydate>10/12/1965</Entrydate>
        <Title>Paper Title 1</Title>
        <Title>Paper Title 2</Title>
        <Publisher>Publisher 1</Publisher>
        <Publisher>Publisher 2</Publisher>
    </Record>
    <Record>
        <Key>22222</Key>
        <Author>CA</Author>
        <Entrydate>11/12/1966</Entrydate>
        <Title>Paper Title 1</Title>
        <Publisher>Publisher 1</Publisher>
    </Record>
    <Record>
        <Key>44444</Key>
        <Author>DA</Author>
        <Author>DB</Author>
        <Author>DC</Author>
        <Author>DD</Author>
        <Author>DE</Author>
        <Author>DF</Author>
        <Entrydate>09/12/1976</Entrydate>
        <Title>Paper Title 1</Title>
        <Title>Paper Title 2</Title>
        <Title>Paper Title 3</Title>
        <Title>Paper Title 4</Title>
        <Title>Paper Title 5</Title>
        <Title>Paper Title 6</Title>
        <Publisher>Publisher 1</Publisher>
        <Publisher>Publisher 2</Publisher>
        <Publisher>Publisher 3</Publisher>
        <Publisher>Publisher 4</Publisher>
        <Publisher>Publisher 5</Publisher>
        <Publisher>Publisher 6</Publisher>
    </Record>
</Records>

Desired output

<Records>
    <Record>
        <Key>11111</Key>
        <Author>AA</Author>
        <Author>AB</Author>
        <Author>AC</Author>
        <Author>AD</Author>
        <Author>AE</Author>
        <Entrydate>10/12/1956</Entrydate>
        <Title>Paper Title 1</Title>
        <Title>Paper Title 2</Title>
        <Title>Paper Title 3</Title>
        <Title>Paper Title 4</Title>
        <Title>Paper Title 5</Title>
        <Publisher>Publisher 1</Publisher>
        <Publisher>Publisher 2</Publisher>
        <Publisher>Publisher 3</Publisher>
        <Publisher>Publisher 4</Publisher>
        <Publisher>Publisher 5</Publisher>
        <Combined>11111|AA, AB, AC, AD, AE|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5</Combined>
    </Record>
    <Record>
        <Key>33333</Key>
        <Author>BA</Author>
        <Author>BB</Author>
        <Entrydate>10/12/1965</Entrydate>
        <Title>Paper Title 1</Title>
        <Title>Paper Title 2</Title>
        <Publisher>Publisher 1</Publisher>
        <Publisher>Publisher 2</Publisher>
        <Combined>33333|BA, BB|Paper Title 1, Paper Title 2|Publisher 1, Publisher 2</Combined>
    </Record>
    <Record>
        <Key>22222</Key>
        <Author>CA</Author>
        <Entrydate>11/12/1966</Entrydate>
        <Title>Paper Title 1</Title>
        <Publisher>Publisher 1</Publisher>
        <Combined>22222|CA|Paper Title 1|Publisher 1</Combined>
    </Record>
    <Record>
        <Key>44444</Key>
        <Author>DA</Author>
        <Author>DB</Author>
        <Author>DC</Author>
        <Author>DD</Author>
        <Author>DE</Author>
        <Author>DF</Author>
        <Entrydate>09/12/1976</Entrydate>
        <Title>Paper Title 1</Title>
        <Title>Paper Title 2</Title>
        <Title>Paper Title 3</Title>
        <Title>Paper Title 4</Title>
        <Title>Paper Title 5</Title>
        <Title>Paper Title 6</Title>
        <Publisher>Publisher 1</Publisher>
        <Publisher>Publisher 2</Publisher>
        <Publisher>Publisher 3</Publisher>
        <Publisher>Publisher 4</Publisher>
        <Publisher>Publisher 5</Publisher>
        <Publisher>Publisher 6</Publisher>
        <Combined>44444|DA, DB, DC, DD, DE, DF|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5, Paper Title 6|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5, Publisher 6</Combined>
    </Record>
</Records>

Code so far

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="no" indent="yes" />
    <xsl:strip-space elements="*" />
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Record">
        <xsl:copy>
            <xsl:copy-of select="Publisher"/>
            <xsl:apply-templates select="*[not(self::Publisher)]" />
            <Combined>
                <xsl:apply-templates select="Publisher/text()" />
            </Combined>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Publisher/text()">
        <xsl:if test="position() &gt; 1">|</xsl:if>
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Solution

  • Change your Record template to the following and omit your last template <xsl:template match="Publisher/text()">.

    <xsl:template match="Record">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
            <Combined>
                <xsl:for-each select="*[not(self::Entrydate)]">
                    <xsl:choose>
                        <xsl:when test="name() = name(preceding-sibling::*[1])">, </xsl:when>
                        <xsl:when test="position() = 1" />
                        <xsl:otherwise>|</xsl:otherwise>
                    </xsl:choose>
                    <xsl:value-of select="." />
                </xsl:for-each>
            </Combined>
        </xsl:copy>
    </xsl:template>
    

    and the result will be as desired:

    <?xml version="1.0"?>
    <Records>
      <Record>
        <Key>11111</Key>
        <Author>AA</Author>
        <Author>AB</Author>
        <Author>AC</Author>
        <Author>AD</Author>
        <Author>AE</Author>
        <Entrydate>10/12/1956</Entrydate>
        <Title>Paper Title 1</Title>
        <Title>Paper Title 2</Title>
        <Title>Paper Title 3</Title>
        <Title>Paper Title 4</Title>
        <Title>Paper Title 5</Title>
        <Publisher>Publisher 1</Publisher>
        <Publisher>Publisher 2</Publisher>
        <Publisher>Publisher 3</Publisher>
        <Publisher>Publisher 4</Publisher>
        <Publisher>Publisher 5</Publisher>
        <Combined>11111|AA, AB, AC, AD, AE|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5</Combined>
      </Record>
      <Record>
        <Key>33333</Key>
        <Author>BA</Author>
        <Author>BB</Author>
        <Entrydate>10/12/1965</Entrydate>
        <Title>Paper Title 1</Title>
        <Title>Paper Title 2</Title>
        <Publisher>Publisher 1</Publisher>
        <Publisher>Publisher 2</Publisher>
        <Combined>33333|BA, BB|Paper Title 1, Paper Title 2|Publisher 1, Publisher 2</Combined>
      </Record>
      <Record>
        <Key>22222</Key>
        <Author>CA</Author>
        <Entrydate>11/12/1966</Entrydate>
        <Title>Paper Title 1</Title>
        <Publisher>Publisher 1</Publisher>
        <Combined>22222|CA|Paper Title 1|Publisher 1</Combined>
      </Record>
      <Record>
        <Key>44444</Key>
        <Author>DA</Author>
        <Author>DB</Author>
        <Author>DC</Author>
        <Author>DD</Author>
        <Author>DE</Author>
        <Author>DF</Author>
        <Entrydate>09/12/1976</Entrydate>
        <Title>Paper Title 1</Title>
        <Title>Paper Title 2</Title>
        <Title>Paper Title 3</Title>
        <Title>Paper Title 4</Title>
        <Title>Paper Title 5</Title>
        <Title>Paper Title 6</Title>
        <Publisher>Publisher 1</Publisher>
        <Publisher>Publisher 2</Publisher>
        <Publisher>Publisher 3</Publisher>
        <Publisher>Publisher 4</Publisher>
        <Publisher>Publisher 5</Publisher>
        <Publisher>Publisher 6</Publisher>
        <Combined>44444|DA, DB, DC, DD, DE, DF|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5, Paper Title 6|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5, Publisher 6</Combined>
      </Record>
    </Records>