0

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>

2 Answers2

0

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>
zx485
  • 28,498
  • 28
  • 50
  • 59
0

I would do:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Record">
    <xsl:copy>
        <xsl:apply-templates/>
        <Combined>
            <xsl:value-of select="Key, string-join(Author, ','), string-join(Title, ','), string-join(Publisher, ',')" separator="|"/>
        </Combined>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

P.S. It should be noted that there is an underlying assumption here that none of the concatenated values will ever contain the , or | delimiting characters. Which seems like a very bold assumption - esp. regarding commas in publication titles.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • An excellent point. I'll adjust to a different delimiter to avoid commas in publication titles. I also tested the 1.0 solution. I think I can combine the answer to this question to this one: https://stackoverflow.com/questions/59957217/xsl-merging-multiple-xml-records-in-the-same-file making one XSL instead of two. If it works, I'll try to make one XSL in both 1.0 and 2.0 just to see if I can get it to work. Many thanks to all who helped. – Hammer Time Feb 05 '20 at 13:13